Java 8 streams are my new golden hammer that I try to use as often as possible with often enormous gains in brevity and readability. Getting multiple return values, such as a maximum value and its associated element, is a bit cumbersome though (and requires an additional "pair" class).
Should I go back to "normal Java" for this task or is this syntax preferable? Is there a more concise way?
List<String> names = Arrays.asList("John","Paul","Ringo"); Pair<String,Integer> longestName = names.stream() .map(n->new Pair<>(n,n.length())) // pretend n.length() to be a lengthy operation .max(Comparator.comparing(Pair::getB)).get(); System.out.println(longestName.getA()+" "+longestName.getB());
P.S.: With "lengthy operation" I mean long running, as in I don't want to calculate it twice.