Comparable and Comparator in Java Example

Comparable and Comparator in Java are very useful for sorting the collection of objects. Java provides some inbuilt methods to sort primitive types array or Wrapper classes array or list. Here we will first learn how we can sort an array/list of primitive types and wrapper classes and then we will use java.lang.Comparable and java.util.Comparator interfaces to sort array/list of custom classes.

Let’s see how we can sort primitive types or Object array and list with a simple program.

Output of the above program is:

Now let’s try to sort an array of objects.

Here is the code I used to sort the array of Employee objects.

When I tried to run this, it throws the following runtime exception.

Comparable and Comparator

Java provides Comparable interface which should be implemented by any custom class if we want to use Arrays or Collections sorting methods.

The Comparable interface has compareTo(T obj) method which is used by sorting methods, you can check any Wrapper, String or Date class to confirm this. We should override this method in such a way that it returns a negative integer, zero, or a positive integer if “this” object is less than, equal to, or greater than the object passed as an argument.

After implementing Comparable interface in Employee class, here is the resulting Employee class.

Now when we execute the above snippet for Arrays sorting of Employees and print it, here is the output.

As you can see that Employees array is sorted by id in ascending order.

But, in most real-life scenarios, we want sorting based on different parameters. For example, as a CEO, I would like to sort the employees based on Salary, an HR would like to sort them based on age. This is the situation where we need to use Java Comparator interface because Comparable.compareTo(Object o) method implementation can provide default sorting and we can’t change it dynamically. Whereas with Comparator, we can define multiple methods with different ways of sorting and then chose the sorting method based on our requirements.

Java Comparator

Comparator interface compare(Object o1, Object o2) method need to be implemented that takes two Object argument, it should be implemented in such a way that it returns negative int if the first argument is less than the second one and returns zero if they are equal and positive int if the first argument is greater than the second one.

Comparable and Comparator interfaces use Generics for compile-time type checking, learn more about Java Generics.

Here is how we can create different Comparator implementation in the Employee class.

All the above implementations of Comparator interface are anonymous classes.

We can use these comparators to pass an argument to sort function of Arrays and Collections classes.

Here is the output of the above code snippet:

So now we know that if we want to sort java object array or list, we need to implement java Comparable interface to provide default sorting and we should implement java Comparator interface to provide different ways of sorting.

We can also create separate class that implements Comparator interface and then use it.

Here is the final classes we have explaining Comparable and Comparator in Java.

Here is the separate class implementation of Comparator interface that will compare two Employees object first on their id and if they are same then on the name.

Here is the test class where we are using different ways to sort Objects in java using Comparable and Comparator.

Here is the output of the above program:

The java.lang.Comparable and java.util.Comparator are powerful interfaces that can be used to provide sorting objects in java.

Comparable vs Comparator

  1. Comparable interface can be used to provide single way of sorting whereas Comparator interface is used to provide different ways of sorting.
  2. For using Comparable, Class needs to implement it whereas for using Comparator we don’t need to make any change in the class.
  3. Comparable interface is in java.lang package whereas Comparator interface is present in java.util package.
  4. We don’t need to make any code changes at client side for using Comparable, Arrays.sort() or Collection.sort() methods automatically uses the compareTo() method of the class. For Comparator, client needs to provide the Comparator class to use in compare() method.

Do you know that Collections.sort() method that takes Comparator argument follows Strategy Pattern?

You can checkout complete code and more core java examples from our GitHub Repository.

By admin

Leave a Reply

%d bloggers like this: