Comparable interface in java

Any data structures in  collections framework  uses the Comparable interface  for sorting of its members . This interface is having one method compareTo().The compareTo() method returns integer value depending on the comparaison.

int status = firstObject.compareTo(secondObject)   is the general format of compareTo() method. The different possibilities of the result is explained here.

status  => Negative when firstObject < secondObject. status => 0(Zero) when firstObject == second object.

status ==Positive when firstObject > secondObject.

Now let us see an example .We have a Student.java class. It is having attributes name and id. We need to store a number of Student objects in a TreeSet  in ascending order of   attribute id .For this our Student.java needs to implement Comparable interface and the compareTo() method should have the logic to sort objects in ascending order of  id .Let us see the Student.java

public class Student implements Comparable {
private int id;
private String name;
public Student(int number, String name) {
this.id = number;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Id = " + getId() + " ; Name = " + getName();
}
public int compareTo(Object o) {
Student student = (Student) o;
return getId() - student.getId();
}
}

Now let us see our main class to run our application. see the ComparableSample.java  shown below.

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class ComparableSample {
private Set set = null;
public ComparableSample() {
set = new TreeSet();
}
public void addItems() {
Student student1 = new Student(2, "Bijoy");
Student student2 = new Student(1, "Karthik");
Student student3 = new Student(3, "Dexter");
Student student4 = new Student(4, "Sasi");
set.add(student4);
set.add(student1);
set.add(student2);
set.add(student3);
}
public void displayItems() {
Iterator itr = set.iterator();
System.out.println("In ascending order");
while (itr.hasNext()) {
System.out.println((Student) itr.next());
}
}
public static void main(String[] args) {
ComparableSample sample = new ComparableSample();
sample.addItems();
sample.displayItems();
}
}

The addItems() method simply adding four Student objects into a TreeSet. First we are adding student4 object. It is having attribute id as 4. Then we are trying to add  student1 object.It is having  attribute id as 2 . So student1.compareTo(student4) will be called. Student4 is having higher value of id. So negative is returning.  Hence student1 is placing before student4. Then student2 is adding to Set. So student2.compareTo(student4) and student2.compareTo(student1) are called. Depending the return value student1 will be arranged in the Set. This comparison is continuing and a Set sorted in ascending order is getting as a result . The displayItems() method simply displays the content of the set using Iterator .

Output

In ascending order

Id = 1 ; Name = Karthik

Id = 2 ; Name = Bijoy

Id = 3 ; Name = Dexter

Id = 4 ; Name = Sasi

So our assumptions are correct.

Now what to do , to get a TreeSet in the reverse (descending)order?The answer is too simple.Just reverse the logic of compareTo() method of  Student.java.So it becomes :  return student.getId() – getId() . And verify the output !!.

One thought on “Comparable interface in java

Leave a Reply

Your email address will not be published.