Hi All,
Today I have released new video on "Java Interview Question: Check String is Pangram Using Java 8 Streams"
This is frequently asked question in Java Interview.
Please watch full video, share, like and Subscribe Youtube channel and press bell icon. So you will get latest video notification.
|
|
|
import java.util.List; import java.util.stream.Collectors; public class FindTheNumberOfVowelsAndConsonents { public static void main(String[] args) { String str = "I am Experienced Developer"; long vowels = 0; long consonents = 0;
// 1. Using Count, map and filter List<Character> list = str.chars() .mapToObj(c -> (char)c) .filter(Character::isLetter) .collect(Collectors.toList());
vowels = str.toLowerCase() .chars() .mapToObj(c -> (char)c) .filter(Character::isLetter) .filter(c -> "aeiou".contains(c.toString())) .count();
System.out.println("Using Count, map and filter : Vowels : "+ vowels); System.out.println("Using Count, map and filter : Consonents : "+ (list.size()-vowels));
// 2. Using count second approach vowels = 0; consonents = 0;
vowels = str.toLowerCase() .chars() .mapToObj(c -> (char)c) .filter(c -> "aeiou".indexOf(c) >= 0) .count();
consonents = str.toLowerCase() .chars() .mapToObj(c -> (char)c) .filter(Character::isLetter) .filter(c -> "aeiou".indexOf(c) == -1) .count(); System.out.println("Using count second approach : Vowels : "+ vowels); System.out.println("Using count second approach : Consonents : "+ consonents);
// 3. Using Conditional Operator vowels = 0; consonents = 0;
vowels = str.toLowerCase() .chars() .filter(Character::isLetter) .map(c -> "aeiouAEIOU".indexOf(c) >= 0 ? 1 : 0) .sum();
consonents = str.toLowerCase() .chars() .filter(Character::isLetter) .map(c -> "aeiouAEIOU".indexOf(c) == -1 ? 1 : 0) .sum(); System.out.println("Using Conditional Operator : Vowels : "+ vowels); System.out.println("Using Conditional Operator : Consonents : "+ consonents); // 4. Using Regular Expression vowels = 0; consonents = 0;
vowels = str.replaceAll("[^aeiouAEIOU]", "").chars().count(); consonents = str.replaceAll("[^a-zA-Z]", "").length() - vowels; System.out.println("Using Conditional Operator : Vowels : "+ vowels); System.out.println("Using Conditional Operator : Consonents : "+ consonents); // 5. Using Filtering and Counting vowels = 0; consonents = 0;
vowels = str.toLowerCase() .chars() .mapToObj(c -> (char)c) .collect(Collectors.filtering(c -> "aeiou".contains(c.toString()), Collectors.counting()));
consonents = str.toLowerCase() .chars() .mapToObj(c -> (char)c) .filter(Character::isLetter) .collect(Collectors.filtering(c -> !"aeiou".contains(c.toString()), Collectors.counting()));
System.out.println("Using Filtering and Counting : Vowels : "+ vowels); System.out.println("Using Filtering and Counting : Consonents : "+ consonents); } } output: Using Count, map and filter : Vowels : 11 Using Count, map and filter : Consonents : 12 Using count second approach : Vowels : 11 Using count second approach : Consonents : 12 Using Conditional Operator : Vowels : 11 Using Conditional Operator : Consonents : 12 Using Conditional Operator : Vowels : 11 Using Conditional Operator : Consonents : 12 Using Filtering and Counting : Vowels : 11 Using Filtering and Counting : Consonents : 12 |
0 Comments