Getting object with max date property from list of objects Java 8

JavaCollectionsJava 8

Java Problem Overview


I have a class called Contact that has a Date lastUpdated; variable.

I would like to pull the Contact out of a List<Contact> that has the max lastUpdated variable.

I know that this can be done by writing a custom comparator and using Collections.max, but I was wondering if there is a way this can be done in Java 8 that does not require using a custom comparator, since I just want to pull the one with a max date in just one spot in my code, and the Contact class should not always use the lastUpdated variable for comparing instances.

Java Solutions


Solution 1 - Java

Writing custom comparator in Java-8 is very simple. Use:

Comparator.comparing(c -> c.lastUpdated);

So if you have a List<Contact> contacts, 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));

Solution 2 - Java

> and the Contact class should not always use the lastUpdated variable > for comparing instances

So you will have to provide a custom comparator whenever you want to compare multiple instances by their lastUpdated property, as it implies that this class is not comparable by default with this field.

Comparator<Contact> cmp = Comparator.comparing(Contact::getLastUpdated);

As you know you can either use Collections.max or the Stream API to get the max instance according to this field, but you can't avoid writing a custom comparator.

Solution 3 - Java

Try the following (untested):

contacts.stream().max(Comparator.comparing(Contact::getLastUpdated)).get()

Solution 4 - Java

Use List<T>.stream().max(Comparator<T>).get() after you defined a suitable Comparator.

Solution 5 - Java

Since you wanted to do it using java 8 lambdas, use this (assuming you have List<Contact> contacts):

Contact latest = contacts.stream()
     .sorted(Comparator.<Contact, Date>comparing(contact-> contact.getLastUpdated(), Comparator.reverseOrder()))
     .findFirst().get();

You can customize this comparator quite easily by simply appending further comparator. So the code is easily modifiable.

List<Contact> sortedContacts = contacts.stream()
         .sorted(Comparator.<Contact, Date>comparing(contact-> contact.getLastUpdated(), Comparator.reverseOrder())
               .thenComparing(contact -> contact.getAge()))
         .collect(Collectors.toList());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew MairoseView Question on Stackoverflow
Solution 1 - JavaTagir ValeevView Answer on Stackoverflow
Solution 2 - JavaAlexis C.View Answer on Stackoverflow
Solution 3 - JavaPuceView Answer on Stackoverflow
Solution 4 - JavaM. ShawView Answer on Stackoverflow
Solution 5 - JavaSacky SanView Answer on Stackoverflow