The `Comparable` and `Comparator` interfaces in Java are used for comparing objects to determine
their ordering. Both are part of the `java.util` package and provide mechanisms for sorting objects
based on specific criteria. Here’s a detailed explanation of each:
1. Comparable Interface
The `Comparable` interface is used to define the natural ordering of objects of a class. A class that
implements the `Comparable` interface must override the `compareTo` method, which compares the
current object with another object of the same type.
# Syntax
public interface Comparable<T> {
int compareTo(T o);
# Key Points
Single Sort Sequence The `Comparable` interface defines a single method, `compareTo(T o)`, which
must be implemented by the class that implements it.
Natural Ordering This is typically used when objects have a natural order (like alphabetical order for
strings or numerical order for numbers).
Modifying ClassesTo use `Comparable`, you have to modify the class whose objects you want to sort.
# Example of Using Comparable
Here's an example of a `Student` class that implements the `Comparable` interface to sort by age:
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
public int getAge() {
return age;
@Override
public int compareTo(Student other) {
return Integer.compare(this.age, other.age);
@Override
public String toString() {
return "Student{name='" + name + "', age=" + age + '}';
# Usage
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 18));
students.add(new Student("Charlie", 20));
Collections.sort(students);
System.out.println(students);
2. Comparator Interface
The `Comparator` interface is used when you want to define multiple ways to compare objects.
Unlike `Comparable`, which imposes a single ordering, `Comparator` allows you to define multiple
orderings.
# Syntax
java
public interface Comparator<T> {
int compare(T o1, T o2);
// other methods like equals, reversed, etc.
# Key Points
Multiple Sort SequencesYou can define multiple sorting sequences with different `Comparator`
instances.
No Need to Modify ClassesUnlike `Comparable`, the class whose objects you want to sort does not
need to implement `Comparator`.
Lambda ExpressionsIn Java 8 and above, `Comparator` is often implemented using lambda
expressions for concise code.
# Example of Using Comparator
Here's an example of sorting the `Student` class by age and name using `Comparator`:
Comparator<Student> byAge = (Student s1, Student s2) -> Integer.compare(s1.getAge(), s2.getAge());
Comparator<Student> byName = (Student s1, Student s2) -> s1.name.compareTo(s2.name);
# Usage
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 22));
students.add(new Student("Bob", 18));
students.add(new Student("Charlie", 20));
students.sort(byAge);
System.out.println("Sorted by age: " + students);
students.sort(byName);
System.out.println("Sorted by name: " + students);
Differences Between Comparable and Comparator
When to Use Which?
Use `Comparable when you have a single natural ordering for the objects (e.g., alphabetic order for
names, chronological order for dates).
Use `Comparator when you need to sort objects in multiple ways or when you want to have the
sorting logic from the object itself.