Java Comparable interface

In this post under Java Collections, I will explain with example the purpose of “Comparable” interface.

Before we understand “Comparable” interface we need to understand the difference between “ordered” and “sorted” collections.

In Ordered collections, elements are placed in a collection according to insertion order or according to some order and the order is not decided based on the element value. This is usually the default order in most collections.

Whereas in Sorted collections, placement of elements is decided based on the element value. So it is called Sorted collections.

An ordered collection cannot be a sorted collection but a sorted collection can be an ordered collection.

Now if we want elements to be placed in a sorted manner. We need to take help of “Comparable” interface.

So “Comparable” interface is used to sort the elements of a collection by taking into consideration the element value.

This “Comparable” interface has only one abstract method “compareTo”. So in other words, this interface is a Functional Interface.

Below is the details of the method

public interface Comparable<T> {
    int compareTo(T o)
}

For our example if we have a list of employees and we want to sort them in ascending order of their employee id.

We will create a “Employee” class implementing the “Comparable” interface as shown below

Employee

1  package core.collection;
2  
3  public class Employee implements Comparable<Employee> {
4      private int id;
5      private String name;
6      private boolean status;
7  
8      public Employee(int id, String name, boolean status) {
9          this.id = id;
10         this.name = name;
11         this.status = status;
12     }
13 
14     @Override
15     public int compareTo(Employee employee) {
16         if(this.id < employee.id)
17             return -1;
18         else if(this.id == employee.id) {
19             return 0;
20         } else {
21             return 1;
22         }
23     }
24 
25     @Override
26     public String toString() {
27         return String.valueOf(this.id);
28     }
29 }

As you can see in the above code, we have provided an implementation of “compareTo” method. If the current employee’s id is less than passed employee id it will return -1, if the current employee’s id is equal to passed employee id, it will return 0 and if the current employee’s id is greater then passed employee id, it will return 1;

Now when we try to sort the collections of employee using “Collections” “sort” method as shown in the below code snippet. It sorts the list in ascending order of the employee id. In other words it uses
the sorting logic defined in the “compareTo” method of “Employee” class.

    Collections.sort(employeeList);

Sorting collections using “Comparable” interface means collections are sorted in natural ordering.

In this way we use “Comparable” interface.

Below is the complete main code for your reference

Main class

package core.collection;

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

public class ComparableDemo {
    public static void main(String[] args) {
        Employee employee1 = new Employee(1, "employee1", true);
        Employee employee2 = new Employee(2, "employee2", true);
        Employee employee3 = new Employee(3, "employee3", true);

        List<Employee> employeeList = new ArrayList<>(0);
        employeeList.add(employee3);
        employeeList.add(employee2);
        employeeList.add(employee1);

        System.out.println("Before sorting: " + employeeList);

        Collections.sort(employeeList);

        System.out.println("After sorting: " + employeeList);
    }
}

Out of the box the Wrapper class and String class already implement “Comparable” interface.

Leave a comment