The Complete Java 8 Comparator Tutorial with examples

Do You Know – Comparator in Java 7 had just 2 methods – compare() and equals(). The enhanced Comparator in Java 8 now boasts of 19 methods. Yes, 17 more methods! What’s more Comparator now is an official Functional Interface as well!

Comparator has undergone a major overhaul in Java 8 while still retaining its essence which is to compare and sort objects in Collections. Comparator now supports declarations via lambda expressionsRead Lambda Expressions Tutorial as it is a Functional Interface. Comparator has a new method comparing() which uses an instance of java.util.function.Function functional interface, specified using lambda expression or its equivalent method reference, for Comparator instance creation. In addition, multiple sort criteria can now be clubbed using comparing() with a thenComparing() method. The range of new capabilities is rounded off with methods for using natural comparison order, in-built Null handling and sort order reversal.

In this tutorial, we will first take a quick look at how Comparators were used before Java 8. We will then take an in-depth look at the new Comparator aspects mentioned above to see how java.util.Comparator has evolved into an enhanced comparison and ordering utility in Java 8.

How Comparator was used prior to Java 8
Until Java 7, 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 Comparator<T> interface, override the compare() method of the interface with the desired comparison logic and use Collections.sort() or similar such methods in Collections API to sort the collection of objects.

Let us now see an example of how Comparators were used prior to Java 8. Let us first create a Class Employee which will be the type of object we will be sorting across all our examples for Java 7 and Java 8 Comparators-

Employee.java
//Employee.java
package com.javabrahman.java8;
public class Employee{
  private String name;
  private Integer age;
  public Employee(String name, Integer age){
    this.name=name;
    this.age=age;
  } 
  //--getters/setters for name and age go here  
  public String toString(){
    return "Employee Name:"+this.name
      +"  Age:"+this.age;
  }
  @Override
  public boolean equals(Object obj) {
     if (obj == this) {
       return true;
     }
     if (!(obj instanceof Employee)) {
       return false;
     }
     Employee empObj = (Employee) obj;
       return this.age==empObj.age
         && this.name.equalsIgnoreCase(empObj.name);
  }
  @Override
  public int hashCode() {
    int hash = 1;
    hash = hash * 17 + this.name.hashCode();
    hash = hash * 31 + this.age;
    return hash;
  }
}

Next we will see how Comparator was used to sort collections prior to Java 8 –

How Comparator was used prior to Java 8
package com.javabrahman.java8.comparator;
import java.util.Comparator;
import com.javabrahman.java8.Employee;
public class EmployeeComparator implements Comparator<Employee> {
  @Override
  public int compare(Employee emp1, Employee emp2) {
    return (emp1.getName().compareTo(emp2.getName()));
  }
}
//ComparatorOldWay.java
package com.javabrahman.java8.comparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.javabrahman.java8.Employee;
public class ComparatorOldWay {
  static List<Employee> employeeList = 
      Arrays.asList(new Employee("Tom Jones", 45), 
        new Employee("Harry Major", 35),
        new Employee("Harry Major", 25), 
        new Employee("Ethan Hardy", 65), 
        new Employee("Nancy Smith", 15),
        new Employee("Deborah Sprightly", 29));

  public static void main(String args[]) {
    Collections.sort(employeeList, new EmployeeComparator());
    employeeList.forEach(System.out::println);
  }
}
 OUTPUT of the above code
Employee Name:Deborah Sprightly Age:29
Employee Name:Ethan Hardy Age:65
Employee Name:Harry Major Age:35
Employee Name:Harry Major Age:25
Employee Name:Nancy Smith Age:15
Employee Name:Tom Jones Age:45
Quick Explanation of the above code-

  • EmployeeComparator class implements Comparator inteface and overrides the compare() method to compare Employee objects passed to it based on the natural ordering of their names of String type.
  • ComparatorOldWay class sorts a static list of Employee objects using an instance of EmployeeComparator and the Collections.sort() method.
  • Output shows that the employeeList gets sorted alphabetically based on the names of the employees.

