I have a class called Contact
that has a Date lastUpdated;
variable.
I would like to pull the Contact
out of a List
Writing custom comparator in Java-8 is very simple. Use:
Comparator.comparing(c -> c.lastUpdated);
So if you have a List
, you can use
Contact lastContact = Collections.max(contacts, Comparator.comparing(c -> c.lastUpdated));
Or, using method references:
Contact lastContact = Collections.max(contacts, Comparator.comparing(Contact::getLastUpdated));