• The comparable interface and comparator interface are the interfaces which are used for the Ordering Collection.
• The Comparable interface imparts natural ordering to the classes which implement it.
• The Comparator interface is used to specify the order relation. It can also be used to override the natural ordering.
• Both the interfaces, Comparable interface and Comparator interface are useful to sorting elements in a collection.
The Comparable interface:-
• The Comparable interface is a member of a java.lang package.
• A class which implements a Comparable interface having a natural ordering.
• When we declare a class, the JVM implementation has no means of determining the order you intend for an objects of that class.
• We can, by implementing the Comparable interface, provide order to the object of any class.
• We can sort collections that contain objects of classes that implement the Comparable interface.
• So, when we implement a Comparable interface in Collection we can get any type of object of class which is sorted.
• For that we have to implement the compareTo() method of Comparable interface.
• There are 14 classes in java which implement the Comparable interface, some of them are Byte, Date, Integer, Character, Double, etc.
public int compareTo(Object obj)
Explanation of comparTo() method:-
• Elements must be mutually comparable.
• In this, we have to compare the elements every time.
• If the elements are not mutually comparable then we will get the ClassCastException.
• Mutually comparable means both the elements must compare with one another.
• Suppose in a collection, we are comparing Date and Float into the TreeSet then it does not work, because, they are not mutually comparable.
• So, if we want to compare those 2 objects then those objects must be of same type.
• Now we are going to write a custom Comparable type. So, for this we need to implements the compareTo method of Comparable interface.
• And below code shows how to implement the Comparable interface.
• The Student class is implementing the Comparable interface so that the objects of this class can be compared to each other based on grade point average (GPA).
class Student implements Comparable
String firstname, lastname;
int stdId = 0;
double GPA = 0.0;
public Student(String firstname, String lastname, int stdId, double GPA)
if(firstname == null || lastname == null || stdId == 0 || GPA == 0.0)
Throw new IllegalArgumentException();
this.firstname = firstname;
this.lastname = lastname;
this.stdId = stdId;
this.GPA = GPA;
public String firstname()
public String lastname()
public int stdId()
public double GPA()
public int compareTo(Object o)
double f = GPA – ((Student)o).GPA;
if(f == 0.0)
return 0; // 0 signifies equals
else if(f < 0.0)
return -1; // negative value signifies less than or before
return 1; // positive value signifies greater than or after
• The StudentList program given below tests the Comparable interface implementation created in the earlier program.
• The StudentList program creates four Student objects and prints then. Because the Student objects are added to the TreeSet, which is a sorted set, from that objects are sorted.
public class ComparableTest
public static void main(String args)
TreeSet stdSet = new TreeSet();
Object studentArray = stdSet.toArray();
for(Object obj : studentArray)
S = (Student) obj;
System.out.println(“Name = %s %s Id = %d GPA = %.lf\n”,s.firstname(),s.lastname(),s.stdId(),s.GPA());
Name = Rima Timothy Id = 104 GPA = 2.3
Name =James Jerry Id = 102 GPA = 2.8
Name = Jim Max Id = 103 GPA = 3.6
Name = Mike Hauffmann Id = 101 GPA = 4.0
• In this example, students are compared based on the GPA.
• This happens because while ordering the elements in the TreeSet collection, the TreeSet looks if the objects have their natural order.
• And in this case, uses the compareTo method to compare the objects.
• Some collections, such as TreeSet, are sorted. The TreeSet implementation needs to know how to order the elements.
• If the elements have a natural order, TreeSet uses the natural order. Otherwise, you have to assist it.
• For example, the TreeSet class has the following constructor that takes Comparator as a parameter.
• This constructor creates a new, empty tree set, sorted according to the specified Comparator.