Comparable Vs Comparator Interface
Comparable Interface
- Purpose: The Comparable interface is used to define the natural ordering of objects of a class. It allows objects to be compared to each other based on their natural order.
- Implementation: To make a class objects sortable using the natural ordering, the class must implement the Comparable interface and override the compareTo() method, which compares the current object with another object and returns a negative integer, zero, or a positive integer based on whether the current object is less than, equal to, or greater than the specified object, Comparable Vs Comparator Interface respectively.
- Usage: Comparable is typically used when the natural ordering of objects is sufficient or desired. For example, sorting integers, strings, or custom objects based on specific attributes.
Comparator Interface
- Purpose: The Comparator interface is used to define custom ordering of objects that may not have a natural ordering or when you want to sort objects based on criteria other than their natural order.
- Implementation: Comparator interface provides a way to define custom comparison logic separately from the objects being compared. It requires implementing the compare() method, which takes two objects as arguments and returns a negative integer, zero, or a positive integer based on whether the first object is less than, equal to, or greater than the second object, respectively.
- Usage: Comparator is used when you need to sort objects based on criteria other than their natural order or when you want to define multiple sorting orders for the same class.

Table of Contents
Example
Example - Comparable Interface:
java
import java.util.ArrayList;
import java.util.Collections;
// Student class implementing Comparable interface
class Student implements Comparable<Student> {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
// Implementing compareTo method for natural ordering
@Override
public int compareTo(Student other) {
return this.id - other.id;
}
// Getter methods
public int getId() {
return id;
}
public String getName() {
return name;
}
}
public class ComparableExample {
public static void main(String[] args) {
// Create a list of students
ArrayList<Student> students = new ArrayList<>();
students.add(new Student(101, "Alice"));
students.add(new Student(103, "Bob"));
students.add(new Student(102, "Charlie"));
// Sort the list using natural ordering (compareTo method)
Collections.sort(students);
// Print the sorted list
for (Student student : students) {
System.out.println("ID: " + student.getId() + ", Name: " + student.getName());
}
}
}
Example
Example - Comparator Interface:
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
// Student class without implementing Comparable interface
class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
// Getter methods
public int getId() {
return id;
}
public String getName() {
return name;
}
}
public class ComparatorExample {
public static void main(String[] args) {
// Create a list of students
ArrayList<Student> students = new ArrayList<>();
students.add(new Student(101, "Alice"));
students.add(new Student(103, "Bob"));
students.add(new Student(102, "Charlie"));
// Sort the list using a custom Comparator
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
});
// Print the sorted list
for (Student student : students) {
System.out.println("ID: " + student.getId() + ", Name: " + student.getName());
}
}
}
Comparable Vs Comparator Interface example
In the Comparable Vs Comparator Interface example, the Student class implements the Comparable interface to define the natural ordering based on student IDs. In the Comparator example, we sort the list of students based on their names using a custom Comparator.