I've done something like this:
List<String> strList = asList("getElementById", "htmlSpecialChars", "httpRequest");
String maxOfLowercase = strList.stream()
.max((o1, o2) -> {
long lowerCount1 = o1.chars().filter(Character::isLowerCase).count();
long lowerCount2 = o2.chars().filter(Character::isLowerCase).count();
return Long.compare(lowerCount1, lowerCount2);
}).get();
But I think it is possible to make this more easier\shoter, isn't it?
There are convenient static methods in Comparator
interface which may help you to make the code shorter:
String maxOfLowercase = strList.stream()
.max(Comparator.comparingLong(o -> o.chars().filter(Character::isLowerCase).count()))
.get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With