Ordering Collections in JAVA : Part 1

Ordering Collections:-

• The comparable interface and comparator interface are the interfaces which are used for the Ordering Collection.
• The Comparable interface imparts natural ordering to the classes which implement it.
• The Comparator interface is used to specify the order relation. It can also be used to override the natural ordering.
• Both the interfaces, Comparable interface and Comparator interface are useful to sorting elements in a collection.
The Comparable interface:-
• The Comparable interface is a member of a java.lang package.
• A class which implements a Comparable interface having a natural ordering.
• When we declare a class, the JVM implementation has no means of determining the order you intend for an objects of that class.
• We can, by implementing the Comparable interface, provide order to the object of any class.
• We can sort collections that contain objects of classes that implement the Comparable interface.
• So, when we implement a Comparable interface in Collection we can get any type of object of class which is sorted.
• For that we have to implement the compareTo() method of Comparable interface.
• There are 14 classes in java which implement the Comparable interface, some of them are Byte, Date, Integer, Character, Double, etc.
public int compareTo(Object obj)
Explanation of comparTo() method:-
• Elements must be mutually comparable.
• In this, we have to compare the elements every time.
• If the elements are not mutually comparable then we will get the ClassCastException.
• Mutually comparable means both the elements must compare with one another.
• Suppose in a collection, we are comparing Date and Float into the TreeSet then it does not work, because, they are not mutually comparable.
• So, if we want to compare those 2 objects then those objects must be of same type.
• Now we are going to write a custom Comparable type. So, for this we need to implements the compareTo method of Comparable interface.
• And below code shows how to implement the Comparable interface.
• The Student class is implementing the Comparable interface so that the objects of this class can be compared to each other based on grade point average (GPA).
class Student implements Comparable
String firstname, lastname;
int stdId = 0;
double GPA = 0.0;
public Student(String firstname, String lastname, int stdId, double GPA)
if(firstname == null || lastname == null || stdId == 0 || GPA == 0.0)
Throw new IllegalArgumentException();
this.firstname = firstname;
this.lastname = lastname;
this.stdId = stdId;
this.GPA = GPA;
public String firstname()
return firstname;
public String lastname()
return lastname;
public int stdId()
return stdId;
public double GPA()
return GPA;
public int compareTo(Object o)
double f = GPA – ((Student)o).GPA;
if(f == 0.0)
return 0; // 0 signifies equals
else if(f < 0.0)
return -1; // negative value signifies less than or before
return 1; // positive value signifies greater than or after
• The StudentList program given below tests the Comparable interface implementation created in the earlier program.
• The StudentList program creates four Student objects and prints then. Because the Student objects are added to the TreeSet, which is a sorted set, from that objects are sorted.
import java.util.*;
public class ComparableTest
public static void main(String args[])
TreeSet stdSet = new TreeSet();
stdSet.add(new Student(“Mike”,”Hauffmann”,101,4.0));
stdSet.add(new Student(“James”,”Jerry”,102,2.8));
stdSet.add(new Student(“Jim”,”Max”,103,3.6));
stdSet.add(new Student(“Rima”,”Timothy”,104,2.3));
Object[] studentArray = stdSet.toArray();
Student s;
for(Object obj : studentArray)
S = (Student) obj;
System.out.println(“Name = %s %s Id = %d GPA = %.lf\n”,s.firstname(),s.lastname(),s.stdId(),s.GPA());
Name = Rima Timothy Id = 104 GPA = 2.3
Name =James Jerry Id = 102 GPA = 2.8
Name = Jim Max Id = 103 GPA = 3.6
Name = Mike Hauffmann Id = 101 GPA = 4.0
• In this example, students are compared based on the GPA.
• This happens because while ordering the elements in the TreeSet collection, the TreeSet looks if the objects have their natural order.
• And in this case, uses the compareTo method to compare the objects.
• Some collections, such as TreeSet, are sorted. The TreeSet implementation needs to know how to order the elements.
• If the elements have a natural order, TreeSet uses the natural order. Otherwise, you have to assist it.
• For example, the TreeSet class has the following constructor that takes Comparator as a parameter.
TreeSet(Comparator comparator)
• This constructor creates a new, empty tree set, sorted according to the specified Comparator.

For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply