Comparable and comparator are both interfaces of Java, a high-level programming language used to sort collection elements. Java basically provides these two distinct interfaces to sort the objects.
There are many notable differences between both interfaces that create a line between the two on the basis of the function they perform.
Key Takeaways
- Comparable is an interface for the natural ordering of objects; Comparator is an external utility for comparing objects.
- Implementing Comparable requires modifying the class of the objects being compared; a Comparator can be used without modifying the original class.
- Comparable imposes a single, default sorting method; Comparator allows multiple, customizable sorting options.
Comparable vs Comparator
Comparable is an interface in Java that defines the compareTo method and is used to compare naturally ordered objects. It compares objects using the “this” reference. The comparator interface sorts objects by taking their attributes into consideration. It considers objects of two different classes.
Comparable is an interface that is present in the lang package of the Java language, and its purpose is to sort the collection elements using only a single attribute such as age, price, id, name, etc.
It is capable of comparing it’s ownself with any other object. An object of any type, be it a list or an array, can be sorted.
Comparator is an interface that is present in the util package of the Java language, and its purpose is to sort the collection elements using multiple attributes at once.
It is a separate class and is external to the type of element that is being compared. The collections class provides a second sorting method that accepts a comparator as the parameter.
Comparison Table
Parameters of Comparison | Comparable | Comparator |
---|---|---|
Sorting type | It provides a single sorting sequence. | It provides multiple sorting sequences. |
Class modification | It modifies the actual class. | It does not modify the actual class. |
Package location | It is present in the java—lang package. | It is present in java. util package. |
Methods | It has the compareTo() method. | It has the compare() method. |
Sorting methods | Collections.sort(List) method. | Collections.sort(List,Comparator) method. |
What is Comparable?
As discussed above, comparable is an interface of Java programming language which is present in the lang package and is used to sort collection elements by using only one attribute. The comparable interface contains only a single method that is compareTo().
The comparable object is capable of comparing with the other objects. For the class to compare the instances, it is necessary for the class to implement the Comparable interface.
A comparable interface was developed for the objects with natural ordering. In other words, this means that the object which is to be sorted must have the order in which it is to be sorted.
In logical terms, the comparable interface compares “this” reference with the object that is mentioned in the method. When any class implements the Comparable interface, the object of that class can be sorted by using the Collections. sort() or the Arrays. sort() method.
The objects will automatically be sorted in the natural order that is defined by the CompareTo method. Numbers, strings, and many other types can be compared. Strings can be compared by using alphabetical comparison.
Various built-in classes of Java implement the comparable interface. The most important thing that should be kept in mind is that the comparable interface can only be used to compare the objects of the same class.
What is Comparator?
The comparator interface of the Java programming language is present in the util package and is used to sort the collection on the basis of multiple attributes at once.
Comparator is a totally separate class. The programmers use to create multiple classes that implement the comparator interface for the purpose of comparing different objects.
To compare any collection object, the first step is to create a class implementing the comparator method, which has the compare() method in it. The following step is to make an instance of that class implementing the comparator interface.
The last step is to call the overloaded method, which is the sort() method and then assign both the list and the instance of the class that was created in the previous step.
The comparator interface compares two objects of different classes. The Comparator interface has two methods in it, which are the compare() method and the equals() method.
The compare() method takes two different objects as its parameter to compare, and the equals() method determines whether the object being passed equals the invoking comparator.
The equals() method returns a boolean value as its output. True is returned when the object passes and the invoking object is both comparators and if not, then false is returned.
The compare() method returns an integer value in its output, indicating the larger among the two passed objects.
Main Differences Between Comparable and Comparator
- The Comparable interface provides a single sorting sequence in which the collection can be sorted through a single element like the id, name, or any specific parameter, whereas the comparator interface provides a multiple sorting sequence in which the collection can be sorted through multiple elements like the id, name or any other parameter.
- The comparable interface affects the original class as it modifies the actual class, whereas, on the other hand, the comparator interface does not affect the original class in any way, as the actual class remains the same without any modifications.
- A comparable interface is present in the lang package of Java, whereas the comparator interface is present in the util package of Java.
- In the comparable interface compareTo() method is used to sort the elements, whereas in the comparator interface compare() method is used to sort the elements.
- The comparable type list elements of a collection can be sorted by using the Collections. Sort (List) method, whereas on the other hand, the comparator type list elements of a collection can be sorted by using the Collections. Sort (List, Comparator) method.