top of page
Writer's pictureHiren Desai

"Java's Dynamic Duo: A Guide to Comparable and Comparator"


Both Comparable and Comparator interfaces are used to perform sorting operation on collection of objects. Java also provide few in-build methods to sort array of primitive types or array/list of Wrapper class elements. But when it comes to the sorting of custom class objects then java.lang.Comparable and java.util.Comparator interfaces are used.

Comparable

Comparator

  1. It's meant for Default Natural Sorting Order

  1. It's meant for Customized Sorting Order

2. Present in java.lang Package

2. Present in java.util Package

3.This interface defines only one method .compareTo()

3.This interface defines 2 method .compare()

.equals()

4. All Java Wrapper class and String class implement Comparable Interface. So they have their natured order defined.

4. Collator class and

RuleBasedCollator class

These two classes implements Comparator interface in java

(No need to remember this point it's just for Knowledge)


Comparable


Comparable interface is used to provide a natural ordering of its instances. It is an interface that is implemented by a class whose objects can be ordered. The interface has a single method, compareTo(Object obj), which returns an integer value based on the comparison of the current object with the object passed as an argument.


The compareTo() method returns the following values:

  • a negative integer if the current object is less than the object passed as an argument

  • zero if the current object is equal to the object passed as an argument

  • a positive integer if the current object is greater than the object passed as an argument.

The Comparable interface is used when we want to sort a collection of objects based on some natural ordering. For example, we can use Comparable interface to sort a collection of strings in alphabetical order, or a collection of integers in ascending order.


Example of Implementing Comparable Interface Suppose we have a class Student with three properties: name, age, and marks.






In the above example, the Student class implements the Comparable interface and overrides the compareTo() method. The compareTo() method compares the current object with the object passed as an argument based on the marks property. If the marks of the current object is less than the marks of the object passed as an argument, it returns a negative value, if they are equal, it returns zero, and if the marks of the current object is greater than the marks of the object passed as an argument, it returns a positive value.


Now, we can create a list of Student objects and sort them using the Collections.sort() method.


Output:


In the above example, we have created a list of Student objects and added them to the list. We have then sorted the list using the Collections.sort() method. Since Student class implements the Comparable interface, the Collections.sort() method uses the compareTo() method to sort the list based on the marks property. Finally, we have printed the sorted list using a for-each loop.



Comparator


Comparator is an interface that allows you to compare two objects. It is used to sort collections of objects or to determine the order of elements in a data structure such as a tree or a priority queue.


The Comparator interface defines two methods: compare() and equals(). The compare() method takes two objects as arguments and returns an integer value that indicates the order of the objects.

If the first object is less than the second, it returns a negative value.

If the first object is greater than the second, it returns a positive value.

If the objects are equal, it returns zero.


Here question arise that it's an interface having two methods compare() and equals() so need to implement compare() but we do not require to implement equals() method because it's already available in class from Object class through inheritance.



Example of Implementing Comparator Interface






Here, We've implemented TreeSet Example implementing Comparator interface

TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in a sorted and ascending order.

As we can see in the above code line number 24,25 duplicates we can insert using Comparator which is Customized way of Sorting the Objects.


Comparator Second Example:


let's consider a Person class that contains name, age, and salary properties. We will define two Comparator implementations to sort a list of Person objects based on age and salary, respectively.


Person Class:























Main App:


Output:











Conclusion: In summary, the Comparable and Comparator interfaces are used for sorting objects in Java collections.

The Comparable interface is used to define a natural ordering of objects, while the Comparator interface is used to define a custom ordering of objects. The Comparable interface is implemented by the class being sorted,

while the Comparator interface is implemented by a separate class. The compare() method is used by the Comparator interface to define the custom ordering, while the compareTo() method is used by the Comparable interface to define the natural ordering. These interfaces provide a flexible and powerful way to sort collections of objects in Java.

55 views0 comments

Recent Posts

See All

Comments


bottom of page