Comparable java interface

Here is the table content of the article will we will cover this topic
1. What is the Comparable java interface?
2. Why we use comparable interface?
3. How to use it?

4. How comparable useful over Comparator?
5. How Comparator useful over comparable?
6. Why Comparable and Comparator are useful?
7. Difference between comparable and comparator?

comparable java

What is the Comparable java interface?

It is an interface that can be used to sort collection elements. This interface is found in java.lang package and contains only one method named compareTo(Object). It imposes order of all the object of class that implements it.

NOTE: All the Wrapper classes implement the Comparable interface by default.

By use of Comparable interface, Object can compare itself with another object. You can provide a single sorting sequence only. It means you can compare the Objects of class based on single data members only.
For example: If you have created a Student class that implements the Comparable interface. So now you can sort the Student data on based on rollNo or name or age etc.

Why we use comparable?

Let’s say you have some data of Student and you want to sort them based on their rollNo.
The first thing comes in mind is that you must compare Student’s rollNo. So, in java you must compare the object with other objects for sorting.
To provide the functionality of Sorting you have to implement the Comparable interface. The Comparable interface has a method compareTo() that is used to compare the objects.

How to use it?

First of all, the class must have to implement the Comparable interface and provide the body to compareTo().

public int compareTo(Object obj): This method is used to compare the current object with the specified object. It takes obj as a parameter and returns int value.
It returns:
positive integer, if current object is greater than the specified object.
negative integer, if current object is less than the specified object.
zero, if current object is equal to the specified object.

In java, String and Wrapper class already implements this interface. So that use can directly compare those objects. For User defined class you have implement it and provide the body to comparerTo() method.

import java.util.ArrayList;
import java.util.Collections;

class Student implements Comparable<Student>
	int rollNo;  
	String name;  
	int age;  
	Student(int rollno,String name,int age)
	public int compareTo(Student student)
		if(rollNo == student.rollNo)  
			return 0;  
		else if(rollNo > student.rollNo)  
			return 1;  
			return -1;  

public class ExampleOfComparable
	public static void main(String args[])
		ArrayList<Student> listOfStudent = new ArrayList<Student>();  
		listOfStudent.add(new Student(1,"Ravi",26));  
		listOfStudent.add(new Student(2,"kant",27));  
		listOfStudent.add(new Student(3,"kamboj",20));  
		// It Sorts all the objects and internally using the CompareTo method  
		for(Student student:listOfStudent)
			System.out.println("RollNo of Student = "+student.rollNo);
			System.out.println("Age of Student = "+student.age);
			System.out.println("Name of Student = ";

Output: RollNo of Student = 1
Age of Student = 26
Name of Student = Ravi
RollNo of Student = 2
Age of Student = 27
Name of Student = kant
RollNo of Student = 3
Age of Student = 20
Name of Student = kamboj

Now, suppose we want sort Student data by their age also. So, you must make changes in compareTo() method. But this is not a good approach because requirement can change any time and every time you must make changes in same method. So,  The solution is using Comparator Interface.

1 thought on “Comparable java interface”

Leave a Comment