Difference between Comparator and Comparable interface in Java


Comparator and Comparable are two interfaces which used to implement Object sorting in Java. It used to sort Objects stored in any Collection classes like HashMap, HashTable, ArrayList etc., Whenever we implement Comparator interface then we need to implement compare (Object o1, Object o2) method and compareTo(Object o) method for Comparable interface. 
Going further we will discuss on differene between these 2 interface and lets see small example on how to use Comparator and Comprable interfaces. 


  • Comparator interface used to compare any class Objects by compare (Object o1, Object o2) method, whereas Comparable interface to sort same class Objects by compareTo(Object o) method. 
  • Comparator defined in java.util package which says to use it as utility to sort Objects, while Comparable interface defined in java.lang package.
  • Comparable interface used to implement natural ordering of Object which used in Date, String and Wrapper classes in Java. If its a same class Objects then compareTo(Onject o) is a good practice to use it. 
  • Collections.sort(List) here objects will be sorted on the basis of CompareTo method in Comparable. Collections.sort(List, Comparator) here objects will be sorted on the basis of Compare method in Comparator.

Comparator Example:

import java.util.Comparator;

class Employee implements Comparator<Employee>{
 private String name;
 private int id;
 public Employee() {
 public String getName() {
  return name;
 public int getId() {
  return id;
 public Employee(String name,int id){
 // Sorting based on Employee Name
 public int compare(Employee obj1, Employee obj2) {
      return 1;
     }else if(obj1.name.toString().compareTo(obj2.name.toString())<0){
      return -1;
      return 0;     
 // Sorting based on Employee Id
 public int compare(Employee obj1, Employee obj2) {
  return obj1.id - obj2.id; 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ComparatorTest  {

 public static void main(String[] args) {
  List <Employee>list = new ArrayList<Employee>();
  Employee e1 = new Employee("Raj",22);
  Employee e2 = new Employee("Anna",25);
  Employee e3 = new Employee("David",12);
  Employee e4 = new Employee("Steve",100);
  Employee e5 = new Employee("Jobs",220);
  Employee e6 = new Employee("Bond",13);
  Collections.sort(list, new Employee());
                   for (Employee contact : list) {



Comparable Example:

public class Employee implements Comparable<Employee> {

 private String empId;
 private String empName;

 public Employee() {
 public Employee(String empId, String empName){
  this.empId = empId;
  this.empName = empName;
 public String getEmpId() {
  return empId;
 public String getEmpName() {
  return empName;

 public int compareTo(Employee emp) {
  return this.empName.compareTo(emp.empName);

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class CompratorTest {
 public static void main(String[] args) {
  List<Employee> arr = new ArrayList<Employee>();
  arr.add(new Employee("22","Raj"));
  arr.add(new Employee("25","Anna"));
  arr.add(new Employee("12","David"));
  arr.add(new Employee("100","Steve"));
  arr.add(new Employee("220","Jobs"));
  arr.add(new Employee("13","Bond"));
  //Object sort using Comparable

  Iterator<Employee> itr1 = arr.iterator();
  while (itr1.hasNext()) {
   Employee empobj = (Employee) itr1.next();
   System.out.println(empobj.getEmpName()+" \t\t "+empobj.getEmpId());


Anna    25
Bond    13
David    12
Jobs    220
Raj    22
Steve    100

No comments:
Write comments