• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Comparable and Comparator in Java

Comparable and Comparator both are the generic interfaces in Java used to compare the data elements of the objects. The Comparable interface is present in the java.lang package and the Comparator interface is present in the java.util package.

The basic difference between the Comparable and Comparator interfaces is that the Comparable interface provides the single sorting sequence whereas, the  Comparator interface provides the multiple sorting sequences. There are some other differences between Comparable and  Comparator interface which we will study in the Comparison Chart.

Content: Comparable Vs Comparator

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonComparableComparator
Basic The Comparable interface allows only single sorting sequence.The Comparator interface allows multiple Sorting sequences.
Packages The Comparable interface is present in the java.lang package.The Comparator interface is present in the java.util package.
Methods The Comparable interface contains only single method
public int compareTo(Object obj);
The Comparator interface contains two methods
public int compare(Object obj1, Object obj2)
boolean equals(Object obj)
ImplementationComparable interface is implemented by the class whose objects are to be compared.Comparator interface is implemented by a sperate class instead to the class whose objects are to be compared.
Comparison The compareTo(Object obj) method compares the object which is used to invoke the method with the specified object passes to the method.The compare(Object obj1, Object obj2) method compare the both the specified objects that are passed to the method.
List/ArrayWhen a list of the object of Comparable type has to be compared the Collection class provides a method i.e. Collections.sort(List lst).When a list of objects of Comparable type has to be compared the Collection class provides a method i.e.
Collections.sort(List, Comparator).

Definition of Comparable

Comparable is an interface that is available in the java.lang package. A class implements the Comparator interface, to sort its object in natural order. The objects are sorted in natural order means that the objects are compared by their ASCII values. The classes that implement the Comparable interface are Byte, Character, Double, Float, Long, Short, String, and Integer classes. Even the Date and Calander class also implements the Comparable interface.

The Comparable interface contains only one method that is CompareTo(Object obj). This method compares the object used to invoke the method with the object specified in the parameter.The syntax of the method is as follow:

public int compareTo(Object obj);

The CompareTo(Object obj) method return 0, when both the object compared by the method contains the same value, it returns -ve value if the invoking object is smaller than then the specified object and returns +ve value if the invoking object has greater value as compared to the specified object.The Collections class provides a sort method for sorting the elements of the list. The list (and array) elements of Comparable type can be sort using the method “Collections.sort(List lst)”.

Definition of Comparator

Comparator is an interface that is available in java.util package. The interface Comparator is not implemented on the class whose objects are to be compared instead separate class implements the Comparator interface so that the sorting logic is applied to each data element of the object in a different class. Comparator contains  two methods as follows:

public int compare(Object obj1, Object obj2)

and

boolean equals(Object obj)

The compare( ) method above compares the first object obj1, with the second object obj2. The compare( )method return 0 when both the object compared by the method contains the same value, it returns -ve value if the object obj1 is smaller than then the object obj2 and returns +ve value if the object obj1 has greater value as compared to the object obj2.

The equals( ) methods checks if specified object is equal to the invoking object. The equals( ) method return true if the both the compared objects are equal else it returns false. The Collections class provides the method for sorting the elements of list and comparator type. The list elements of comparator types are sorted b y the method Collections.sort(List, Comparator).

Key Differences Between Comparable and Comparator

  1. The comparable interface allows single sorting sequence that means you can compare only single data element of the object in compareTo( ) method on the other hand Comparator interface allows multiple sorting sequences that mean you can compare multiple data elements of the object.
  2. The  Comparable interface is implemented by the class whose objects are to be compared because the sorting logic is defined inside the same class. On the other hand, the Comparator interface is not implemented by the class whose objects are to be compared because the sorting logic is defined in the separate classes where each class defines sorting on single data element of the object and these defining classes implements the Comparator interface.
  3. The Comparable interface lies inside java.lang package whereas, the Comparator interface lies inside the java.util package.
  4. The Comparable interface declares only one method that is compareTo(Object obj) whereas, the Comparator interface declares two methods that are, compare(Object obj1, Object obj2) and equals(Object obj).
  5. The compareTo(Object obj) method in Comparable compares method invoking object with the specified object passed to the method whereas, the compare(Object obj1, Object obj2) method of Comparator compares the objects obj1 with obj2 that are passed to the method.
  6. The Collections class provides a sorting method “Collections.sort(List lst)” to sort the objects of the Comparable type. The Collections class provides sorting method Collections.sort(List, Comparator)to sort the objects of Comparator type.

Conclusion

If you want to sort the objects in the natural ordering, then you can use the comparable interface else you want to sort the objects based on any attribute then Comparator interface is used.

Related Differences:

  1. Difference Between Packages and Interfaces in Java
  2. Difference Between Quick Sort and Merge Sort
  3. Difference Between Insertion Sort and Selection Sort
  4. Difference Between Bubble Sort and Selection Sort
  5. Difference Between Friend Function and Friend Class

Leave a Reply Cancel reply

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

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy