Optional<Integer> opInt = Arrays.asList(s.split("\\s")).stream()
.map(l -> l.length())
.filter(l -> l % 2 == 0)
.sorted(Comparator.reverseOrder())
.findFirst();
System.out.println(opInt.get());
Have written code above for that. Can we write concise code for it.
Instead of using Arrays.asList().stream()
, use Arrays.stream(…)
.
You can use method reference instead of using the lambda. l -> l.length()
.
You can replace .sorted(Comparator.reverseOrder())
with a
.max()
Refactored Code:
final OptionalInt max = Arrays.stream(s.split(" "))
.mapToInt(String::length)
.filter(l -> l % 2 == 0)
.max();
System.out.println(max.orElse(0));