What is the Difference Between Comparable and Comparator in Java

The main difference between comparable and comparator in Java is that comparable sorts the collection based on a single element while comparator sorts the collection based on multiple elements.

Java is a high-level, general-purpose programming language that helps to build various applications such as web, desktop, mobile, and high performance distributed systems. Moreover, one major advantage of Java is that it supports Object Oriented Programming (OOP). The paradigm allows creating classes and objects. Class is a blue print to create objects. In addition, it has data members to describe the properties and methods to denote the behaviors. Furthermore, Java provides various interfaces to sort objects using the data members of the class. Comparable and comparator are two of them.

Key Areas Covered

1. What is Comparable in Java
     – Definition, Functionality
2. What is Comparator in Java
     – Definition, Functionality
3. What is the Difference Between Comparable and Comparator in Java
     – Comparison of Key Differences

Key Terms

Comparable, Comparator, Interface, Java

Difference Between Comparable and Comparator in Java - Comparison Summary

What is Comparable in Java

The comparable interface helps to compare an object with another object. The class implements java.lang.Comparable interface to compare its instances. The first step is to implement the comparable interface with the class and then to override the method compareTo() of the Comparable interface.

Comparable in Java

Figure 1: Student class

Difference Between Comparable and Comparator in Java

Figure 2: Main program for Comparable

In the above program, Student is a class that implements the Comparable interface. Most importantly, Student class has three data members: id, name, and marks. The constructor helps to provide the initial values to the data members. Moreover, this class overrides the compareTo method. Student object is passed to this method. Thus, this method returns 1,-1 or 0 depending on the comparison.

In the main program, “al” is an object of ArrayList. Programmers can insert Student objects to this ArrayList. The sort method sorts the elements. Finally, the “for loop” displays the elements on the console.

What is Comparator Java

Comparator interface is used to sort elements of a list using different comparators. An example program is as follows.

Comparator in Java

Figure 3: Name and Mark Comparator

Comparable vs Comparator in Java

Figure 4: Main Program for Comparator

In the above program, Student is a class that implements the Comparable interface. Moreover, student class has three data members: id, name, and marks. The constructor helps to provide initial values to the data members. The MarkComparator class implements the Comparator interface. Thus, it overrides the compare method which accepts two Student objects. According to the comparison, the compare method returns 1, -1 or 0.

Similarly, there is another class called AgeComparator which implements the Comparator interface. It also overrides the compare method. Also, this method gets two student objects. Moreover, the compare method returns the integer according to the comparison.

In the main program “al” is an object of ArrayList. Programmer can insert Student obejcts to this ArrayList, just like in line 34,35 and 36. Furthermore, in line 38, the elements are compared according to the name. The “for loop” displays the elements on the screen. In line 43, the elements are compared according to the mark.  Then, the next “for loop” displays the elements on the screen.

Difference Between Comparable and Comparator in Java

Definition

Comparable is an interface in Java used to order the objects of the user-defined class that provides single sorting sequences while Comparator is an interface in Java used to order the objects of a user-defined class that provides multiple sorting sequences. Thus, this explains the main difference between Comparable and Comparator in Java.

Sort Elements

Furthermore, Comparable provides compareTo() method to sort elements whereas Comparator provides compare() method to sort elements. Hence, this is also a difference between Comparable and Comparator in Java.

Syntax to Sort

Another difference between Comparable and Comparator in Java is that Syntax to sort in Comparable is Collections.sort(List) while Syntax to sort in Comparator is Collections.sort(List, Comparator)

Conclusion

In brief, Comparable and Comparator are two interfaces to sort objects using the data members of the class. The main difference between Comparable and Comparator in Java is that Comparable sorts the collection based on a single element while Comparator sorts the collection based on multiple elements.

Reference:

1. “Java Comparable – Javatpoint.” Www.javatpoint.com, Available here.
2. “Java Comparator – Javatpoint.” Www.javatpoint.com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply