Using the Comparator class in Java 8 to compare objects
February 8, 2015 Leave a comment
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) { this.id = id; this.name = name; this.age = age; } public UUID getId() { return id; } public void setId(UUID id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.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) .thenComparing(Employee::getAge);
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:
employees.sort(employeeNameComparator);
View all posts related to Java here.