Java 8’s Comparator is a Functional Interface
Owing to the fact that the Comparator interface has just one abstract method, compare(), it automatically qualifies to be a Functional Interface
Click to read detailed Article on Functional Interfaces
in Java 8. Nevertheless, Java 8 designers have gone ahead and annotated the Comparator class interface @FunctionalInterface to enforce its role as a Functional Interface. Being a functional interface, Comparator can now be used as an assignment target for a lambda expression or a method reference.

Java 8’s Comparator as an assignment target for LambdaExpressions
Given the fact that its a functional interface, an instance of a Comparator can now be created in Java 8 with a lambda expression specifying its comparison logic. Take a look at the code snippet below –

Defining a Comparator using lambda expression
package com.javabrahman.java8.comparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.javabrahman.java8.Employee;
public class ComparatorsInJava8 {
  static List<Employee> employeeList = 
      Arrays.asList(new Employee("Tom Jones", 45), 
        new Employee("Harry Major", 35),
        new Employee("Harry Major", 25), 
        new Employee("Ethan Hardy", 65), 
        new Employee("Nancy Smith", 15),
        new Employee("Deborah Sprightly", 29));
  public static void main(String[] args) {
    Comparator<Employee> empNameComparator = (Employee emp1, Employee emp2) -> {
      return (emp1.getName().compareTo(emp2.getName()));
    };
    Collections.sort(employeeList, empNameComparator);
    employeeList.forEach(System.out::println);
  }
}
 OUTPUT of the above code
Employee Name:Deborah Sprightly Age:29
Employee Name:Ethan Hardy Age:65
Employee Name:Harry Major Age:35
Employee Name:Harry Major Age:25
Employee Name:Nancy Smith Age:15
Employee Name:Tom Jones Age:45
Quick Explanation of the above code-

  • ComparatorsInJava8 class uses the same list of Employee objects as that used for the previous example for Java 7 style of Comparator.
  • An instance of the Comparator, empNameComparator, is created using a lambda expression.
  • The lambda expression takes 2 Employee instances,emp1 and emp2, as input and outputs the comparison of their names using the natural comparison order of Strings.
  • Using empNameComparator for sorting results in a correctly sorted Employee list by name.

Java 8 Comparator’s comparing() method’s working
The comparing() method is a new static method introduced in Comparators in Java 8. It has the signature –

static <T,U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor)

comparing() 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.

Let’s see a code snippet to understand the use of comparing() method. I have removed the code which was same as previous example below for brevity –

Comparator creation using static method comparing()
public static void main(String[] args) {
 Comparator<Employee> comparatorObj=Comparator.comparing(Employee emp -> emp.getName());
 Collections.sort(employeeList, comparatorObj);
 employeeList.forEach(System.out::println);
}
 OUTPUT of the above code
Employee Name:Deborah Sprightly Age:29
Employee Name:Ethan Hardy Age:65
Employee Name:Harry Major Age:35
Employee Name:Harry Major Age:25
Employee Name:Nancy Smith Age:15
Employee Name:Tom Jones Age:45
Quick Explanation of the above code-

  • An instance of the Comparator, comparatorObj, is created using the static method Comparator.comparing().
  • The comparing() is passed a lambda expression, which corresponds to a Function<T,R> instance accepting an Employee object as input and returns an employee name – the sort key.
  • Using comparatorObj for sorting results in a correctly sorted Employee list by name.
  • NOTE – Instead of the lambda expression, you can also use an equivalent method reference as well. The comparing() method with a method reference will then be written like this –
    Comparator empNameComparator = Comparator.comparing(Employee::getName);

Java 8 Comparator’s thenComparing() method for multiple sort criteria
Many-a-times we need to sort with multiple sort orders. I.e. on more than one attributes of an object. The second level sort order gets used if the first level sort criteria is indecisive. In the list of Employees we are using as an example, there are two employees with the name Harry Major. Let us take a second sort order of age and say that in such cases we will put the employee with the younger age first.

