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 do we use comparable interface?
3. How to use it?

4. How is comparable useful over Comparator?
5. How is 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 on all the objects of a class that implements it.

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

By use of a 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 the class based on single data members only.
For example: If you have created a Student class that implements a Comparable interface. So now you can sort the Student data based on rollNo or name or age etc.

Why do we use comparable?

Let’s say you have some data of Students and you want to sort them based on their rollNo.
The first thing that comes to 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 a 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 an int value.
It returns:
positive integer, if the current object is greater than the specified object.
negative integer, if the current object is less than the specified object.
zero, if the current object is equal to the specified object.

In java, the String and Wrapper class already implements this interface. So that users can directly compare those objects. For a User-defined class, you have implemented 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)
	{  
		this.rollNo=rollno;  
		this.name=name;  
		this.age=age;  
	}  
  
	public int compareTo(Student student)
	{  
		if(rollNo == student.rollNo)  
			return 0;  
		else if(rollNo > student.rollNo)  
			return 1;  
		else  
			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  
		Collections.sort(listOfStudent);  
		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 = "+student.name);
		}
    }  
}

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 to sort Student data by their age also. So, you must make changes to the compareTo() method. But this is not a good approach because requirements can change at any time and every time you must make changes in the same method. So,  The solution is using Comparator Interface.

1 thought on “Comparable java interface”

Leave a Comment