How to use Java Lambda expression for sorting a List using comparator
- Details
- Written by Nam Ha Minh
- Last Updated on 17 August 2019   |   Print Email
The following examples illustrate how to use Lambda expressions - the main feature coming with Java SE 8 - to improve the boilerplate code of comparator written for sorting list collections.
Suppose that we have the following model class:
class Book { private String title; private float price; Book(String title, float price) { this.title = title; this.price = price; } // getters and setters public String toString() { return this.title + "-" + this.price; } }
A list of books is created as follows:
Book book1 = new Book("Head First Java", 38.9f); Book book2 = new Book("Thinking in Java", 30.0f); Book book3 = new Book("Effective Java", 50.0f); Book book4 = new Book("Code Complete", 42.5f); List<Book> listBooks = Arrays.asList(book1, book2, book3, book4);
Now, we want to sort this list by the book’s attributes such as title and price (ascending and descending). Let’s see how Lambda expressions can make the comparator code more concise compared with classic code (before Java 8).
1. Sorting a Java list collection in the classic way
As a way mentioned in the article Sorting List Collections Examples, we can write a comparator to sort the list. For example, the following code creates a comparator which compares two books by their titles:
Comparator<Book> titleComparator = new Comparator<Book>() { public int compare(Book book1, Book book2) { return book1.getTitle().compareTo(book2.getTitle()); } };
And sort the above list like this:
Collections.sort(listBooks, titleComparator);
Print the list:
System.out.println("\nAfter sorting by title:"); System.out.println(listBooks);
Output:
After sorting by title: [Code Complete-42.5, Effective Java-50.0, Head First Java-38.9, Thinking in Java-30.0]
2. Sorting a Java list collection using Lambda expression
Since Java 8 with Lambda expressions support, we can write a comparator in a more concise way as follows:
Comparator<Book> descPriceComp = (Book b1, Book b2) -> (int) (b2.getPrice() - b1.getPrice());
This comparator compares two books by their prices which cause the list to be sorted in descending order of prices, using the Lambda expression:
(Book b1, Book b2) -> (int) (b2.getPrice() - b1.getPrice());
Here, a Lambda expression can be used because the Comparator interface declares only one method - compareTo() - so the compiler can infer the method’s signature. Then pass this comparator into the Collections.sort() method as normal:
Collections.sort(listBooks, descPriceComp);
Print the list:
System.out.println("\nAfter sorting by descending price:"); System.out.println(listBooks);
Output:
After sorting by price descending: [Effective Java-50.0, Code Complete-42.5, Head First Java-38.9, Thinking in Java-30.0]
In the Lambda expression, we can even safely remove the types of the parameters because the compiler can infer them from the declared type Comparator. Here’s a more concise version of the Lambda expression:
(b1, b2) -> (int) (b1.getPrice() - b2.getPrice())
And more, this expression can be passed directly into the Collections.sort() method:
Collections.sort(listBooks, (b1, b2) -> (int) (b1.getPrice() - b2.getPrice()));
Print the list:
System.out.println("\nAfter sorting by ascending price:"); System.out.println(listBooks);
Output:
After sorting by price ascending: [Thinking in Java-30.0, Head First Java-38.9, Code Complete-42.5, Effective Java-50.0]
3. List Sorting Example Program using Lambda Expression
Assembling all the snippets above, we have the following demo program:
package net.codejava.lambda; import java.util.*; /** * This program demonstrates how to use Lambda expressions to improve code of * comparator used to sort list collections. * * @author www.codejava.net */ public class LambdaComparatorExample { public static void main(String[] args) { Book book1 = new Book("Head First Java", 38.9f); Book book2 = new Book("Thinking in Java", 30.0f); Book book3 = new Book("Effective Java", 50.0f); Book book4 = new Book("Code Complete", 42.5f); List<Book> listBooks = Arrays.asList(book1, book2, book3, book4); System.out.println("Before sorting:"); System.out.println(listBooks); Comparator<Book> titleComparator = new Comparator<Book>() { public int compare(Book book1, Book book2) { return book1.getTitle().compareTo(book2.getTitle()); } }; Collections.sort(listBooks, titleComparator); System.out.println("\nAfter sorting by title:"); System.out.println(listBooks); Comparator<Book> descPriceComp = (Book b1, Book b2) -> (int) (b2.getPrice() - b1.getPrice()); Collections.sort(listBooks, descPriceComp); System.out.println("\nAfter sorting by descending price:"); System.out.println(listBooks); Collections.sort(listBooks, (b1, b2) -> (int) (b1.getPrice() - b2.getPrice())); System.out.println("\nAfter sorting by ascending price:"); System.out.println(listBooks); } } class Book { private String title; private float price; Book(String title, float price) { this.title = title; this.price = price; } String getTitle() { return this.title; } void setTitle(String title) { this.title = title; } float getPrice() { return this.price; } void setPrice(float price) { this.price = price; } public String toString() { return this.title + "-" + this.price; } }
Output:
Before sorting: [Head First Java-38.9, Thinking in Java-30.0, Effective Java-50.0, Code Complete-42.5] After sorting by title: [Code Complete-42.5, Effective Java-50.0, Head First Java-38.9, Thinking in Java-30.0] After sorting by price descending: [Effective Java-50.0, Code Complete-42.5, Head First Java-38.9, Thinking in Java-30.0] After sorting by price ascending: [Thinking in Java-30.0, Head First Java-38.9, Code Complete-42.5, Effective Java-50.0]
Related Tutorials:
- Understand Object Ordering in Java with Comparable and Comparator
- Java Sorting a list by multiple attributes example
- Java Sorting List Collections Examples
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand Java Access Modifiers
- Understand how variables are passed in Java
- Understand Encapsulation in Java
Comments