Comparable vs Comparator: Difference and Comparison

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

  1. Comparable is an interface for the natural ordering of objects; Comparator is an external utility for comparing objects.
  2. Implementing Comparable requires modifying the class of the objects being compared; a Comparator can be used without modifying the original class.
  3. 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 vs Comparator

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.

/10

IT Quiz

Test your knowledge about topics related to technology

1 / 10

WWW Stands for

2 / 10

Which mobile company first introduced Emoji internationally on their mobile devices

3 / 10

Which two websites offer free e-mail services?

4 / 10

Which of the following semiconductor is mostly used to construct electronic circuits?

5 / 10

Firewall in computer is used for

6 / 10

LED stands for:

7 / 10

Which is an Input device

8 / 10

The app or software, or website asks about access of your location, camera, storage, contacts etc., are known as

9 / 10

The core idea of develop AI is bulding machines and alogrithms to

10 / 10

Who is considered as the father of computing

Your score is

0%

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 ComparisonComparableComparator
Sorting typeIt provides a single sorting sequence.It provides multiple sorting sequences.
Class modificationIt modifies the actual class.It does not modify the actual class.
Package locationIt is present in the java—lang package.It is present in java. util package.
MethodsIt has the compareTo() method.It has the compare() method.
Sorting methodsCollections.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.

comparable

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.

comparator

Main Differences Between Comparable and Comparator

  1. 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.
  2. 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.
  3. A comparable interface is present in the lang package of Java, whereas the comparator interface is present in the util package of Java.
  4. 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.
  5. 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.
Difference Between X and Y 2023 05 06T084306.029
References
  1. https://dom-pubs.onlinelibrary.wiley.com/doi/abs/10.1111/j.1463-1326.2011.01545.x
  2. https://journals.asm.org/doi/abs/10.1128/aac.47.6.1824-1831.2003
  3. https://ieeexplore.ieee.org/abstract/document/6042862/

One request?

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 ♥️

Want to save this article for later? Click the heart in the bottom right corner to save to your own articles box!

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.