Comparable Example Java

Comparable Example Java


Before going through this program, it is advised to go through the interface Comparable tutorial.

In the following Employee program, compareTo() method of Comparable interface is used to sort the employees by their ages. To use the compareTo() method, Employee class implements Comparable interface.

Example code on Comparable Example


Comparable Example
Output screenshot on Comparable Example

An Employee class implementing generics Comparable interface is declared. Here generics means, a Comparable object compares only Employee objects. Two instance variables (also known as fields) age and department are declared and through the constructor their values are assigned. Two gettter methods (getAge() and getDepartment()) are also declared with which the field values can be retrieved later.

The Employee class overrides the abstract method of Comparable interface, compareTo() method. The Employee objects are sorted as per the age field. The keyword "this" refers the Employee object with which the compareTo() method is called and emp refers the Employee object we are passing as parameter.

One more class EmployeeExample is created that creates a number of Employee objects and sorts.

A generics ArrayList object employeeList is created and populated with a few elements (here, elements are Employee objects) with add() method.

The Comparable interface should be used with the sort() method of Collections class. Now the sort() method sorts employeeList as the per the Comparator's compareTo() method.

With enhanced for loop, all the employees ages and departments are printed. Observe, the ages are printed in ascending order.

Another program on Comparable is available at Comparable vs. Comparator.

Requires lot of practice to have command over "Comparable Example".

11 thoughts on “Comparable Example Java

  1. Madhava Nandan Rao

    Hello sir,
    I am your student in nexwave. Java 22 batch..
    In the aboive code, what is happening in compareTo(),
    How “this” is working in that? I mean can you explein the compareTo().

    And if we remove Comparable and compareTo(), Then Collections.sort() is showing some error.. Is there any link between them?

    Thank you sir..

    1. S. Nageswara Rao, Corporate Trainer Post author

      compareTo() does lexicographical comparison. It takes one-by-one characters from each string and then compares. Returns the difference of ASCII values of the two characters. When dissimilar characters pair is encountered once, the comparison terminates. It is given in my way2java.com

      Before using compareTo() etc. the elements must be sorted with sort() method. This is also given in my site way2java.com.

      1. Rajesh

        public int compareTo(Employee emp)
        {
        return this.age – emp.age;
        }
        where are we calling this method in the above program by passing emp as parameter.plz rectify how are employee objects are sorted based on compareTo() method?

  2. mohan

    public int compareTo(Employee emp)
    {
    return this.age – emp.age;
    }
    where are we calling this method in the above program by passing emp as parameter.plz rectify how are employee objects are sorted based on compareTo() method?

Leave a Reply

Your email address will not be published. Required fields are marked *