Java Comparator Tutorial with Examples

Java Comparator tutorial with examples will help you understand how to use the Comparator interface in an easy way. Comparator interface in Java is used to define the order of objects of the custom or user defined class.

The Java Comparator interface is contained in the java.util package, so you need to import java.util package in order to use the Comparator interface in the code. The Comparator interface contains two methods, compare and equals.

The compare method returns a negative number if object1 is less than object2, zero if object1 and object2 are equal, and a positive number if object1 is greater than the object2.

The equals method returns true if the specified object is equal to this comparator object.

The class implementing the Comparator interface must define the compare method. You may skip implementing the equals method if you want to use the default equals method defined in the Object class (which is a superclass of all the Java classes).

The Comparator interface in Java is used to sort or order the object of a custom class using various criteria. For example, if you have a Product class and you want to sort its objects by the price in descending order or by product name in ascending order.

Once the custom Comparator is created for your class, you can use pass that to theĀ sort method of the Collections class or Array class to sort the objects using the custom Comparator.

As compared to the Comparable interface which allows only one implementation to sort and compare the objects, the Comparator interface allows us to sort the class objects in multiple ways. For example, consider below given Emp class representing employees.

It is not possible to sort the objects of the Emp class by salary in ascending order as well as by age in descending order if the Emp class implements the Comparable interface. It is because the class can have only one implementation of the comareTo method. However, by defining custom Comparator classes you can do that as given in the below example code which has Emp objects stored in an ArrayList.

Output

The above given code has defined two custom Comparator classes for the Emp class. One comparator class sorts the employee objects using the age in ascending order and another class sorts the employee objects by the salary in ascending order. You can have as many Comparator implementations as you want for the given class.

As you can see from the output, the Emp objects are sorted by age and salary in the ascending order. If you want to sort the employee objects by age in descending order, you need to change the comparator implementation as given below.

Output

As you can see from the output, the Emp objects are now sorted by the age in the descending order.

Java Comparator Examples

References:
Java Comparator interface Javadoc

Please let me know if you liked the Java Comparator tutorial with examples in the comments section below.

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

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