Comparable and comparator are both interfaces of java which is a high-level programming language, is used to sort collection elements. Java basically provides these two distinct interfaces to sort the objects.
There are many notable differences between both the interfaces that create a line between the two on the basis of the function they perform.
Comparable vs Comparator
The difference between comparable and comparator are distinctive on various parameters like the former interface is used to sort the collection through a single element, whereas the later interface is used to sort collection through multiple elements. A certain class has to itself implement java. Lang.Comparable interface in order to be able to efficiently compare its instances, whereas the comparator interface is external to the element type that is being compared.
Want to save this article for later? Click the heart in the bottom right corner to save to your own articles box!
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.
|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 be able to compare the instances, it is a necessary condition 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 the comparable interface is implemented by any class, 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 passed, and the invoking object is both comparators, and if not, then false is returned.
The compare() method returns an integer value in its output which indicates 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.
I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️
Sandeep Bhandari holds a Bachelor of Engineering in Computers from Thapar University (2006). He has 20 years of experience in the technology field. He has a keen interest in various technical fields, including database systems, computer networks, and programming. You can read more about him on his bio page.