There are many ways by which we can sort the objects in the java. But there are two special kinds of interfaces using which we can sort the objects in the java. These interfaces are Comparable and Comparator. In this tutorial, we are going to discuss these two interfaces.
First, let’s
discuss the differences between these two Interfaces and after that, we will
look into them using examples:
Differences between Comparator and Comparable
The differences
between Comparable and Comparator interface are as follows:
1.
Use
Comparable to sort the object with natural sorting order whereas use Comparator to sort the objects based
on user-defined criteria.
2.
Comparable interface has compareTo()
method whereas Comparator interface has compare() method.
3.
Comparable interface is available in java.lang package whereas Comparator
interface is present in java.util
package.
4.
Comparable interface compares the current reference(this) with the object specified whereas Comparator compares two different objects.
5.
If
we are implementing Comparable interface
then the original class will get affected whereas if we are implementing Comparator interface we don’t have to
change the implementation of the class.
Now, that the
differences are clear let’s jump directly into the individual interfaces and understand
them with the help of an example
Comparable Interface:
We should use
the concept of the comparable interface when we have to sort the object based
on default implementation. As discussed, the comparable interface has the compareTo()
method. As comparable is an interface so every class which implements this
interface has to override the compareTo() method. The compareTo() method will
compare the current reference(this) with the object that is passed as an
argument to the compareTo() method.
Consider the
following example.
Output
Student list after sorting
1 Ram India
2 Ravi India
3 Rahul India
4 Shyam India
This is the scenario where we wanted to sort the list based on only one criterion. Now, consider the scenario where we have to sort the list based on multiple criteria. Comparator interface provides the solution to this problem. Let’s see how.
Comparator Interface
Unlike comparable,
the Comparator interface provides the facility to sort the given objects with different
criterion. Comparator interface has compare() method. So, any class which is
implementing the Comparator interface has to override compare() method. Collections
class has a sort() method which will sort the collection. It takes the list object
and type of Comparator.
Consider the
following example:
Output
Student list sorting by studentId
1 Ram India
2 Ravi India
3 Rahul India
4 Shyam India
Student list sorting by studentName
3 Rahul India
1 Ram India
2 Ravi India
4 Shyam India
In this example,
we have two Class which implements Comparator interfaces. Each class has their
compare() method which is sorting the list object based upon different
criterion. IdComparator is sorting the list based on studentId and
NameComparator is sorting the list based on studentName. In the main class, we
can use the object of any of the Comparator to sort the collection.
As while using
comparator interface we created two separate classes and given the
implementation separately for sorting hence, our main class remains untouched. This
is not in the case of Comparable interface. While using the Comparable
interface as we have to override the compareTo() method inside the class
itself, hence, we need to change the implementation of the class.
With this
discussion, we conclude that which sorting method we should purely depend upon
the requirement. I hope I have explained all the concepts of this topic.
Related Articles:
1. Core java interview questions.
2. Java 8 interview questions.
3. Internal working of Hashmap in java.
0 Comments