Comparable and Comparator interface in java

Comparable and Comparator:

Comparable and Comparator both are interfaces in Java used to sort elements in collection. Difference between comparable and comparator is one of the most popular java interview questions. Let us see each interface in detail and the difference between them.

Comparable:

Comparable is an interface present in java.lang package. This interface imposes a natural ordering on the objects of each class that implements it. Natural ordering of elements simply means the ascending order of its ASCII value (ordering provided by the JVM). For objects, it is the order defined by its compareTo() method. Note that string objects will be ordered alphabetically since the String class implements the Comparable interface and defines the compareTo() method to compare two strings alphabetically.
Lists and arrays of objects that implement this interface can be sorted automatically by Collections.sort() and Arrays.sort() respectively. Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.
Let us see an example program.

Employee.java

ComparableExample.java


Output:

Here, I have defined an order based on the employee name i.e. to sort employee objects based on the employee’s name. We can also define to sort based on other parameters like id or date of birth, based on the requirement.

Comparator:

Comparator is an interface present in java.util package. This interface imposes a total ordering on some collection of objects (not necessarily the one that implements). A comparator is capable of comparing two objects of different class’s instance. The comparator is mostly used when the objects needs to be sorted based on more than one parameter.

Let us see an example program.
Employee.java

Now we define a comparator to sort employee objects based on name. Employees with same name are then sorted by employee id.

NameComparator.java


ComparatorExample.java

Output:

After seeing the above examples, it may appear like both comparable and comparator does the same. Even for the second example we could have used comparable interface to sort employees based on name and Id. But this is a simple example of how to use the interfaces. There will be some other cases where we cannot use the comparable interface to sort the objects and in such cases we can use comparator interface.

When to use Comparable and Comparator?

Comparable should be used when you want to sort objects of the implementing class itself. This means that you want to define a comparator for an already existing class and you have the source code to modify the class to implement the comparable interface. On the other hand, consider a scenario where you don’t have the source code and you want to define a comparator for a class in a client provided library (i.e. you are not the original author). In this case you cannot modify the source code to implement comparable interface. Here the only way is to define a custom comparator class which implements the Comparator interface.

Difference between Comparable and Comparator interface:

ComparableComparator
Comparable interface is for self-comparing i.e. the class which implements comparable interface compares one of its objects with another. Class whose objects to be sorted must implement this interface.Comparator is more flexible. Class whose objects to be sorted do not need to implement this interface. Some other class can implement this interface and used to compare two objects of another class.
Comparison of null arguments not allowedUnlike Comparable, a comparator may optionally permit comparison of null arguments, while maintaining the requirements for an equivalence relation.
Present in java.lang packagePresent in java.util package which very much says that Comparator should be used as a utility to sort objects which Comparable should be provided by default.
Provides the method,

compareTo(T ob)

Compares this object with the specified object for order.

Provides the method,

compare(T o1, T o2)

Compares its two arguments for order.

Comparable is preferable if you want to sort objects based on natural order.Comparator has a distinct advantage of being self-descriptive for example if you are writing Comparator to compare two Employees based on their name then the comparator can be named as NameComparator

 

Hope you understood what is Comparable and Comparator interface in Java and when to use it. If you have any comments, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.
3 comments
  1. It’s a nice article. As a developer this is very helpful to me & great learning about comparable interface in java . Thanks for sharing such informative post. Sharing some additional knowledge on java interface.

Add Comment

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