Java - getting max value from an arraylist of objects?

后端 未结 5 967
北恋
北恋 2020-11-27 08:14

Is there an easy way to get the max value from one field of an object in an arraylist of objects?

For example, out of the following object, I was hoping to get the h

5条回答
  •  情话喂你
    2020-11-27 08:25

    With Java 8 you can use stream() together with it's predefined max() function and Comparator.comparing() functionality with lambda expression:

    ValuePairs maxValue = values.stream().max(Comparator.comparing(v -> v.getMValue())).get();
    

提交回复
热议问题