Updated March 20, 2023
Difference Between Comparable and Comparator
In the following article, we will discuss the difference between Comparable vs Comparator. We first need to understand what are they and why are they used? Comparing the two values like 2 integers, 2 float values are easy in Java programming language using simple arithmetic operators, but how can we compare the values of 2 objects like two Students, two Employees, etc. Comparing the objects of Collections in Java is a little bit difficult and can be done by simple arithmetic and comparison operators. For the comparison of Collection objects in Java, the Comparable and Comparator interfaces are used. Comparable and Comparator are the two interfaces present in 2 different Java packages to sort the collection of objects. By implementing these interfaces in the class, we can use the various methods of them.
What is Comparable?
The comparable interface has a single method compareTo() which is used to compare the value of an object with the one supplied in the method. The Comparable interface defines the natural or default ordering of the objects of a class.
On defining the compareTo() method in the class, we need to make sure that the return value can be:
- Negative: Returns negative if the object which is to be compared is less than the supplied object value.
- Positive: Returns positive if the object which is to be compared is greater than the supplied object value.
- Zero: Returns zero if the object which is compared is equal to the supplied object value.
What is Comparator?
Comparator interface is used for the scenarios when we don’t want to use the natural ordering and want the ordering/ sorting of elements according to the specific requirements. As we cannot make any changes to the compareTo() method of Comparable interface, the Comparator interface provides a method compare() which is used to sort the objects of collection.
It takes 2 arguments in the method, and the return value can be:
- Negative: Returns a negative value if the first argument’s value is less than the value of the second argument.
- Positive: Returns a positive value if the first argument’s value is greater than the value of the second argument.
- Zero: Returns a zero value if the first argument’s value is equal to the value of the second argument.
Head to Head Comparison between Comparable and Comparator (Infographics)
Below are the top 6 differences between Comparable vs Comparator:
Key Difference between Comparable and Comparator
Some of the key differences between the Comparable interface and Comparator interface are given below:
- Comparator interface provides multiple ways of sorting the objects of Collections, whereas Comparable interface provides only a single way of sorting the objects.
- The comparable interface needs to be implemented by the class if it needs to use its methods, whereas, for Comparator, nothing needs to be done in the original class.
- Basically, Comparable is used for the objects with natural ordering; for example, in an Employee class, Employees need to be sorted on the basis of their Employee_id, whereas in the Comparator interface, sorting needs to be done through a separate class.
- Only a single object is passed as an argument in a Comparable interface, whereas 2 arguments are passed in the Comparator interface, which means a Comparable interface compares this reference with the value of the object specified in the method, whereas in Comparator comparison is done with 2 different objects provided in an argument of the method.
- Comparable is provided by default as it is present in java.lang package, whereas Comparator is provided as a utility and is present in java. util package.
- There is no need to use Arrays.sort(), Collections.sort() method if the class is implementing the Comparable interface as it automatically sorts the objects of Array and List using compareTo() method, whereas it is not so in the case of Comparator interface.
- Using Comparators allows us to not to add extra code in the original class, whereas using Comparable, mandatorily, we need to add the code.
- The comparable interface allows us only a single sorting sequence, which means we can sort the collection on the basis of a single element like in an Employee table, sorting can be done on a single basis like name, employee id, date of joining, etc. whereas Comparator interface provides multiple sorting sequence which means we can sort the collections on multiple bases like name, employee id, date of joining, etc.
- If the user does not have access to the original code and needs to write only the sorting function, Comparable can not be used for those scenarios, and Comparator is the only choice for such cases.
Comparable vs Comparator Comparison Table
Let’s discuss the top comparison between Comparable vs Comparator:
|In order to sort the objects of collections, Comparable provides a compareTo() method to the programmers.||In order to sort the objects of collections, the Comparator provides compare() method to the programmers.|
|Using Comparable, we can sort the elements of collection on the basis of a single element.||Using Comparator, we can sort on the basis of multiple elements of the collections at a time.|
|A comparable interface is present in java.lang package||Comparator interface is present in java. util package|
|Sorting in Comparator interface is based on natural order, for example, roll numbers of students in a Student class.||Sorting in a Comparable interface is done through a separate class.|
|Using Comparable, the original class using it is affected, i.e.modification is done on the actual class.||Using Comparator, the original class is not affected, i.e. modification is not done on the actual class.|
|The syntax for sorting the list using Comparator is Collections.sort(List)||The syntax for sorting the list using Comparator is Collections.sort(List, Comparator)|
Collections are one of the most important and used sections while programming in Java language. So understanding its every concept deeply is very important be it sorting, searching, printing on a console, etc. The above description clearly explains what Comparable and Comparator interface is, why is it used for and the major difference between the two of them. As both are used in their specific scenarios, before using any of them in a program, the programmer must understand the situation thoroughly.
This is a guide to Comparable vs Comparator. Here we discuss the key differences with infographics and comparison table. You can also go through our other suggested articles to learn more –