Difference between Comparable and Comparator in java

In this post, we will see the difference between Comparable and Comparator in Java. First, we will see Comparable vs Comparator in brief later we will see each point in details.

Difference between Comparable and Comparator in java

Difference between Comparable and Comparator in java

ComprableComparator
Comparable interface
provide single
way of
sorting, means we can sort
at a time either name or id.
Comparable interface can be used to provide multiple way of sorting, means we can sort at a time name as well as id.
Comparable interface is available in java.lang package.Comparator interface is available in java.util package.
Comparable has only one method i.e compareTo() and this is not a functional interface.Comparator is a functional interface and apart from compare() method, it has been introduced new methods in jdk1.8.
While sorting using Comparable actual class is modified.In case of Comparator actual class is not modified.
In case of Comparable we need to override comapreTo(Object o) method.In case of Comparator we need to override compare(Object o1, Object o2) method.
In case of comparable we need to use Collections.sort(Collection c). In case of Comparator we need to use Collections.sort(Collection c, Comparator compr)

 

Example of sorting a list of Employee using Comparable –

Output is –

ram 101
gita 102
mohan 103
rohan 105
sita 106

Difference between Comparable and Comparator in java

If we want to sort the employee list basis of the name we need to modify compareTo() logic something like below.

 

 

Example of sorting a list of Employee using Comparator –

 

Output is –

sorting using comparator by name ——
3 gopi
2 krishna
5 mohan
1 radhika
sorting using comparator by id ——–
1 radhika
2 krishna
3 gopi
5 mohan

Difference between Comparable and Comparator in java

In case of Comparable we need to override comapreTo(Object o) method, In case of Comparator we need to override compare(Object o1, Object o2) method.

In the Comparable interface, we have compareTo() method and we need to while sorting using Comparable. In the Comparator interface we have compare() method and we need to override it.

Declaration of compareTo() in Comparable interface –

Declaration of compare() in Comparator interface –

Difference between Comparable and Comparator in java

In the case of Comparable we need to use Collections.sort(Collection c), In case of Comparator we need to use Collections.sort(Collection c, Comparator compr).

Collections.Sort() method has two overloaded versions, the first one accept a list as an argument and the second one accepts list and Comparator as an argument.

 

This is all about the difference between Comparable and Comparator in java with example in Java.

While sorting using Comparable actual class is modified, In case of Comparator actual class is not modified.

What does it mean when we say in case of the Comparable actual class is modified but in case of Comparator it is not? Did you notice in the above examples, in the case of Comparable our Employee class is implementing this interface, but in the case of Comparator Employee class is not implementing Comparator interface? Suppose we have Employee class as third party library i.e as a jar file. Can we sort a list of Employee using Comparable? The answer is no, we can’t. Employee class needs to implement the Comparable interface, which is a little tricky because we have a jar file (means.class). Here we can go for Comparator. Hope this is clear when we need to go for Comparator.

That’s all about Difference between Comparable and Comparator in java.

You may like.

Difference between Comparable and Comparator in java

Comparable docs.

Comparator docs.

Top