Comparable interface in Java

Comparable Interface

 
The Comparable interface declares a compareTo() method that is used to define natural order of objects of class implementing it.
 
The signature of this method is :
 
int compareTo(T obj)
 
This method compares the invoking object with the object passed to it as the method parameter.
 
It returns a negative integer, zero, or a positive integer depending on whether the invoking object is less than, equal to, or greater than the specified object.
 
 

Implementing Comparable Interface

 
Comparable interface is implemented by Date, Enum, String and wrapper classes like Short, Integer, Float, etc..
 
So, while comparing instances of these, we do not need to implement Comparable interface.
 
But, for classes created by us like Employee, Student etc, we need to implement it.
 
 
Here is an example of Class implementing Comparable interface.
 

package com.topjavatutorial;

public class Employee implements Comparable<Employee> {

  int id;

  String name;

  Employee(int id, String name) {
    this.id = id;
    this.name = name;
  }

  @Override
  public int compareTo(Employee emp) {
    return this.name.compareTo(emp.name);
  }

}


 
In this class, compareTo() method compares employees as per their name.
 
 

TreeSet with instances of class implementing Comparable interface

 
In the example, TreeSet stores instances of Employee class, which implements Comparable.
 

package com.topjavatutorial;

import java.util.TreeSet;

public class TreeSetDemo {

  public static void main(String[] args) {

    TreeSet<Employee> empTreeSet = new TreeSet<Employee>();
    
    Employee emp1 = new Employee(1, "Clark");
    Employee emp2 = new Employee(2, "Bernie");
    Employee emp3 = new Employee(3, "Alex");
    
    empTreeSet.add(emp1);
    empTreeSet.add(emp2);
    empTreeSet.add(emp3);
    
    
    for(Employee emp : empTreeSet)
            System.out.println(emp.name);
    

  }

}


 
Running this program will produce following output :
 
Alex
Bernie
Clark
 
The output is sorted by name as that was the logic added in compareTo() method in Employee class.
 
 

TreeMap of instances of class implementing Comparable interface

 
In the example, the TreeMap stores map of employees and their designations.
 

package com.topjavatutorial;

import java.util.TreeMap;

public class TreeMapDemo {

  public static void main(String[] args) {

    TreeMap<Employee,String> empTreeMap = new TreeMap<Employee,String>();
    
    Employee emp1 = new Employee(1, "Clark");
    Employee emp2 = new Employee(2, "Bernie");
    Employee emp3 = new Employee(3, "Alex");
    
    empTreeMap.put(emp1, "Developer");
    empTreeMap.put(emp2, "Manager");
    empTreeMap.put(emp3, "Tester");
    
    
    for(Employee emp : empTreeMap.keySet())
            System.out.println(emp.name);
    

  }

}


 
Running this program will produce following output :
 
Alex
Bernie
Clark
 
 

Comparator interface

 
The Comparator interface is used to define the sort order of a collection of objects, without requiring them to implement this interface.
 

Read more about Comparator interface

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

One comment

  1. […] Read more about Comparable interface.     Comparator interface is useful in scenarios where :   – We need to sort instances of a class that you can’t modify and that doesn’t implement Comparable.   – We want to sort the objects using sorting logic other than the natural order.     […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: