Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Max Date in List<Object>?

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?

like image 537
user2783484 Avatar asked Jan 08 '14 12:01

user2783484


People also ask

How do I find the max date in a list of dates using Java?

stream() . map(u -> u. date) . max(Date::compareTo) .

How to find Min and max date in Java?

Finding Min or Max Date. To get max or min date from a stream of dates, you can use Comparator. comparing( LocalDate::toEpochDay ) Comparator. The toEpochDay() function returns the count of days since epoch i.e. 1970-01-01.

What is the max date in Java?

For the Date/Time (TZ) data type, the maximum date is January 18, 2038.


1 Answers

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(); 
like image 146
assylias Avatar answered Sep 30 '22 03:09

assylias