Comparable vs Comparator

Comparable vs Comparator

4 mins read609 Views Comment
Anshuman
Anshuman Singh
Senior Executive - Content
Updated on Nov 22, 2022 18:43 IST

Comparable and comparator both are types of interfaces provided by Java in order to sort objects using data members of the class. But, both of these interfaces work differently; hence, comparable vs comparator interfaces have differences. The main difference between comparable vs comparator is that comparable provides a single sorting sequence, whereas comparator provides multiple sorting sequences.

2022_11_MicrosoftTeams-image-149.jpg

In this article, we will explore comparable vs comparator in great detail. But, before we begin, let’s quickly go through the topics listed under the table of contents (TOC) that we will cover in this blog.

You can aslo explore: Java Methods Explained (with Code)

Table of contents (TOC)

Comparable vs Comparator 

For better understanding, let’s explore the difference between comparable and comparator interfaces in a tabular format:

Benchmark Comparable Comparator
Provides Single sorting sequence Multiple sorting sequence
Modifies the original class Yes No
Method used to sort is compareTo() compare()
Sorts the data according to Natural sorting order Customized sorting order
It is present in  java.lang package java.util package
Method to sort the list elements Collections.sort(List)  Collections.sort(List, Comparator)
The logic of sorting must be in Same class Separate class

What is comparable? 

Comparable definition: A comparable interface provides a single sorting sequence, and when this interface is used, it affects the actual class or the original class. 

In layman’s terms, in Java, comparable interface allows an object to compare itself with another object and helps to sort the list of custom objects. If you have a fixed requirement, for example, if you want to sort the class objects depending on a single field of the class, then it is recommended to use the comparable interface. For a better understanding of comparable interfaces, let’s go through an example in the next section of this article.

You can also explore: Multithreading in Java

Comparable example

For this example, let’s use the comparable interface to sort the list of elements based on age.

 
import java.util.*;
import java.io.*;
class Student implements Comparable
{
int rollno;
String name;
int age;
Student(int rollno,String name,int age)
{
this.rollno=rollno;
this.name=name;
this.age=age;
}
public int compareTo(Student st){
if(age==st.age)
return 0;
else if(age>st.age)
return 1;
else
return -1;
}
}
class Main{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(256,"Atul",56));
al.add(new Student(289,"Aquib",34));
al.add(new Student(198,"Jaya",61));
Collections.sort(al);
for(Student st:al){
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Copy code

The output will be something like this:

2022_11_comparable.jpg

What is comparator?

Comparator definition: A comparator interface provides multiple sorting sequence, and when this interface is used, it does not affects the actual class or the original class. 

In layman’s terms, in Java, comparator interface allows an object to compare different objects of different classes. You can use this interface primarily when you require a unique sorting order for configurable objects that is not dependent on numerous fields. For a better understanding of comparator interfaces, let’s go through an example in the next section of this article.

You can also explore:This keyword in Java

Comparator example

Here’s an exapmle of a comparator interface:

 
import java.io.*;
import java.lang.*;
import java.util.*;
class Student {
int rollno;
String name, address;
public Student(int rollno, String name, String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}
public String toString()
{
return this.rollno + " " + this.name + " "
+ this.address;
}
}
class Sortbyroll implements Comparator {
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
class Sortbyname implements Comparator {
public int compare(Student a, Student b)
{
return a.name.compareTo(b.name);
}
}
class Main {
public static void main(String[] args)
{
ArrayList ar = new ArrayList();
ar.add(new Student(234, "Anshuman", "Lucknow"));
ar.add(new Student(345, "Atul", "Noida"));
ar.add(new Student(734, "Aquib", "Shilong"));
ar.add(new Student(563, "Jaya", "Delhi"));
System.out.println("Unsorted");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
Collections.sort(ar, new Sortbyroll());
System.out.println("\nSorted by rollno");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
Collections.sort(ar, new Sortbyname());
System.out.println("\nSorted by name");
for (int i = 0; i < ar.size(); i++)
System.out.println(ar.get(i));
}
}
Copy code

The output will be something like this:

2022_11_comparator.jpg

Key differences between comparable vs comparator

Here are some of the key differences between comparable vs comparator:

  • The comparable interface uses the compareTo() method to sort, whereas the comparator interface uses the compare() method.
  • A comparable interface can be found in java.lang package, whereas the comparator interface, can be found in java.util package.
  • A comparable interface modifies the actual or the original class. But, the comparator interface does not modify the actual or the original class.
  • The comparable interface sorts the data according to natural sorting order, whereas the comparator interface sorts the data according to customized sorting order.

You can also explore: Free Java Courses

Conclusion

Now that you know comparable vs comparator interfaces, you will be able to code more smoothly. If you have any queries, please feel free to drop your query through the comment. We will be happy to help!

You can also explore other differece between articles, such as:

Difference Between Class and Interface
Difference Between Class and Interface
The main difference between class and interface is that a class describes the behavior of an object. In contrast, an interface contains the behaviors assigned and defined by the class....read more
Difference between Abstract class and Interface in Java
Difference between Abstract class and Interface in Java
Both abstract class and interface in java helps in achieving abstraction. They are similar in nature yet differ from each other when it comes to their usage. Let’s see what...read more
Difference Between Fraud and Misrepresentation
Difference Between Fraud and Misrepresentation
The main difference between fraud and misrepresentation is that fraud happens when a person or a party intentionally and willfully represents false information to deceive another party. In contrast, misrepresentation...read more

FAQs

What is the main difference between comparable vs comparator?

The main difference between comparable vs comparator is that comparable provides a single sorting sequence, whereas comparator provides multiple sorting sequences.

In regards to comparable vs comparator, what is comparable?

In terms of comparable vs comparator, a comparable interface provides a single sorting sequence, and when this interface is used, it affects the actual class or the original class.

In regards to comparable vs comparator, what is a comparator?

In terms of comparable vs comparator, a comparator interface provides multiple sorting sequence, and when this interface is used, it does not affect the actual class or the original class.

Regarding comparable vs comparator, which interface uses the compareTo() method and which uses the compare() method?

In terms of comparable vs comparator, the comparable interface uses the compareTo() method, and the comparator interface uses the compare() method.

Regarding comparable vs comparator, which interface sorts the data according to the natural sorting order and which interface sorts the data according to the customized sorting order?

In terms of comparable vs comparator, the comparable interface sorts the data according to the natural sorting order and the comparator interface sorts the data according to the customized sorting order.

About the Author
author-image
Anshuman Singh
Senior Executive - Content

Anshuman Singh is an accomplished content writer with over three years of experience specializing in cybersecurity, cloud computing, networking, and software testing. Known for his clear, concise, and informative wr... Read Full Bio