Comparator interface

Here is the table content of the article will we will cover this topic.
1. What is the Comparator interface in Java?
2. Why do we use Comparator?
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?

What is the Comparator interface in Java?

It is an interface that can be used to sort collection elements. This interface is found in java.util package and contains two methods compare(Object obj1, Object2 obj) and equals(Object obj). It imposes order on all the objects of the class that implements it. A Comparator interface is used to order the objects of a user-defined class.

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

comparator interface in java

Why do we use Comparator?

In a Comparable interface, we must implement the comparable interface in the same class for which we want to perform sorting. So, what if we want to change the Sorting data member then we must make changes in the same method of the class. But the Comparator is external to the element type we are comparing. We can create multiple separate classes that can implement Comparator to compare different members.

How to use it?

Let’s say you have Student data and you want to sort the data based on different data members of the class. For every data member, you can create a separate class that will implement the Comparator interface and provides the body to compare() method.

public int compare(Object obj1, Object obj2): This method is used to compare the first object(obj1) with a second object(obj1). It takes two parameters and returns int value.
It returns:
positive integer, if the first object is greater than the second object.
negative integer, if the first current object is less than the second object.
zero, if the first object is equal to the second object.

public boolean equals(Object obj): You can compare the current object with the specified object. It returns a boolean value based on the comparison.

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

class Student
{  
	int rollNo;  
	String name;  
	int age;  
	Student(int rollno,String name,int age)
	{  
		this.rollNo=rollno;  
		this.name=name;  
		this.age=age;  
	}  
}

class AgeComparator implements Comparator<Student>
{  
	@Override
	public int compare(Student student1, Student student2) 
	{
		if(student1.age==student2.age)  
		return 0;  
		else if(student1.age>student2.age)  
		return 1;  
		else  
		return -1;  
	}  
}  

class RollNoComparator implements Comparator<Student>
{  
	@Override
	public int compare(Student student1, Student student2) 
	{
		if(student1.rollNo==student2.rollNo)  
		return 0;  
		else if(student1.rollNo>student2.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 based on Age 
		Collections.sort(listOfStudent, new AgeComparator());  
		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);
		}
		
		// It Sorts all the objects based on RollNo 
				Collections.sort(listOfStudent, new RollNoComparator());  
				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 = 3
Age of Student = 20
Name of Student = kamboj
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 = 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

Leave a Comment