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

前端 未结 5 655
小鲜肉
小鲜肉 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

    Streams are perfect for these sort of problems.

    Forecast highest = forecasts.stream()
                                .max((fc1, fc2) -> fc1.getTemp() - fc2.getTemp())
                                .get();
    

提交回复
热议问题