Java Comparator

Introduction

In the previous tutorial we learned about the Comparable interface, now we are going to understand Java’s Comparator interface, what it is, what it does and how to use it. We will also see that the general contract of Comparator is exactly same as the Comparable interface. We will also take note that the same ClassCastException and Consistency with equals need to be kept in mind while implementing compare method of Comparator interface.

Comparator

Comparator is an interface in java.util package with a single method compare which is implemented by a class other than the one whose instances are being compared.

What does it mean exactly?

Well, it means that the instances of the class implementing Comparator can compare the given objects of some class and arrange them in the order specified by the compare method. For example, Let’s say you have a Student class whose instances are to be compared for some property rollNumber. What you can do here is, create a RollNumberComparator where the compare method takes two Student instances and compares them.

Signatures

So, if you are writing a class (more precisely a Value class) with various sorting possibilities, based on the various fields/attributes/state of its instances you should implement Comparator and override compare method accordingly.

This is how the Comparator interface looks like:

 

A simple invocation of compare method would look like:

                               compare(thisObject, anotherObject);

The compare() method returns an int with the following characteristics:

  1.  negative Integer – If thisObject < anotherObject
  2.  zero – If thisObject == anotherObject
  3.  positive Integer – If thisObject > anotherObject

 

In the TreeMap example above, when you put a key-value pair in the TreeMap, the input key is inserted at its sorted place based on the compareTo implementation of String class, which enforces a natural ordering that is lexicographical. You must look at the TreeMap’s implementation of put() method.

Comparator Implementation

To further consolidate our understanding of Comparator, lets write our own comparator class and see how it compares objects with each other.

For an example we will write a Student class and the student instances can be sorted based on various criteria. At some point their teacher might want to sort them based on RollNumber to arrange them in the examination hall and at another point the student may be sorted based on the marks obtained to find the topper of the class.

Student.java

StudentRollNumberComparator.java

StudentMarksComparator.java

ComparatorDemo.java


General Contract of Comparator

The general contract is same as the General Contract of Comparable, so, you should refer to that.

 Related Posts

Recommended Reading

  • Comparator Javadoc  This tutorial is by no means complete, it just gives you a head start. You must study the Javadoc and try out your findings by writing sample code. Especially after Java 8 release, a huge lot of methods have been added and you would find them useful in your day-to-day programming assignments.