How to find Max Date in List<Object>?

前端 未结 6 2223
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 19:13

Consider a class User

public class User{
  int userId;
  String name;
  Date date;
}

Now I have a List

6条回答
  •  心在旅途
    2020-12-23 19:46

    Since you are asking for lambdas, you can use the following syntax with Java 8:

    Date maxDate = list.stream().map(u -> u.date).max(Date::compareTo).get();
    

    or, if you have a getter for the date:

    Date maxDate = list.stream().map(User::getDate).max(Date::compareTo).get();
    

提交回复
热议问题