For exactly these kinds of multiple sort ordering, Java 8 Comparator provides a default method thenComparing() which has the signature –

default Comparator<T> thenComparing(Comparator<? super T> other)

The thenComparing() method then does the second level sort, if the first level sort is indecisive. Let us extend the above code example to add a second-level sort criteria by age.

Comparator's thenComparing() method for multiple sort criteria
public static void main(String[] args) {
  Comparator<Employee> empNameComparator = Comparator.comparing(Employee::getName).thenComparing(Employee::getAge);
  Collections.sort(employeeList, empNameComparator);
  employeeList.forEach(System.out::println);
}
 OUTPUT of the above code
Employee Name:Deborah Sprightly Age:29
Employee Name:Ethan Hardy Age:65
Employee Name:Harry Major Age:25
Employee Name:Harry Major Age:35
Employee Name:Nancy Smith Age:15
Employee Name:Tom Jones Age:45
Quick Explanation of the above code-

  • First comparing() method is invoked with method reference for Employee's getName() method. This returns a Comparator instance with the first level sort based on Employee name as we saw in previous section.
  • We append .thenComparing(Employee::getAge) to the Comparator instance returned using comparing() method, which adds a second level sort based on Employee's getAge() method.
  • The output is as expected with the employee named ‘Harry Major’ with the lesser age placed earlier in the sorted employeeList than his elder namesake employee.

Java 8 Comparator’s natural order comparison methods
Java 8 Comparator supports natural order comparison of elements in a Collection. I.e. instead of defining our own comparison logic, we can instead use the inherent natural order defined for that type of element via its implementation of Comparable interface. Comparator provides two static methods naturalOrder() and reverseOrder() to allow natural order comparison and reverse natural order comparison respectively. These have the syntax –

static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
AND
static <T extends Comparable<? super T>> Comparator<T> reverseOrder()

Let us take the case of String type which has natural comparison order defined as alphabetical. To use our existing example for sorting using Employee objects, we will extract out all the names of Employees by utilizing the stream() method to convert the Employee list into a Stream of Employee objects. We will then map these Employee objects using a Function into their names and collect these names into a list of Strings holding these names.
(In case you are not aware of mapping of elements in a Stream using map() method you can read the tutorial on Stream.map() method
Tutorial on how Stream API’s map() method works
, and tutorial on Function interface
Read java.util.function.Function interface tutorial
.)

Java 8 Comparator natural order sorting
public static void main(String[] args) {
  List<String> empNames = employeeList.stream().map(Employee::getName).collect(Collectors.toList());
  empNames.sort(Comparator.naturalOrder());
  empNames.forEach(System.out::println);
 OUTPUT of the above code
Deborah Sprightly
Ethan Hardy
Harry Major
Harry Major
Nancy Smith
Tom Jones
Quick Explanation of the above code-

  • Employee names are extracted into an empNames List as explained before the code snippet.
  • empNames List is then sorted using the Comparator.naturalOrder() method which returns a Comparator instance of String’s natural comparison order based on the empName’s generic type of String.
  • Employee names are sorted in natural comparison order, i.e. alphabetical order, and printed.
  • Instead of invoking the Comparator.naturalOrder() method to the empNames.sort() method, you can invoke the Comparator.reverseOrder() method in order to sort the empNames List in reverse of natural comparison order or reverse alphabetical order.

Java 8 Comparator’s null handling using nullsFirst()and nullsLast() methods
There are instances where the sort key value is nullable. We have to then decide whether we want to place objects with sort key as null earlier in the ordering than the objects with non-null sort key or at the end of the list after the objects with non-null sort key. Java 8 Comparators provide nullsFirst() and nullsLast() static methods for exactly such sort order handling of null valued sort keys. These methods have the signature –

static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
AND
static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)

