Comparator

Allows to define custom ordering of objects. For example, while sorting.

Definition

@FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
}

Usage

class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.getAge() - p2.getAge();   

    }
}

public class ComparatorExample {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();

        Collections.sort(people, new AgeComparator());
    }
}
Collections.sort(people, (p1, p2) -> p1.getAge() - p2.getAge());

Default methods

In addition to the abstract method compare, it also provides a bunch of utility methods to easily define a Comparator.

For example,

List<Person> sortedByAge = people.stream()
                                 .sorted(Comparator.comparing(Person::getAge))
                                 .collect(Collectors.toList());

Here's a list of all such methods:

  • static comparing(Function keyExtractor): Like the example above
  • static comparing(Function, Comparator): Like the example above but with Comparator for the given key.

Similar methods but for specific types:

  • static comparingDouble
  • static comparingInt
  • static comparingLong

Lexicographic order sorting (to be used after the above methods):

  • thenComparing
  • thenComparingDouble
  • thenComparingInt
  • thenComparingLong

Methods for null friendly Comparator

  • nullsFirst(Comparator)
  • nullsLast(Comparator)

To modify ordering or comparator:

  • static naturalOrder: returns Comparator which compares Comparable objects in natural order
  • static reverseOrder()
  • reversed()

Backlinks