Java Stream: find an element with a min/max value of an attribute
Stream<String> stringStream = stringList.stream();
String coolest = stringStream.reduce((a,b)->
coolnessIndex(a) > coolnessIndex(b) ? a:b;
).get()
Here's a variant using an Object[]
as a tuple, not the prettiest code but concise
String coolestString = stringList
.stream()
.map(s -> new Object[] {s, coolnessIndex(s)})
.max(Comparator.comparingInt(a -> (int)a[1]))
.map(a -> (String)a[0])
.orElse(null);