Saturday, June 27, 2015

Java 8: Sorting a List using Lambdas and Method References

This post shows how you can use Java 8 lambda expressions and method references to sort a list of Person objects by age. In Java 8, the List interface has a sort method, which expects a Comparator to compare two objects.

Traditionally, you would either sort a list by creating a specific class that implements the Comparator interface, like this:

public class AgeComparator implements Comparator<Person> {
  @Override
  public int compare(Person p1, Person p2) {
    return Integer.compare(p1.getAge(), p2.getAge());
  }
}

list.sort(new AgeComparator());

or, you would use an anonymous class, like this:

list.sort(new Comparator<Person>() {
  @Override
  public int compare(Person p1, Person p2) {
    return Integer.compare(p1.getAge(), p2.getAge());
  }
});

As you can see, this is quite verbose!

Java 8 introduces lambda expressions, which allow you to pass code more concisely. Since Comparator is a functional interface, you can use a lambda expression to sort the list:

list.sort((p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));

You may have noticed that the Comparator class has a static method called comparingInt that takes a ToIntFunction and returns a Comparator object. So, we can rewrite the code above to:

import static java.util.Comparator.comparingInt;
list.sort(comparingInt(p -> p.getAge()));

Finally, we can improve our code even further by using a method reference, which is just "syntactic sugar" for a lambda expression:

list.sort(comparingInt(Person::getAge));

The final solution is not only shorter but is also easier to read :)

28 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Very nice post to keep sharing... Thanks for giving very nice information from your post… Java Training in Chennai | RPA Training in Chennai

    ReplyDelete
  3. Amazing blog about the various informative information on the programming languages... Java Training in Chennai | Blue prism Training in Chennai

    ReplyDelete
  4. Learned a lot from your blog. Good creation and hats off to the creativity of your mind. Share more like this.
    RPA Training Institutes in Chennai | RPA Training in Chennai | Blue Prism Training in Chennai | UiPath Training in Chennai

    ReplyDelete
  5. I believe that your blog will surely help the readers who are really in need of this vital piece of information. Waiting for your updates.

    TOEFL Classes in Chennai
    Best TOEFL Classes in Chennai
    TOEFL in Chennai
    TOEFL Classes near me
    Spanish Classes in Chennai
    Spanish Language Course in Chennai
    Spanish Courses in Chennai

    ReplyDelete

  6. Awwsome informative blog ,Very good information thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
    Airport Management Courses in Chennai | Airport Management Training in Chennai | Diploma in Airport Management Course in Chennai | Airlines Training Chennai | Airline Academy in Chennai

    ReplyDelete
  7. Great informative bog. Thanks for sharing such a valuable information with us.

    Education
    Technology

    ReplyDelete
  8. I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
    Hadoop Training in Chennai
    CCNA Training in Chennai
    Big Data Course in Chennai
    Big Data Hadoop Training in Chennai
    CCNA Training institute in Chennai
    CCNA institute in Chennai

    ReplyDelete
  9. I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.
    R Programming Training in Chennai | R Programming Training in Chennai with Placement | R Programming Interview Questions and Answers | Trending Software Technologies in 2018

    ReplyDelete
  10. Great efforts put it to find the list of articles which is very useful to know, Definitely will share the same to other forums.Roles and reponsibilities of hadoop developer | hadoop developer skills Set | hadoop training course fees in chennai | Hadoop Training in Chennai Omr

    ReplyDelete
  11. Informative post,It is useful for me to clear my doubts.I hope others also like the information you gave in your blog.
    Selenium Certification Training in T nagar
    Selenium Courses in T nagar
    Selenium Training Institutes in OMR
    Selenium Courses in OMR

    ReplyDelete