Comparable vs Comparator

Comparable Comparator
What is it? Here the Object compares itself with another Object of same type.For example: An Employee comparing his/her salary with another employee. This is a third party which compares two objects of same type.For example: A manager comparing the performances of two of it’s team members.
Do we need to write a separate class? NO.

The class itself must implements the java.lang.Comparable interface so that it’s instances can be compared.The class has to override the compareTo(Object object) method of Comparable interface.

YES.

It is a separate class which implements java.util.Comparator interface. This class overrides the compare(Object obj1, Object obj2) method of Comparator interface.

In how many ways we can provide the sorting? Comparable interface can be used to provide single way of sorting.

The objects are sorted in natural order which means the object itself knows how it is to be ordered.For example: We can think of “Id” as the natural order for sorting Employees.

Comparator interface is used to provide different ways of sorting.

We can create multiple Comparators for comparing the objects in multiple ways. For example: We can create SalaryComparator and NameComparator for sorting Employees based on Salary and Name respectively.

When to use? 1.If there is a natural or default way of sorting Object then use Comparable. For example: We can think of “Id” as the natural order for sorting Employees.

2. When you have access to code.

3.If your class implements Comparable then Arrays.sort() and Collections.sort() can sort its instances automatically. You do not need to write separate comparators and pass them to overloaded sort() method.

4.Objects which implement Comparable interface can be used as keys in a SortedMap( like TreeMap) or as elements in a SortedSet  (like TreeSet). Otherwise you have to write separate Comparator and pass it in the constructor of TreeMap.

 

 

1.If an Object can be sorted on multiple ways and client class is specifying on which parameter sorting should take place
then use Comparator interface. For example Employee can be sorted on name, salary etc.

2.When you don’t have access to code. In these cases you can not implement Comparable and Comparator is only way to sort those objects.

Example
Let us take the example of Employee. In this case, the natural ordering of the Employee is by Id. We have also created two Comparators SalaryComparator and NameComparator to sort Employees by Salary and Name respectively.

Employee Class which implements Comparable

package com.comparable.example;

public class Employee implements Comparable{

private String name;
private Long salary;
private Long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getSalary() {
return salary;
}
public Employee(String name, Long salary, Long id) {
this.name = name;
this.salary = salary;
this.id = id;
}
@Override
public String toString() {
return “Employee [name=” + name + “, salary=” + salary + “, id=” + id
+ “]”;
}
public void setSalary(Long salary) {
this.salary = salary;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int compareTo(Employee employee) {
return this.getId().compareTo(employee.getId());
}

}

SalaryComparator

package com.comparator.example;

import java.util.Comparator;

import com.comparable.example.Employee;

public class SalaryComparator implements Comparator{

@Override
public int compare(Employee emp1, Employee emp2) {
return emp1.getSalary().compareTo(emp2.getSalary());
}

}

NameComparator

package com.comparator.example;

import java.util.Comparator;

import com.comparable.example.Employee;

public class NameComparator implements Comparator{

@Override
public int compare(Employee emp1, Employee emp2) {
return emp1.getName().compareTo(emp2.getName());
}

}

TestEmployee class

package com.comparable.example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.comparator.example.NameComparator;
import com.comparator.example.SalaryComparator;

public class TestEmployee {

public static void main(String[] args) {

List emps = new ArrayList<>();

Employee emp1 = new Employee(“Gyan”, 1000000L, 1L);
emps.add(emp1);
Employee emp2 = new Employee(“Praveen”, 1500000L, 2L);
emps.add(emp2);
Employee emp3 = new Employee(“Rochit”, 800000L, 4L);
emps.add(emp3);
Employee emp4 = new Employee(“Vivek”, 12000000L, 3L);
emps.add(emp4);
Employee emp5 = new Employee(“Prabhakar”, 1800000L, 5L);
emps.add(emp5);
Employee emp6 = new Employee(“Sudeep”, 1600000L, 6L);
emps.add(emp6);

Collections.sort(emps);
System.out.println(“Natural Ordering by Id:”+emps);
Collections.sort(emps,new SalaryComparator());
System.out.println(“Ordering by Salary:”+emps);
Collections.sort(emps,new NameComparator());
System.out.println(“Ordering by Name:”+emps);

}

}

Output

Natural Ordering by Id:[Employee [name=Gyan, salary=1000000, id=1], Employee [name=Praveen, salary=1500000, id=2], Employee [name=Vivek, salary=12000000, id=3], Employee [name=Rochit, salary=800000, id=4], Employee [name=Prabhakar, salary=1800000, id=5], Employee [name=Sudeep, salary=1600000, id=6]]

Ordering by Salary:[Employee [name=Rochit, salary=800000, id=4], Employee [name=Gyan, salary=1000000, id=1], Employee [name=Praveen, salary=1500000, id=2], Employee [name=Sudeep, salary=1600000, id=6], Employee [name=Prabhakar, salary=1800000, id=5], Employee [name=Vivek, salary=12000000, id=3]]

Ordering by Name:[Employee [name=Gyan, salary=1000000, id=1], Employee [name=Prabhakar, salary=1800000, id=5], Employee [name=Praveen, salary=1500000, id=2], Employee [name=Rochit, salary=800000, id=4], Employee [name=Sudeep, salary=1600000, id=6], Employee [name=Vivek, salary=12000000, id=3]]

Leave a Reply

Your email address will not be published. Required fields are marked *