Difference between Comparable and Comperator – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Tue, 04 Jul 2023 17:39:51 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Difference between Comparable and Comperator – Programmerbay https://programmerbay.com 32 32 Difference between Comparable and Comperator in Java in Tabular Form https://programmerbay.com/difference-between-comparable-and-comperator-in-java-in-tabular-form/ https://programmerbay.com/difference-between-comparable-and-comperator-in-java-in-tabular-form/#respond Tue, 04 Jul 2023 17:39:51 +0000 https://programmerbay.com/?p=9279 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 comparable Using 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

]]>
https://programmerbay.com/difference-between-comparable-and-comperator-in-java-in-tabular-form/feed/ 0