Comparable vs Comparator
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.
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
- What is comparable?
- Comparable example
- What is a comparator?
- Comparator example
- Key differences between comparable vs comparator?
- Conclusion
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 |
Best-suited IT & Software courses for you
Learn IT & Software with these high-rated online courses
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); } } }
The output will be something like this:
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)); }}
The output will be something like this:
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:
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.
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