Comparable and Comparator Interfaces
Both interfaces are used to order the objects of the user-defined class.
Compmarable interface is found in java.lang package and contains only one method named compareTo(Object).
It provides a single sorting sequence only, i.e., you can sort the elements on the basis of single data member only. For example, it may be name, age or anything else.
compareTo(Object obj) method
public int compareTo(Object obj): It is used to compare the current object with the specified object. It returns
positive integer, if the current object is greater than the specified object.
negative integer, if the current object is less than the specified object.
zero, if the current object is equal to the specified object.
We need to implements Comparable interface first and override compareTo method.
Unlike Comparable, Comparator is external to the element type we are comparing. It’s a separate class. We create multiple separate classes (that implement Comparator) to compare by different members. Collections class has a second sort() method and it takes Comparator which could be found in java.util package. The sort() method invokes the compare() to sort objects. To compare Student by age, we need to do 3 things :
1) Create a class that implements Comparator (and thus the compare() method that does the work previously done by compareTo()).
2) Make an instance of the Comparator class.
3) Call the overloaded sort() method, giving it both the list and the instance of the class that implements Comparator.
Or you may call sort() method directly on Collection that you want to sort and give it Comparator as an argument.
Key differences between Comparable vs Comparator in Java.
|It provides single sorting sequences.||It provides multiple sorting sequences.|
|It affects the original class. i.e., actual class is altered.||It doesn’t affect the original class, i.e., actual class is not altered.|
|This method can sort the data according to the natural sorting order.||This method sorts the data according to the customized sorting order.|
|The class whose objects you want to sort must implement comparable interface.||Class, whose objects you want to sort, do not need to implement a comparator interface.|
|The logic of sorting must be in the same class whose object you are going to sort.||The logic of sorting should be in separate class to write different sorting based on different attributes of objects.|
|Comparable interface is present in java.lang package.||Comparator interface is present in java.util package.|
|Comparable provides compareTo() method to sort elements in Java.||Comparator provides compare() method to sort elements in Java.|
|Implemented frequently in the API by: Calendar, Wrapper classes, Date, and String.||It is implemented to sort instances of third-party classes.|
|All wrapper classes and String class implement comparable interface.||The only implemented classes of Comparator are Collator and RuleBasedCollator.|