Comparable vs Comparator in Java

To compare 2 objects in Java, java has provided two interfaces.

1)Comparable

2)Comparator

Let’s take a look into it one by one.

Comparable:

A comparable object compares itself with another object. The class for which the objects need to be compared must implements the java.lang.Comparable interface to compare its instances.

 Let’s take an example.

Consider a class Employee which has 2 member variables which are employee name and salary. There are 2 instances of it which are emp1 and emp2. In emp1 object, name is John and Salary is 10k.In emp2, name is david and salary is 20k. Let’s sort it using salary.

We can implement the Comparable interface with the Employee class, and we override the method compareTo() of Comparable interface. Kindly check below.

Class Employee implements Comparable
{
  private String name;
  private Integer salary;

  public Employee (String name, Integer salary)
  {
     this.name = name;
     this.salary = salary; 
  }
 
  public int compareTo(Employee emp)
  {
     this.salary - emp.salary;
  }

  public String getName()
  {
     return name;
  }

  public String getSalary()
  {
     return salary;
  }

} 

//Now let's call this class.

Class ComparableTest
{
   
 public static void main(String[] args)
    {
        ArrayList<Employee> list = new ArrayList<Employee>();
        list.add(new Employee("John", 5000));
        list.add(new Employee("David", 10000));
        list.add(new Employee("Rob", 20000));
 
        Collections.sort(list);\\Line 8
 
        System.out.println("Employees after sorting : ");
        for (Employee emp: list)
        {
            System.out.println(emp.getName() + " " +
                               emp.getSalary());
        }
    }
}

If you take a look into the compareTo method in Employee class, it is comparing basis salary. In main class ComparableTest at Line 8, we call Collections.sort method to finally sort the list.

Comparator:

Unlike Comparable, Comparator is a separate class. We create multiple separate classes (that implement Comparator) to compare by different members.
Collections class has a second sort() method which takes Comparator class object as 2nd parameter. The sort() method invokes the compare() to sort objects.

class EmpCompare implements Comparator<Employee>
{
    public int compare(Employee e1, Employee e2)
    {
        if (e1.getSalary() < e2.getSalary()) return -1;
        if (e1.getSalary() > e2.getSalary()) return 1;
        else return 0;
    }
}

So here we have a separate class, which implements Comparator interface.

Now this class is passed to Collections.sort method. We add 2 lines.

EmpCompare empCompare = new EmpCompare();

Collections.sort(list, empCompare);

and everything else remains same in the code which we saw above.

Benefit of using Comparator interface is that we can compare on various properties since the class is different for each comparison.

For comparable, the compareTo interface method has to be defined in the class to be compared. So it can be compared only on one property.

So comparable is compared using compareTo method and comparator is compared using compare method.