1. ホーム
  2. java

How to find Max Date in List<Object>?

2023-12-25 06:17:41

Question

Consider a class User

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

Now I have a List<User> of size 20, how can I find the max date in the list without using manual iterator?

How to solved?

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();