Comparable Interface in Java

Comparable Interface in Java

In this particular article we will discuss the comparable interface in Java along with some simple examples by which one can easily understand the concepts.

Comparable interface in Java

Basically the comparable interface is used for ordering the user-defined classes. This interface is found in java.lang package that contain only one method named CompareTo(object) and it is the property by which comparison and sorting can be done. If a class implements a comparable interface then a collection of its objects can be sorted using the Collection.sort() method and an array of objects containing the class's objects as elements can be sorted using the Array.sort() method.

We can sort the following elements:

  • User-defined class object
  • String object
  • Wrapper class object

String and wrapper classes implement the comparable interface. So if you store the objects of the wrapper or string classes then it will be comparable.

Some syntax that are used in the comparable interface:

  • Collection.sort(): It is used to sort the collection of a class's objects.
  • Array.sort(): It is used to sort the elements of a class's objects.
  • compare.To(): It is used to compare the current object with a specified object.
  • Void.sort(): It is used to sort the elements of a list and the list should be of comparable type.

The following are examples of sorting a collection of Java objects using the comparable interface.

Here we are creating two Java files, one is Employee.java and another one is Main.java.

Employee.java

class Employee implements Comparable

{

    int id;

    String name;

    int age;

    String address;

    int salary;

 

    Employee(int id, String name, int age, String address, int salary)

    {

        this.id=id;

        this.name=name;

        this.age=age;

        this.address=address;

        this.salary=salary;

    }

    public int compareTo(Object obj)

    {

        Employee E=(Employee)obj;

        if(age==E.age)

        return 0;

        else if(age>E.age)

        return 1;

        else

        return -1;

    }

}

Main.java

import java.util.*;

 

class Main

{

    public static void main(String args[])

    {

        ArrayList EL=new ArrayList();

        EL.add(new Employee(1001, "Shubham", 19, "Jhansi", 5000));

        EL.add(new Employee(1002, "Akash", 23, "Lucknow", 10000));

        EL.add(new Employee(1003, "Ravi", 34, "Kanpur", 12000));

        EL.add(new Employee(1004, "Shashikant", 21, "Delhi", 15000));

        Collections.sort(EL);

        Iterator itr=EL.iterator();

        while(itr.hasNext()){

        Employee s=(Employee)itr.next();

        System.out.println(s.id+" "+s.name+" "+s.age+" "+s.address+" "+s.salary);

    }   

}

In the preceding example we have sorted the employee list depending on their age and the output is as follows:

Output

Output

If we sort the employee list depending on their salary then the output will be as follows:

Output

sort the employee

And if we sort the employee list depending on their id then the output will be as follows:

Output

ort the list

You can also sort the list depending on their name or address.

Up Next
    Ebook Download
    View all
    Learn
    View all