Approach 1 — Using max with a Comparator
Optional<String> longest = words.stream()
.max(Comparator.comparingInt(String::length));
Approach 2 — Using reduce Pairwise
Optional<String> longest2 = words.stream()
.reduce((a, b) -> a.length() >= b.length() ? a : b);
Explanation:
maxis readable and efficient.reducedemonstrates functional programming by comparing elements pairwise.
