Both Comparable and Comparator interfaces in java are used in sorting of objects. Some of the main differences between them are outlined below:
1. Comparable interface lies in java.lang package while Comparator interface lies in java.util package.
2. Comparable interface use compareTo() method to compare objects while Comparator interface use compare() method.
Signature of compareTo() is:
public int compareTo(T obj)
Signature of compare() is :
Unlike compareTo(), compare() takes two arguments. These arguments are the two objects which are being compared.
3. Comparable in Java is used to implement natural ordering of objects while Comparator can be used to implement any user defined ordering.
4. If you have a List (or an array) with each of its elements implementing Comparable interface and you pass this List( or array) to Collection.sort() (or Arrays.sort()), then they will be automatically sorted out according to their natural order.
If their elements do not implement Comparable then you have to pass a Comparator object to
Collections.sort() (or Arrays.sort()) in order to sort the elements like:
Or
5. In order to use Comparable you have to change the class whose instances you are going to compare. For example, if you are going to compare instances of Accounts class then you have to change it in order to implement Comparable interface.
But Comparator does not force you to change the class. You can create separate comparator class as:
and then pass it to the funtions like Arrays.sort() or Collections.sort() as shown in point 4.
Here you do not need to change your Account class.
6. Comparable contract specifies that if null is passed as argument to compareTo(), then it should throw NullPointerException.
Obj1.compareTo(null) // should throw NullPointerException
While contract of Comparator, upto Java 6, does not say anything about null arguments passed to compare(). It also does not specify that compare() should throw NullPointerException.
Its upto you how to handle null arguments. Here you can throw NullPointerException or, unlike Comparable, allow the comparison.
Note that contract of Comparator in Java7 explicitly says that :
Unlike Comparable, a comparator may optionally permit comparison of null arguments, while maintaining the requirements for an equivalence relation.
And
compare() should throw:
NullPointerException - if an argument is null and this comparator does not permit null arguments
7. Comparable should be used to compare elements according to their natural order and Comparator can be used for any other user defined order.
8. Objects which implement Comparable can be used as keys in a SortedMap ( like TreeMap) and as elements in a SortedSet (like TreeSet) without explicitly passing Comparator to their constructors.
9. Comparable can be used when you have only one sorting criteria. For example If you want to sort instances of a PhoneNumber class according to its owners last name only.
But If you have multiple sorting criteria you would need Comparator. For example If you want to sort phone numbers according to area or owners first name also in addition to last name.
1. Comparable interface lies in java.lang package while Comparator interface lies in java.util package.
2. Comparable interface use compareTo() method to compare objects while Comparator interface use compare() method.
Signature of compareTo() is:
public int compareTo(T obj)
compareTo() takes only one argument and compare it with the object on which compareTo()is called i.e object referenced by 'this' keyword.
Signature of compare() is :
public int compare(T obj1, T obj2)
Unlike compareTo(), compare() takes two arguments. These arguments are the two objects which are being compared.
3. Comparable in Java is used to implement natural ordering of objects while Comparator can be used to implement any user defined ordering.
4. If you have a List (or an array) with each of its elements implementing Comparable interface and you pass this List( or array) to Collection.sort() (or Arrays.sort()), then they will be automatically sorted out according to their natural order.
If their elements do not implement Comparable then you have to pass a Comparator object to
Collections.sort() (or Arrays.sort()) in order to sort the elements like:
Collections.sort (listObject, comparatorObject);
Or
Arrays.sort (array, comparatorObject);
public class Account implements Comparable {
…
@Override
public int compareTo(Account a1){
…
}
}
But Comparator does not force you to change the class. You can create separate comparator class as:
public class AccountComparator implements Comparator{
…
@Override
public int compare(Account a1, Account a2){
…
}
}
and then pass it to the funtions like Arrays.sort() or Collections.sort() as shown in point 4.
Here you do not need to change your Account class.
6. Comparable contract specifies that if null is passed as argument to compareTo(), then it should throw NullPointerException.
Obj1.compareTo(null) // should throw NullPointerException
While contract of Comparator, upto Java 6, does not say anything about null arguments passed to compare(). It also does not specify that compare() should throw NullPointerException.
Its upto you how to handle null arguments. Here you can throw NullPointerException or, unlike Comparable, allow the comparison.
Note that contract of Comparator in Java7 explicitly says that :
Unlike Comparable, a comparator may optionally permit comparison of null arguments, while maintaining the requirements for an equivalence relation.
And
compare() should throw:
NullPointerException - if an argument is null and this comparator does not permit null arguments
7. Comparable should be used to compare elements according to their natural order and Comparator can be used for any other user defined order.
8. Objects which implement Comparable can be used as keys in a SortedMap ( like TreeMap) and as elements in a SortedSet (like TreeSet) without explicitly passing Comparator to their constructors.
9. Comparable can be used when you have only one sorting criteria. For example If you want to sort instances of a PhoneNumber class according to its owners last name only.
But If you have multiple sorting criteria you would need Comparator. For example If you want to sort phone numbers according to area or owners first name also in addition to last name.
I would like to know your comments and if you liked the article then please share it on social networking buttons.
For tabular format of the differences between Comparable and Comparator interfaces, see my blog post.
ReplyDeleteAwesome Post.
ReplyDeleteThumbs up.
Thanks a lot for the article, nice job.
ReplyDeletenice...
ReplyDeleteThanks nice article..here another one blog also explained good please go through this blog http://adnjavainterview.blogspot.in/2014/06/difference-between-comparable.html
ReplyDeleteNice Explantion.
ReplyDeletenice one
ReplyDeleteIt’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 a java interface
ReplyDeletegreat explanation!
ReplyDeletegreat job
ReplyDeleteThanks for this awesome article on Comparable and Comparator Interface in Java .
ReplyDelete