Hello readers, this tutorial explains how to use Lambda expressions in order to improve the boilerplate code of theComparator written for sorting the list collections.
1. Introduction
Thecomparator interface has undergone a major overhaul in Java8 while still retaining its essence which is tocompare andsort objects in the collections. Comparator now supportsdeclarations vialambda expressions as it is aFunctional Interface. Here is a simple source code for thejava.util.function.Comparator interface.
package java.util.function;@FunctionalInterfacepublic interface Comparator<T> {/**** Rest Code Goes Here *****/}The comparator has a new method i.e.comparing() which uses an instance of thejava.util.function.Function functional interface. This method is specified by using the lambda expressions or its equivalent method reference for creating theComparator instance. Thecomparing() method has the following signature.
static <T,U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor)
This method works by taking aFunction<T,R> functional interface instance as an input, whereT is the type of input object andR is the sort key which is returned (or extracted) from the input object whenFunction<T,R> processes it. In addition, developers can now implement themultiple sort criteria’s which can be clubbed using thecomparing() method with athenComparing() method. Java8 Comparator provides a default methodthenComparing() which has the following signature.
default Comparator<T> thenComparing(Comparator<? super T> other)
This method does the second level sorting if in case the first level sorting is indecisive.
1.1 How Comparator was used prior to Java 8?
Until Java7,Comparator interface could only be used in one single way. Given a collection of objects of type<T> to sort, one would create an implementation of theComparator<T> interface, override thecompare() method of the interface with the desired comparison logic and uses theCollections.sort() or similar such methods in the Collections API to sort the collection of objects. 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);
Since Java8 with Lambda expressions support, developers 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 the descending order of prices by using the Lambda expression. The comparator instance is then passed into theCollections.sort() method as normal.
Now, open up the Eclipse Ide and let’s see how to work with Java8 Lambda expressions using the Comparator interface in Java.
2. Java8 Comparator Example
2.1 Tools Used
We are using Eclipse Oxygen, JDK 8 and Maven.
2.2 Project Structure
Firstly, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!

2.3 Project Creation
This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go toFile -> New -> Maven Project.
In the New Maven Project window, it will ask you to select project location. By default, ‘Use default workspace location’ will be selected. Select the ‘Create a simple project (skip archetype selection)’ checkbox and just click on next button to proceed.
It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default:0.0.1-SNAPSHOT.
Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and apom.xml file will be created. It will have the following code:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>Java8Comparator</groupId><artifactId>Java8Comparator </artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging></project>
Developers can start adding the dependencies that they want. Let’s start building the application!
3. Application Building
Below are the steps involved in developing this application.
3.1 Java Class Creation
Let’s create the required Java files. Right-click on thesrc/main/java folder,New -> Package.
A new pop window will open where we will enter the package name as:com.jcg.java.

Once the package is created in the application, we will need to create the model and the implementation classes to illustrate the implementation of Comparator interface in Java8. Right-click on the newly created package:New -> Class.
A new pop window will open and enter the file name as:Employee. The model (i.e.POJO) class will be created inside the package:com.jcg.java.


Thank you!
We will contact you soon.
Repeat the step (i.e. Fig. 7) and enter the filename as:ComparatorTest. The implementation class will be created inside the package:com.jcg.java.

3.1.1 Implementation of Model Class
ThisPOJO class is used to store the employee data i.e.id,name, andage. Let’s see the simple code snippet.
Employee.java
package com.jcg.java;public class Employee {private int id, age;private String name;/**** Employee Default Constructor ****/public Employee() { }/**** Employee Parameterized Constructor ****/public Employee(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "(" + this.id + ", " + this.name + ", " + this.age + ")";}}3.1.2 Implementation of Comparator Class
The following example sorts the list of employees using the anonymous class and the lambda expression. Let’s have a look at a simple code example where theComparator interface is being used.
ComparatorTest.java
package com.jcg.java;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class ComparatorTest {public static void main(String[] args) {List<Employee> employeeList1 = getEmployees();System.out.println("-----------Before Sorting List--------------\n" + employeeList1);/**** Anonymous Class (Old Way) ****/Comparator<Employee> comparator1 = new Comparator<Employee>() {@Overridepublic int compare(Employee emp1, Employee emp2) {return new Integer(emp1.getAge()).compareTo(new Integer(emp2.getAge()));}};/*** Sorting the Employee List Using Comparator By Age ****/Collections.sort(employeeList1, comparator1);System.out.println("-------------After Sorting list Using Anonymous Class-------------");System.out.println(employeeList1);List<Employee> employeeList2 = getEmployees();/**** Lambda Expression from Java8 ****/Comparator<Employee> comparator2 = (emp1, emp2) -> {return new Integer(emp1.getAge()).compareTo(new Integer(emp2.getAge()));};/*** Sorting the Employee List Using Comparator By Age ****/Collections.sort(employeeList2, comparator2);System.out.println("---------------After Sorting List Using Lambda Expression From Java8--------------");System.out.println(employeeList2);}/**** Helper Method #1 - This Method Prepares The Dummy Employee List ****/private static List<Employee> getEmployees() {List<Employee> employees = new ArrayList<Employee>();employees.add(new Employee(105, "Harry", 28));employees.add(new Employee(108, "Daniel", 35));employees.add(new Employee(110, "Lucifer", 40));employees.add(new Employee(102, "April", 25));employees.add(new Employee(104, "Toby", 22));return employees;}}Do remember, developers will have to use the ‘JDK 1.8’ dependency for implementing the Stream’s usage in their applications.
4. Run the Application
To run the application, right-click on theComparatorTest class,Run As -> Java Application. Developers can debug the example and see what happens after every step!
5. Project Demo
The application shows the following logs as output for theComparator functional interface.
ConsumerTest.java
-----------Before Sorting List--------------[(105, Harry, 28), (108, Daniel, 35), (110, Lucifer, 40), (102, April, 25), (104, Toby, 22)]-------------After Sorting list Using Anonymous Class-------------[(104, Toby, 22), (102, April, 25), (105, Harry, 28), (108, Daniel, 35), (110, Lucifer, 40)]---------------After Sorting List Using Lambda Expression From Java8--------------[(104, Toby, 22), (102, April, 25), (105, Harry, 28), (108, Daniel, 35), (110, Lucifer, 40)]
That’s all for this post. Happy Learning!
6. Conclusion
In this tutorial, we looked at what is theComparator<T> interface defined in Java8. I hope this article served developers whatever they were looking for.
7. Download the Eclipse Project
This was an example ofComparator interface in Java8.
You can download the full source code of this example here:Java8Comparator

Thank you!
We will contact you soon.









