How to find the max element from an array list of objects?

前端 未结 5 709
小鲜肉
小鲜肉 2021-01-18 13:55

Collections.max(arraylist) doesn\'t work, and a regular for loop won\'t work either.

What I have is:

ArrayList

        
5条回答
  •  情书的邮戳
    2021-01-18 14:31

    Another solution is use map reduce:

    Optional element = forecasts
                         .stream()
                         .reduce((a,b) -> a.getTemperature() > b.getTemperature() ? a : b );
    

    In this way you could even use use parallelStream()

提交回复
热议问题