Comparable and Comparator both are the generic interfaces in Java used to compare the data elements of the objects. The Comparable interface is present in the java.lang package and the Comparator interface is present in the java.util package.
The basic difference between the Comparable and Comparator interfaces is that the Comparable interface provides the single sorting sequence whereas, the Comparator interface provides the multiple sorting sequences. There are some other differences between Comparable and Comparator interface which we will study in the Comparison Chart.
Content: Comparable Vs Comparator
Comparison Chart
Basis for Comparison | Comparable | Comparator |
---|---|---|
Basic | The Comparable interface allows only single sorting sequence. | The Comparator interface allows multiple Sorting sequences. |
Packages | The Comparable interface is present in the java.lang package. | The Comparator interface is present in the java.util package. |
Methods | The Comparable interface contains only single method public int compareTo(Object obj); | The Comparator interface contains two methods public int compare(Object obj1, Object obj2) boolean equals(Object obj) |
Implementation | Comparable interface is implemented by the class whose objects are to be compared. | Comparator interface is implemented by a sperate class instead to the class whose objects are to be compared. |
Comparison | The compareTo(Object obj) method compares the object which is used to invoke the method with the specified object passes to the method. | The compare(Object obj1, Object obj2) method compare the both the specified objects that are passed to the method. |
List/Array | When a list of the object of Comparable type has to be compared the Collection class provides a method i.e. Collections.sort(List lst). | When a list of objects of Comparable type has to be compared the Collection class provides a method i.e. Collections.sort(List, Comparator). |
Definition of Comparable
Comparable is an interface that is available in the java.lang package. A class implements the Comparator interface, to sort its object in natural order. The objects are sorted in natural order means that the objects are compared by their ASCII values. The classes that implement the Comparable interface are Byte, Character, Double, Float, Long, Short, String, and Integer classes. Even the Date and Calander class also implements the Comparable interface.
The Comparable interface contains only one method that is CompareTo(Object obj). This method compares the object used to invoke the method with the object specified in the parameter.The syntax of the method is as follow:
public int compareTo(Object obj);
The CompareTo(Object obj) method return 0, when both the object compared by the method contains the same value, it returns -ve value if the invoking object is smaller than then the specified object and returns +ve value if the invoking object has greater value as compared to the specified object.The Collections class provides a sort method for sorting the elements of the list. The list (and array) elements of Comparable type can be sort using the method “Collections.sort(List lst)”.
Definition of Comparator
Comparator is an interface that is available in java.util package. The interface Comparator is not implemented on the class whose objects are to be compared instead separate class implements the Comparator interface so that the sorting logic is applied to each data element of the object in a different class. Comparator contains two methods as follows:
public int compare(Object obj1, Object obj2) and boolean equals(Object obj)
The compare( ) method above compares the first object obj1, with the second object obj2. The compare( )method return 0 when both the object compared by the method contains the same value, it returns -ve value if the object obj1 is smaller than then the object obj2 and returns +ve value if the object obj1 has greater value as compared to the object obj2.
The equals( ) methods checks if specified object is equal to the invoking object. The equals( ) method return true if the both the compared objects are equal else it returns false. The Collections class provides the method for sorting the elements of list and comparator type. The list elements of comparator types are sorted b y the method Collections.sort(List, Comparator).
Key Differences Between Comparable and Comparator
- The comparable interface allows single sorting sequence that means you can compare only single data element of the object in compareTo( ) method on the other hand Comparator interface allows multiple sorting sequences that mean you can compare multiple data elements of the object.
- The Comparable interface is implemented by the class whose objects are to be compared because the sorting logic is defined inside the same class. On the other hand, the Comparator interface is not implemented by the class whose objects are to be compared because the sorting logic is defined in the separate classes where each class defines sorting on single data element of the object and these defining classes implements the Comparator interface.
- The Comparable interface lies inside java.lang package whereas, the Comparator interface lies inside the java.util package.
- The Comparable interface declares only one method that is compareTo(Object obj) whereas, the Comparator interface declares two methods that are, compare(Object obj1, Object obj2) and equals(Object obj).
- The compareTo(Object obj) method in Comparable compares method invoking object with the specified object passed to the method whereas, the compare(Object obj1, Object obj2) method of Comparator compares the objects obj1 with obj2 that are passed to the method.
- The Collections class provides a sorting method “Collections.sort(List lst)” to sort the objects of the Comparable type. The Collections class provides sorting method Collections.sort(List, Comparator)to sort the objects of Comparator type.
Conclusion
If you want to sort the objects in the natural ordering, then you can use the comparable interface else you want to sort the objects based on any attribute then Comparator interface is used.
Leave a Reply