Comparable vs Comparator: Difference Between Comparable and Comparator

Comparable and Comparator: An Overview

Comparable and comparator are both interfaces in the Java programming language to determine the behaviour in which classes will be implemented. While Comparable is used on objects that are naturally ordered, the Comparator interface implements sorting by taking the attributes of an object into consideration. Further, Comparator sorting takes into account objects of two different classes and Comparable compares objects using the “this” reference. 

Now, that we have established the basic principle on which both interfaces work, let’s look at the differences between the two.

Difference Between Comparable and Comparator

Method of Sorting

Comparable

It uses the compareTo() function for sorting. This is the only method present in the Comparable sorting interface. 

compareTo(Object O) takes as argument an object and compares it to another of the same type. If an object is a string, you can only compare it to another object of the same type. The same is the case with an int object and so on. compareTo() returns a negative, positive or zero integer value based on the result of the sorting. 

Comparator

There are two methods to sort elements in Comparator: compare() and equals(). 

compare(Object O1, Object O2) takes into account two arguments as input and provides the desired output. It returns an integer to indicate how the first argument comares with the second. 

  • If O1 is less than O2, it returns a negative integer.
  • If O1 is greater than O2, it will return a positive integer.
  • If O1 is equal to O2, it returns 0.

equals(Object) takes into account an object as input and compares it to the comparator. It returns True if the Object is equal to the Comparator. It also ensures that the order does not change. 

Package Used

Comparable

It exists in the java.lang package of Java. 

Comparator

It exists in the java.util package of Java.

Ordering and Class

Comparable

  • It takes into account objects that follow natural ordering i.e. have a tendency to sort themselves. For example, alphabetical order or numerical order in case of names, price, salary, roll number, age, etc. 
  • It is also important that both objects belong to the same class.
  • Comparable interface compares “this” reference with the object specified. 
  • Sorting using Comparable affects the actual class. 

Comparator

  • This interface is used primarily to sort attributes of specified objects. They do not need to have a natural order; it can be customised. 
  • The logic of sorting is required to be in separate classes to compare the attributes of the objects of the two classes in consideration.
  • The actual class remains unaffected.

Sequences and Collections

Comparable

  • It supports single sorting sequences only. This implies that you can only consider one element or attribute of a collection such as a roll number or age or rank.
  • For sorting a collection of objects, arrays or list, you can use Collection. sort(List) or Arrays. sort(List). This will bring the objects in their natural order.

Comparator

  • It supports multiple sorting sequences. This implies that you can consider multiple elements or attributes of a collection such as a roll number, age and rank.
  • You can use a collection.sort(list, comparator) to sort a collection.

Here is a summary of all the differences  between Comparable and Comparator

Comparable Comparator
Package
Java.lang Java.util
Method of Sorting
compareTo() compare()
Ordering
Natural Ordering Custom Ordering
Type of Object
Objects in comparison must be of the same type Objects of different classes are considered
Affect on Class
Class gets modified Doesn’t affect the class
Sorting Sequence
Single Sorting Sequence Multiple Sorting Sequence

 

Things To Keep In Mind While Using Comparable And Comparator In Java

  • Comparable interface is the one to opt if you are selling to perform a standard comparison for a specific class.
  • Comparator allows the use of lambda. 
  • You can use the compareTo() method to implement both Comparator and Comparable.
  • Comparator interface is recommended if you are seeking flexibility in sorting

Conclusion

In most real-life scenarios, when we sort, we usually have a variety of parameters in mind. Also, since using comparable results in default sorting and can’t be changed dynamically, it is less practical than Comparator which allows you to customize and choose from different sorting methods to suit your requirement. For this reason alone, Comparator finds more practical applications than Comparable.

Read: Some interesting java project to check out

If you’re interested to learn more about Java, full stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

The best time to learn is now!

REGISTER & START LEARNING TODAY
LEARN MORE

Leave a comment

Your email address will not be published. Required fields are marked *

×