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.
|
|
|
boolean result = false;
// 1. Using Chars() + isLetter method result = str.toLowerCase() .chars() .filter(Character::isLetter) .distinct() .count() == 26; System.out.println("Using Chars() + isLetter method : is String Pangram : "+ result);
// 2. Using Set Set<Character> set = str.toLowerCase() .chars() .mapToObj(c -> (char)c) .filter(Character::isLetter) .collect(Collectors.toSet()); System.out.println("Using Set : is String Pangram : "+ (set.size() == 26));
// 3. Using rangeClosed and allMatch method result = false; result = IntStream.rangeClosed('a', 'z') .allMatch(c -> str.toLowerCase().indexOf(c) != -1); System.out.println("Using rangeClosed and allMatch method : is String Pangram : "+ result);
// 4. Using rangeClosed and noneMatch method result = false; result = IntStream.rangeClosed('a', 'z') .noneMatch(c -> str.toLowerCase().indexOf(c) == -1); System.out.println("Using rangeClosed and noneMatch method : is String Pangram : "+ result); // 5. Using rangeClosed and anyMatch method result = false; result = IntStream.rangeClosed('a', 'z') .anyMatch(c -> str.toLowerCase().indexOf(c) != -1); System.out.println("Using rangeClosed and anyMatch method : is String Pangram : "+ result); // 6. Using Chaining Collections result = false; result = str.toLowerCase() .chars() .filter(Character::isLetter) .mapToObj(c -> (char)c) .collect(Collectors.collectingAndThen(Collectors.toSet(), set1 -> set1.size() == 26)); System.out.println("Using Chaining Collections : is String Pangram : "+ result); // 7. Using GroupingBy Map<Character, Long> map = str.toLowerCase() .chars() .distinct() .filter(Character :: isLetter) .mapToObj(c -> (char)c) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println("Using GroupingBy : is String Pangram : "+ (map.size() == 26)); // 8. Using Stream of and split method result = false; String strDup = "abcdefghijklmnopqrstuvwxyz"; result = Stream.of(strDup.split("")) .allMatch(c -> str.toLowerCase().contains(c)); System.out.println("Using Stream of and split method: is String Pangram : "+ result);
Output: Using Chars() + isLetter method : is String Pangram : true Using Set : is String Pangram : true Using rangeClosed and allMatch method : is String Pangram : true Using rangeClosed and noneMatch method : is String Pangram : true Using rangeClosed and anyMatch method : is String Pangram : true Using Chaining Collections : is String Pangram : true Using GroupingBy : is String Pangram : true Using Stream of and split method: is String Pangram : true |
0 Comments