Difference between Comparable and Comperator in Java in Tabular Form

In Java, there are two interfaces that provides a way to sort and compare objects, namely Comparable and Comparator. Both interfaces play significant roles in sorting and ordering elements, but they differ in when it comes to identify ordering of elements.

The classes that implements Comparable interface support default natural sorting, if one needs to implement a customized sorting order then Comperator comes into picture. Further, the classes which doesn’t implement Comparable interface and required a need to support for sorting, then Comparator can also be used in such case.

For user-defined classes, Comparator can be used for sorting objects based on some values as a base for determining order of elements.

Point to Remember:

1. Comparable is for default natural sorting, on other hand, Comparator is for custom sorting

2. Comparable resides in java.lang package, whereas, Comparator comes under java.util package

3. Comparable interface consists only single method, named compareTo(), while Comparator contains 2 method, named equals() and compare()
4. All wrapper classess & String implements Comparable interface, whereas, Collator or RuleBaseCollator are two classes that directly implement Comparator .

Comparable 

Comparable is an interface that empowers natural sorting of elements. A class that implements the Comparable interface defines compareTo() method, which takes class type object and returns an integer indicating the relative order of the elements.

Comparator

Comparator, on the other hand, is an interface that empowers custom sorting of elements. Unlike Comparable, which only supports natural order of elements. A  Comparator can be defined and used independently with class. It can also be passed as a parameter to a Collections.sort() method or used to sort elements stored in a collection that implements the SortedSet or SortedMap interfaces.

Difference between Comparable and Comperator in Java In Tabular Form

BasisComparableComparator
DefinitionIt defines the natural sorting order of elements in a classIt provides a way to define a custom sorting order for elements in a class
MethodIt provides compareTo() method that is required to implemented by a classIt provides compare() method that is required to implemented by a class
Object TypeCan only be used with objects of the same type and comparableUsing Comparator, we can use non comparable objects in a class
Sorting OrderSorting order is determined by the class's implementation of the
compareTo() method
Sorting order is determined by the implementation of the compare() method in the Comparator implementation
Collections.sort()Collections.sort(list) can be used to sort a collection of objects that implement ComparableCollections.sort(list,comparatorObj) can be used to sort a collection of objects by passing a Comparator object as a parameter
OrderingOnly one ordering can be defined for a classMultiple orderings can be defined for a class by creating multiple Comparator objects
PackageIt resides in java.lang packageIt resides in java.util package

Leave a Reply