Comparable Vs Comparator in Java

Comparable and Comparator in Java are two interfaces used to sort a collection of objects. The obvious question is why two interfaces when they both do the same task of sorting the objects. In this post we’ll see what are Comparable and Comparator interfaces in Java, why both are required and differences between Comparable and Comparator in Java.

Comparable interface in Java

Comparable interface in Java has a single method compareTo().

int compareTo(T o)– This method compares the object used to call the method with the object passed as parameter for ordering.

The objects of the class implementing this interface are ordered as per the implementation of the compareTo() method. Since the class itself is implementing the interface so the ordering as provided by Comparable interface is referred to as the class’s natural ordering.

Lists (and arrays) storing objects of class that implements this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map i.e TreeMap or as elements in a sorted set i.e. TreeSet, without the need to specify a Comparator.

Comparator interface in Java

Comparator interface in Java has a method compare().

int compare(T o1, T o2)– Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Comparator interface is not part of the class which is sorted using the ordering provided by Comparator interface implementation. Comparator interface is implemented as a separate class or as an anonymous class.

Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order.

Comparable and Comparator Java example

Let us see an example to see how sort ordering is done using Comparable and Comparator and why both of them are needed.

There is a Person class with fields firstName, lastName and age. You want to sort Person class objects using the firstName + lastName so the class implements Comparable interface and provide logic for that ordering by overriding the compareTo() method.
Person.java

Class to create Person class objects and do the sorting by calling the Collections.sort() method. Since the objects stored in the List are already implementing Comparable interface so the objects will be sorted by using that implementation.

Output

So far you have your class, class’ natural ordering is also defined by implementing the Comparable interface.

Now suppose you have a new requirement to sort the Person class by age or by last name + first name. Person class is already coupled with the Comparable interface and the ordering imposed by that interface. In this sort of scenario you can use Comparator interface, since Comparator can be implemented by another class so you can create different Comparator classes for different sort ordering. Then you can use the Collections.sort() method where Comparator is also passed to sort the objects.

As you can see two new Comparator classes are added one that sorts by age and another sorts by last name. Person class still has its Comparable interface implementation defining its natural ordering.

Output

As you can see now if you want to sort by age field then you can pass the Comparator that sorts by age in the Collections.sort() method. Same way if you want to sort by lastName field then you can pass the Comparator that sorts by lastName in the Collections.sort() method. If you want to sort by firstName you still have that implementation provided by the implementation of the Comparable interface.

Difference between Comparable and Comparator in Java

Comparable Comparator
Comparable interface has compareTo() method for providing sorting logic. Comparator interface has compare() method for providing sorting logic.
Comparable interface is in java.lang package. Comparator interface is in java.util package.
Comparable interface is implemented by the class whose objects are to be sorted. Comparator interface is implemented by some other class or as an anonymous class.
Since the class whose objects are to be sorted implements the Comparable interface so Comparable can provide only one way of sorting the objects. There can be many Comparator classes providing logic for different sort ordering.
A List or array having objects that implement the Comparable interface can be sorted by just passing the List in Collections.sort() or by using Arrays.sort() for array. In case of Comparator, instance of Comparator has to be passed with the Collections.sort() method.

That’s all for the topic Comparable Vs Comparator in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.