Comparable interface in java

Let’s learn comparable interface in java.

Comparable interface in java

Comparable interface is an interface used to compare objects of same type. It can be implemented by any custom class and is found in java.lang package.

Declaration:

public interface Comparable<T>

Parameters:

<T> the type of objects that this object may be compared to


Comparable interface provides only one abstract method named compareTo(Object obj) method. This method is referred to as its natural comparison method.

Declaration:

public int compareTo(Object obj)

Parameters:

obj the object to be compared.

compareTo method returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The compareTo() method is used to compare objects which returns,

  • positive number if first object is greater than the second object.
  • zero if first object is equal to second object.
  • negative number if first object is less than the second object.

Let’s see java comparable example(natural ordering).

class Employee implements Comparable<Employee>
{
   int empID;
   String empName;
   int empAge;
   Employee(int empID, String empName, int empAge)
   {
      this.empID = empID;
      this.empName = empName;
      this.empAge = empAge;
   }
   public int compareTo(Employee emp)
   {
      if(empAge == emp.empAge)
      {
         return 0;
      }
      else if(empAge > emp.empAge)
      {
         return 1;
      }
      else
      {
         return -1;
      }
   }
}
import java.util.ArrayList;
import java.util.Collections;
public class ComparableExample
{
   public static void main(String[] args)
   {
      ArrayList<Employee> al = new ArrayList<Employee>();
      al.add(new Employee(7056,"virat", 25));  
      al.add(new Employee(7158,"amit", 28));  
      al.add(new Employee(7263,"jay", 20));     
      Collections.sort(al);
      for(Employee obj : al)
      {  
         System.out.println(obj.empID + " " + obj.empName + " " + obj.empAge);
      }
   }
}


Output:

7263 jay 20
7056 virat 25
7158 amit 28


Now let’s see comparable interface example in reverse order.

class Employee implements Comparable<Employee>
{
   int empID;
   String empName;
   int empAge;
   Employee(int empID, String empName, int empAge)
   {
      this.empID = empID;
      this.empName = empName;
      this.empAge = empAge;
   }
   public int compareTo(Employee emp)
   {
      if(empAge == emp.empAge)
      {
         return 0;
      }
      else if(empAge < emp.empAge)
      {
         return 1;
      }
      else
      {
         return -1;
      }
   }
}
import java.util.ArrayList;
import java.util.Collections;
public class ComparableExample
{
   public static void main(String[] args)
   {
      ArrayList<Employee> al = new ArrayList<Employee>();
      al.add(new Employee(7056,"virat", 25));  
      al.add(new Employee(7158,"amit", 28));  
      al.add(new Employee(7263,"jay", 20));     
      Collections.sort(al);
      for(Employee obj : al)
      {  
         System.out.println(obj.empID + " " + obj.empName + " " + obj.empAge);
      }
   }
}


Output:

7158 amit 28
7056 virat 25
7263 jay 20


Also read – interface in java