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?
stream() . map(u -> u. date) . max(Date::compareTo) .
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.
For the Date/Time (TZ) data type, the maximum date is January 18, 2038.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With