Comparator Vs Comparable in Java

The Comparator in Java used  for Sorting of members in data structures like TreeSet or TreeMap.The Comparable interface is also using for the same purpose. But there are  few differences between these two interfaces.This chapter deals with Comparator  Vs Comparable in Java

1)Comparable interface is having the method compareTo(Object arg). But Comparator is having method compare(Object arg1 , Object arg2). The working of  these two methods can be well understood by analyzing examples.In our case we need to sort few Student objects in ascending order of attribute ‘id’.

First Let us see the working of Comparable interface.Here the Student.java is implementing the Comparable interface. So the compareTo() method does the necessary comparison to sort the objects in ascending order of  ‘id’.

Now let us see the ComparableSample.java.It is having the main() method.The addItems() method is adding few Student objects . Then the  displayItems() method is displaying the items in the sorted order.

Let us verify the output.

Output

In ascending order

Id = 1 ; Name = Karthik

Id = 2 ; Name = Bijoy

Id = 3 ; Name = Dexter

Id = 4 ; Name = Sasi

Now let us see an example which sorts a number of Student objects  using Comparator. See the Student.java first.

Now let us see the ComparatorSample.java . In the constructor  we are initializing a TreeSet object with a Comparator instance as argument . The Comparator instance does the necessary comparison for sorting. The Comparator  instance shown here sorts the set in ascending order

Now let us verify our output.

Output

In ascending order

Id = 1 ; Name = Karthik

Id = 2 ; Name = Bijoy

Id = 3 ; Name = Dexter

Id = 4 ; Name = Sasi

2)From the method arguments itself , it is clear that compareTo() method of Comparable  is calling on current object.It compares the current object with  the object which is coming as argument.But the compare() method receives two arguments and comparing those two argument objects.

3)The purpose of these two interfaces is to sort the collection elements in an order. If we want objects of a particular class in a collection (example: TreeSet)  , and   we are interested in Comparable only .In that case the class whose objects needs to be stored in the structure should implement Comparable. If we are interested in Comparator then the class whose objects needs to be stored  need not implement Comparator(We can create a separate class implementing Comparator which does comparison). In other words we cannot make a separate comparison class by implementing Comparable. But we can make a separate comparison class by implementing Comparator.

4)Suppose we need a group of objects of a particular class to be sorted in an order. And we implemented that particular class with Comparable . In the later stage of our application we need to sort  a group of objects of the same class  in another order. In that case  Comparable is useless. We cannot make multiple Comparable implementations for a particular class. But if we are using Comparator , it is possible to define any number Comparator units as our wish.

So let us discuss an example on point 4 explained above .

We have an Student.java class which implements the Comparable interface to make objects of employee.java in ascending order of attribute ‘ id’.

And at certain point of time  , we need the objects in descending order of id .And also  later we need objects in ascending order of attribute ‘name’. So we should have two Comparator instances . It is explained in the Main.java .

So if we are using Comparator then  we can define new comparing strategies . If we use Comparable alone  , we cannot get different sorting strategies.Let us verify the output.

Output

In ascending order of id

Id = 1 ; Name = Karthik

Id = 2 ; Name = Bijoy

Id = 3 ; Name = Dexter

Id = 4 ; Name = Sasi

In descending order of id

Id = 4 ; Name = Sasi

Id = 3 ; Name = Dexter

Id = 2 ; Name = Bijoy

Id = 1 ; Name = Karthik

In ascending order of name

Id = 2 ; Name = Bijoy

Id = 3 ; Name = Dexter

Id = 1 ; Name = Karthik

Id = 4 ; Name = Sasi

So this makes our understanding clear.

See Related Topics:

Comparable in Java

Comparator in Java

Collections in Java

Iterator in Java

2 thoughts on “Comparator Vs Comparable in Java

  1. I like what you guys are up as well. Such intelligent work and reporting! Carry on the excellent works guys I’ve incorporated you guys to my blogroll. I feel it?

  2. Good details i Found in ur website it’s self understandable Thanks a Lot

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">