Java Comparable and Comparator Interfaces

In this article, We will see why we need Comparable and Comparator. Examples using Comparable and Comparator. Difference between Comparable and Comparator and cases where we need to use the Comparable and Comparator.

In java For predefined Comparable classes default natural sorting order is already available.

We can use Collections.sort or Arrays.sort method to sort the objects in the collection in natural ordering. Means if there are alphabets, sort alphabetically, and if the numbers sort ascending. But

If we are not satisfied with the default natural sorting order then we can define our own customized sorting order by Comparator.

For that, we have the Comparator interface in java. Using this we can order objects or sort the objects in collection and Maps.

Comparator Interface defines two method compare(Object obj1, Object obj2) and Equals().

Java Comparable interface Example

    
import java.util.*;
class Student implements Comparable
{
String name;
int rollNumber;
Student(String name,int rollNumber)
{
	this.name=name;
	this.rollNumber=rollNumber;
}
public String toString()
{
	return name+"----"+rollNumber;
}
public int compareTo(Object o)
{
	int rollNumber1=this.rollNumber;
	int rollNumber2=((Student)o).rollNumber;
	if(rollNumber1 < rollNumber2)
	{
		return -1;
	}
	else if(rollNumber1 > rollNumber2)
	{
		return 1;
	}
	else return 0;
}
}
 

We have a Main class
    
public class mainClassComp
{
	public static void main(String[] args)
	{
		Student e1=new Student("Bob",2001);
		Student e2=new Student("Sarah",2013);
		Student e3=new Student("Jasmin",1099);
		Student e4=new Student("Amar",2050);
		Student e5=new Student("Maithili",1234);
        
		TreeSet t1=new TreeSet(); //Without comparator

		t1.add(e1);
		t1.add(e2);
		t1.add(e3);
		t1.add(e4);
		t1.add(e5);
        
		System.out.println(t1);//[Jasmin----1099, Maithili----1234, Bob----2001, Sarah----2013, Amar----2050]
        
		TreeSet t2=new TreeSet(new MyComparator());//with customized comparator

		t2.add(e1);
		t2.add(e2);
		t2.add(e3);
		t2.add(e4);
		t2.add(e5);
		System.out.println(t2);[Amar----2050, Bob----2001, Jasmin----1099, Maithili----1234, Sarah----2013]
	}
}
 

We have a Comparator class

    
 
class MyComparator implements Comparator
{
	public int compare(Object obj1,Object obj2)
	{
		Student e1=(Student)obj1;
		Student e2=(Student)obj2;
		String s1=e1.name;
		String s2=e2.name;
		return s1.compareTo(s2);
		}
}



 

When to use Comparable and Comparator

  • If we are not satisfied with the default natural sorting order then we can define our own customized sorting order by Comparator.
  • While development you have to write code to sort object of a class which is already written some senior developers and you are not allowed to change the code. IIn this situation cases you have to use to sort those objects. You can not use Comparable to sort the objects.
  • If you are using collection classes like SorteSet or SortedMap, TreeSet, and TreeMap for storing your objects. It is not mandatory to write comparator for these classes as their classes will always provide.
  • The Order of comparison is very important while implementing Comparable or Comparator interface. In the above example, if you are sorting an object-based upon name than you can compare the first name or last name.
    Difference between Comparable and Comparator 

  Comparable Comparator
 1 Comparable meant for default natural sorting order. Comparator meant for customized sorting order.
 2 Present in java.lang package. Present in java.util package.
 3 It contains only one method. compareTo() method. Contains 2 methods. Compare() method. Equals() method.
 4 String class and all wrapper Classes implements Comparable interface. The only implemented classes of Comparator are Collator and RuleBasedCollator. (used in GUI)


In this article, We have seen Java Comparable and Comparator Interfaces and their use cases and examples.

All source code in the article can be found in the GitHub repository.