Stream<String> stringStream = stringList.stream();
String coolest = stringStream.reduce((a,b)->
coolnessIndex(a) > coolnessIndex(b) ? a:b;
).get()
if calling coolnessIndex
is expensive we can use distinct
so it’s called only for distinct elements (assuming that the coolnesIndex is the same for equal elements)
Stream<String> stringStream = stringList.stream().distinct();
String coolest = stringStream.reduce((a,b)->
coolnessIndex(a) > coolnessIndex(b) ? a:b;
).get()
Stream distinct()