Java Stream: find an element with a min/max value of an attribute

后端 未结 9 1753
梦毁少年i
梦毁少年i 2020-12-06 15:58

I have a stream of objects and I would like to find the one with a maximal value of some attribute that\'s expensive to calculate.

As a specific simple example, say

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 16:30

    Stream stringStream = stringList.stream();
    String coolest = stringStream.reduce((a,b)-> 
        coolnessIndex(a) > coolnessIndex(b) ? a:b;
    ).get()
    

提交回复
热议问题