Difference between Comparable and Comparator in java

In this post, we will see the difference between Comparable and Comparator in Java. First, we will see Comparable vs Comparator in brief later we will see each point in detail.

ComprableComparator
Comparable interface
provide single
way of
sorting, means we can sort
at a time either name or id.
Comparable interface can be used to provide multiple way of sorting, means we can sort at a time name as well as id.
Comparable interface is available in java.lang package.Comparator interface is available in java.util package.
Comparable has only one method i.e compareTo() and this is not a functional interface.Comparator is a functional interface and apart from compare() method, it has been introduced new methods in jdk1.8.
While sorting using Comparable actual class is modified.In case of Comparator actual class is not modified.
In case of Comparable we need to override comapreTo(Object o) method.In case of Comparator we need to override compare(Object o1, Object o2) method.
In case of comparable we need to use Collections.sort(Collection c). In case of Comparator we need to use Collections.sort(Collection c, Comparator compr)

Example of sorting a list of Employee using Comparable –

package comparableexample;

import java.util.*;

class Employee implements Comparable {
	String name;
	int id;
 
	public Employee(String string, int i) {
		this.name = string;
		this.id = i;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public int getId() {
		return id;
	}
 
	public void setId(int id) {
		this.id = id;
	}
 
	@Override
	public int compareTo(Object o) {
		Employee employee = (Employee) o;
		int id = employee.getId();
		return this.id - id;
	}
}
 
public class SortingUsingComparable {
	public static void main(String[] args) {
		List<Employee> emplist = new ArrayList<Employee>();
		emplist.add(new Employee("ram", 101));
		emplist.add(new Employee("sita", 106));
		emplist.add(new Employee("mohan", 103));
		emplist.add(new Employee("rohan", 105));
		emplist.add(new Employee("gita", 102));
		Collections.sort(emplist);
		for (Employee e : emplist) {
			System.out.println(e.getName() + " " + e.getId());
		}
	}
}

Output is –

ram 101
gita 102
mohan 103
rohan 105
sita 106

If we want to sort the employee list basis of the name we need to modify compareTo() logic something like below.

@Override
public int compareTo(Object o) {
	Employee1 employee = (Employee1) o;
	String name1 = employee.name;
	return this.name.compareTo(name1);
}

Example of sorting a list of Employee using Comparator –

import java.util.Collections;
import java.util.*;
 
class Employee{
	private String name;
	private int id;
	public Employee() {
		
	}
	public Employee(String name, int id) {
		super();
		this.name = name;
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	
}
 
public class SortingUsingComparator {
public static void main(String[] args)throws Exception {
	
	List<Employee> emplist = new ArrayList<>();
	emplist.add(new Employee("mohan",5));
	emplist.add(new Employee("radhika",1));
	emplist.add(new Employee("gopi",3));
	emplist.add(new Employee("krishna",2));
	Collections.sort(emplist, new Comparator() {
 
		@Override
		public int compare(Object o1, Object o2) {
			Employee e1 = (Employee)o1;
			Employee e2 = (Employee)o2;
			return e1.getName().compareTo(e2.getName());
		}
		
		
	});
	
	
	System.out.println("sorting using comparator by name ------");
	
	for(Employee e:emplist) {
		System.out.println(e.getId()+" "+e.getName());
	}
	
	
	System.out.println("sorting using comparator by id  --------");
	Collections.sort(emplist, new Comparator() {
 
		@Override
		public int compare(Object o1, Object o2) {
			Employee e1 = (Employee)o1;
			Employee e2 = (Employee)o2;
			return e1.getId() -e2.getId();
		}
		
		
	});
	
	for(Employee e:emplist) {
		System.out.println(e.getId()+" "+e.getName());
	}
}
}

Output is –

sorting using comparator by name ——
3 gopi
2 krishna
5 mohan
1 radhika
sorting using comparator by id ——–
1 radhika
2 krishna
3 gopi
5 mohan

In case of Comparable we need to override comapreTo(Object o) method, In case of Comparator we need to override compare(Object o1, Object o2) method.

In the Comparable interface, we have compareTo() method and we need to while sorting using Comparable. In the Comparator interface we have compare() method and we need to override it.

Declaration of compareTo() in Comparable interface –

public interface Comparable<Object> {
   
    public int compareTo(Object o);
}

Declaration of compare() in Comparator interface –

public interface Comparator<Object> {
  
    int compare(Object o1, Object o2);

}

In the case of Comparable we need to use Collections.sort(Collection c), In case of Comparator we need to use Collections.sort(Collection c, Comparator compr).

Collections.Sort() method has two overloaded versions, the first one accept a list as an argument and the second one accepts list and Comparator as an argument.

While sorting using Comparable actual class is modified, In case of Comparator actual class is not modified.

What does it mean when we say in case of the Comparable actual class is modified but in case of Comparator it is not? Did you notice in the above examples, in the case of Comparable our Employee class is implementing this interface, but in the case of Comparator Employee class is not implementing Comparator interface? Suppose we have Employee class as third party library i.e as a jar file. Can we sort a list of Employee using Comparable? The answer is no, we can’t. Employee class needs to implement the Comparable interface, which is a little tricky because we have a jar file (means.class). Here we can go for Comparator. Hope this is clear when we need to go for Comparator.

That’s all about Difference between Comparable and Comparator in java.

You may like.

Comparable docs.

Comparator docs.