Core Java

Java 8 Comparator Example

Photo of YatinYatinJanuary 16th, 2018Last Updated: February 25th, 2019
0 357 5 minutes read

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!

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 toFile -> 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 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.

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)
Want to be a Java 8 Ninja ?
Subscribe to our newsletter and download the Java 8 FeaturesUltimateGuideright now!
In order to get you up to speed with the major Java 8 release, we have compiled a kick-ass guide with all the new features and goodies! Besides studying them online you may download the eBook in PDF format!

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.

Fig. 9: Java Class (ComparatorTest.java)
Fig. 9: Java Class (ComparatorTest.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!

Fig. 10: Run Application
Fig. 10: Run Application

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.

Download
You can download the full source code of this example here:Java8Comparator
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of YatinYatinJanuary 16th, 2018Last Updated: February 25th, 2019
0 357 5 minutes read
Photo of Yatin

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).

Related Articles

Bipartite Graph

Java not equal Example

January 17th, 2020
Bipartite Graph

Java API Tutorial

October 26th, 2020
Bipartite Graph

Java Struct Example

January 8th, 2020
Bipartite Graph

Java Node Example

November 20th, 2019
Bipartite Graph

Java Swing MVC Example

January 26th, 2016
Bipartite Graph

How to call a method in Java

December 26th, 2019
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.