Comparable Interface

public interface Comparable
Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Comparable interface features:
  • Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects.
  • int compareTo(Object o1)
    This method compares this object with o1 object and returns a integer.Its value has following meaning:
    1. positive – this object is greater than o1
    2. zero – this object equals to o1
    3. negative – this object is less than o1
Method of Comparable interface
  1. public int compareTo(Object obj);
    Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.
Example of Comparable Interface
import java.lang.Comparable; public class Employee implements Comparable{ int employeeId; String employeeName; int employeeAge; public Employee(int employeeId, String employeeName, int employeeAge) { super(); this.employeeId = employeeId; this.employeeName = employeeName; this.employeeAge = employeeAge; } public int compareTo(Object arg0) { Employee employee=(Employee) arg0; return (this.employeeId < employee.employeeId ) ? -1: (this.employeeId > employee.employeeId ) ? 1:0 ; } public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public int getEmployeeAge() { return employeeAge; } public void setEmployeeAge(int employeeAge) { this.employeeAge = employeeAge; } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class EmployeeMain { public static void main(String[] args) { Employee emp1=new Employee(131, "Sunil",30); Employee emp2=new Employee(121, "Anand",22); Employee emp3=new Employee(123, "Sujeet",27); Employee emp4=new Employee(104, "Sanjeev",28); List<Employee> list = new ArrayList<Employee>(); list.add(emp1); list.add(emp2); list.add(emp3); list.add(emp4); System.out.println("Before Sort : "); for (int i = 0; i < list.size(); i++) { Employee employee=(Employee) list.get(i); System.out.println("Employee Id: "+employee.getEmployeeId()+ "||"+"Employee name: "+employee.getEmployeeName()+ "||"+"Employee Age: "+employee.getEmployeeAge()); } Collections.sort(list); System.out.println("After Sort by Id : "); for (int i = 0; i < list.size(); i++) { Employee employee=(Employee) list.get(i); System.out.println("Employee Id: "+employee.getEmployeeId()+ "|| "+"Employee name: "+employee.getEmployeeName()+ "||"+"Employee Age: "+employee.getEmployeeAge()); } } }

Before Sort :
Employee Id: 131||Employee name: Sunil||Employee Age: 30
Employee Id: 121||Employee name: Anand||Employee Age: 22
Employee Id: 123||Employee name: Sujeet||Employee Age: 27
Employee Id: 104||Employee name: Sanjeev||Employee Age: 28
After Sort by Id :
Employee Id: 104|| Employee name: Sanjeev||Employee Age: 28
Employee Id: 121|| Employee name: Anand||Employee Age: 22
Employee Id: 123|| Employee name: Sujeet||Employee Age: 27
Employee Id: 131|| Employee name: Sunil||Employee Age: 30

Download this example


Latest Tutorial

Most Visited Tutorial