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()

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()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)