Home » Core Java » Java 8 Comparator Example

About Yatin

Yatin
The author is graduated in Electronics & Telecommunication. During his studies, he has been involved with a significant number of projects ranging from programming and software engineering to telecommunications analysis. He works as a technical lead in the information technology sector where he is primarily involved with projects based on Java/J2EE technologies platform and novel UI technologies.

Java 8 Comparator Example

Hello readers, this tutorial explains how to use Lambda expressions in order to improve the boilerplate code of the Comparator written for sorting the list collections.
 
 
 
 
 
 
 
 
 
 

1. Introduction

The comparator interface has undergone a major overhaul in Java8 while still retaining its essence which is to compare and sort objects in the collections. Comparator now supports declarations via lambda expressions as it is a Functional Interface. Here is a simple source code for the java.util.function.Comparator interface.

package java.util.function;
	
@FunctionalInterface
public interface Comparator<T> {
	/**** Rest Code Goes Here *****/
}

The comparator has a new method i.e. comparing() which uses an instance of the java.util.function.Function functional interface. This method is specified by using the lambda expressions or its equivalent method reference for creating the Comparator instance. The comparing() 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 a Function<T,R> functional interface instance as an input, where T is the type of input object and R is the sort key which is returned (or extracted) from the input object when Function<T,R> processes it. In addition, developers can now implement the multiple sort criteria’s which can be clubbed using the comparing() method with a thenComparing() method. Java8 Comparator provides a default method thenComparing() 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 the Comparator<T> interface, override the compare() method of the interface with the desired comparison logic and uses the Collections.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 the Collections.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!

Fig. 1: Application Project Structure

Fig. 1: Application Project Structure

2.3 Project Creation

This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project.

Fig. 2: Create Maven Project

Fig. 2: Create 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.

Fig. 3: Project Details

Fig. 3: Project Details

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.

Fig. 4: Archetype Parameters

Fig. 4: Archetype Parameters

Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.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 the src/main/java folder, New -> Package.

Fig. 5: Java Package Creation

Fig. 5: Java Package Creation

A new pop window will open where we will enter the package name as: com.jcg.java.

Fig. 6: Java Package Name (com.jcg.java)

Fig. 6: Java Package Name (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.

Fig. 7: Java Class Creation

Fig. 7: Java Class Creation

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.

Fig. 8: Java Class (Employee.java)

Fig. 8: Java Class (Employee.java)

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.

Fig. 9: Java Class (ComparatorTest.java)

Fig. 9: Java Class (ComparatorTest.java)

3.1.1 Implementation of Model Class

This POJO class is used to store the employee data i.e. id, name, and age. 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;
	}

	@Override
	public 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 the Comparator 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>() {
			@Override
			public 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 the ComparatorTest class, Run As -> Java Application. Developers can debug the example and see what happens after every step!

Fig. 10: Run Application

Fig. 10: Run Application

5. Project Demo

The application shows the following logs as output for the Comparator 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 the Comparator<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 of Comparator interface in Java8.

Download
You can download the full source code of this example here: Java8Comparator
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of