Difference Between Comparable and Comparator

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.

/10

IT Quiz

Test your knowledge about topics related to technology

1 / 10

Which of the following AI domain attempts to extract information from spoken and written words using algorithms?

2 / 10

Phones that offer advanced features not typically found in cellular phones, and are called

3 / 10

Which of the following is not an electronic device?

4 / 10

Android is -

5 / 10

How many numbers of home pages a web site can contain

6 / 10

'.BAK' extension usually refers to what kind of file?

7 / 10

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

8 / 10

Which web browser is developed by the Google

9 / 10

What is the radix of the octal number system?

10 / 10

Which mobile company first introduced Emoji internationally on their mobile devices

Your score is

0%

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.

Comparable vs Comparator

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.

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 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.

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 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.

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

Leave a Comment

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