To understand the functioning of these methods, let us make the two of the employee names as null. The below two sections of code and respective output shows how the use of nullsFirst() and nullsLast() handles sorting when some objects have sort-key as null.

Java 8 Comparator with sort key null and using nullsFirst()
public class ComparatorsInJava8 {
  static List<Employee> employeeList = Arrays.asList(
      new Employee(null, 45),
      new Employee("Harry Major", 35),
      new Employee("Harry Major", 25),
      new Employee(null, 65),
      new Employee("Nancy Smith", 15),
      new Employee("Deborah Sprightly", 29));
  public static void main(String[] args) {
    Comparator<Employee> empNameComparator = Comparator.comparing(Employee::getName, Comparator.nullsFirst(String::compareTo));
    Collections.sort(employeeList, empNameComparator);
    employeeList.forEach(System.out::println);
   }
}
 OUTPUT of the above code
Employee Name:null Age:45
Employee Name:null Age:65
Employee Name:Deborah Sprightly Age:29
Employee Name:Harry Major Age:35
Employee Name:Harry Major Age:25
Employee Name:Nancy Smith Age:15
Keeping the employee list same and invoking the Comparator using nullLast()
Java 8 Comparator with sort key null and using nullsLast()
public static void main(String[] args) {
    Comparator<Employee> empNameComparator = Comparator.comparing(Employee::getName, Comparator.nullsLast(String::compareTo));
    Collections.sort(employeeList, empNameComparator);
    employeeList.forEach(System.out::println);
   }
}
 OUTPUT of the above code
Employee Name:Deborah Sprightly Age:29
Employee Name:Harry Major Age:35
Employee Name:Harry Major Age:25
Employee Name:Nancy Smith Age:15
Employee Name:null Age:45
Employee Name:null Age:65
Quick Explanation of the above code-

  • Using nullsFirst(), the comparator places the two Employee objects with the sort key null (employee name null) before the other Employee objects in the list.
  • Likewise, nullsLast(), the comparator places these two Employee objects after the other Employee objects in the list.

Java 8 Comparator’s sort order reversal method – reversed()
In case you simply want to sort in the order opposite to a defined comparator’s sorting order then you need not write the reverse comparison logic again. Instead simply use the Comparator.reversed() default method.

The reversed() method has the signature –

default Comparator<T> reversed()
Java 8 Comparator order reversal using reversed() method
public static void main(String[] args) {
  Comparator<Employee> empNameComparator = Comparator.comparing(Employee::getName).reversed();
  Collections.sort(employeeList, empNameComparator);
  employeeList.forEach(System.out::println);
}
 OUTPUT of the above code
Employee Name:Tom Jones Age:45
Employee Name:Nancy Smith Age:15
Employee Name:Harry Major Age:35
Employee Name:Harry Major Age:25
Employee Name:Ethan Hardy Age:65
Employee Name:Deborah Sprightly Age:29
Quick Explanation of the above code-

  • Comparator’s default method reversed() is applied to the empNameComparator which has been initially defined to sort in alphabetical order of Employee names.
  • Due to reversed() being applied to it, the Employee objects are printed in reverse alphabetical order of their names.

Summary– In this tutorial on Java 8 Comparators we first had a look at how Comparators were used till Java 7. Next we learnt that Comparator is now a functional interface and how it can be defined using a lambda expression. We then had a look at the comparing() method which allows us to define a Comparator using a Function instance which returns the sort key. Next we looked at thenComparing() method which allows us to sort using multiple sort orders. This was followed by understanding the natural order comparison related methods of Comparator – naturalOrder() and reverseOrder(). Next we looked at the null-valued sort key handling methods – nullsFirst() and nullsLast(). Lastly, we understood the reversed() method which allows us to sort in the reverse order of a defined Comparator.

 

Digiprove sealCopyright © 2014-2016 JavaBrahman.com, all rights reserved.