JavaStream: Characters
Continuing from the last blog how about checking a pangram using streams.
public class PangramChecker {
private String allChars = "abcdefghijklmnopqrstuvwxyz";
public boolean isPangram(String input) {
if (input.length() <= 0 || input.isBlank()){
return false;
}
return (allChars.chars().map(x -> (char) x).filter(x -> input.toLowerCase().indexOf(x) >= 0).count() == 26);
}
}
All the for loops have been squeezed into a simple line
The Logic
- Define a
String
for all the characters in the English language –allChars
- Get the Stream using
allChars.chars()
- Since it returns an
int
map it toChar
- Look for every character one by one in the
Input String
usingindexOf
. (It returns -1 for character not found.) - Get the count of all the characters found, if anyone was not found we will have less than 26.
It can be further optimized… want to try?