Using the Comparator class in Java 8 to compare objects

Java 8 comes with a range of built-in implementations of the Comparator interface.

Consider the following Employee class:

public class Employee
    private UUID id;
    private String name;
    private int age;

    public Employee(UUID id, String name, int age)
    { = id; = name;
        this.age = age;
    public UUID getId()
        return id;

    public void setId(UUID id)
    { = id;

    public String getName()
        return name;

    public void setName(String name)
    { = name;
    public int getAge()
        return age;

    public void setAge(int age)
        this.age = age;

…and the following list of employees:

List<Employee> employees = new ArrayList<>();
employees.add(new Employee(UUID.randomUUID(), "Elvis", 50));
employees.add(new Employee(UUID.randomUUID(), "Marylin", 18));
employees.add(new Employee(UUID.randomUUID(), "Freddie", 25));
employees.add(new Employee(UUID.randomUUID(), "Mario", 43));
employees.add(new Employee(UUID.randomUUID(), "John", 35));
employees.add(new Employee(UUID.randomUUID(), "Julia", 55));        
employees.add(new Employee(UUID.randomUUID(), "Lotta", 52));
employees.add(new Employee(UUID.randomUUID(), "Eva", 42));
employees.add(new Employee(UUID.randomUUID(), "Anna", 20));   

Here’s how to build an object that implements the Comparator interface using the default comparing method of the Comparator object. The comparator compares the Employee objects based on their names:

Comparator<Employee> employeeNameComparator = Comparator.comparing(p -> p.getName());

There’s nothing stopping you from specifying another property, such as the age as the basis for the comparison. Here’s an example using the new operator ‘::’:

Comparator<Employee> employeeAgeComparator = Comparator.comparing(Employee::getAge);

Also, it’s equally easy to chain comparisons using the thenComparing static method:

Comparator<Employee> compositeComparator = Comparator.comparing(Employee::getName)

What if you’d like to reverse the order? Here you are:

Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getAge).reversed();

And what about null values? How can you indicate the way null values should be handled? You can easily determine whether null values should be ordered first or last:

Comparator<Employee> employeeAgeComparator = Comparator.nullsFirst(Comparator.comparing(Employee::getAge));

…where nullsFirst has a “sister” method called nullsLast. You’ll probably guess what those methods imply.

The Comparator interface also provides a naturalOrder() method that provides a Comparator that compares the objects based on their natural ordering. It’s best applied to objects that implement the Comparable interface. It can be readily used on primitive types and strings since they can be ordered in a natural way, such as in a numeric or alphabetical order:

List<Integer> ints = Arrays.asList(1, 4, 5, 3, 6 ,3, 6);
Comparator<Integer> intComparator = Comparator.naturalOrder();

The reversedOrder default method is the exact opposite of naturalOrder:

List<Integer> ints = Arrays.asList(1,4,5,3,6,3,6);
Comparator<Integer> intComparator = Comparator.reverseOrder();

OK, great, but how can I use a Comparator? All types that implement the List interface has a new method called “sort” which accepts a comparator. It doesn’t return anything, but acts directly on the List object which called upon it:


View all posts related to Java here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Using the Comparator class in Java 8 to compare objects

  1. Lashmi Sethuraman says:

    Very useful explanation. Thanks. Helped me to get a clear understanding of the usage of Comparator in Java 8.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: