Tuesday, 21 October 2025

Java Interview Question : Comparable vs Comparator in Java Collection Framework– Key Difference Explained.

 When working with Java Collections Framework, sorting is one of the most common operations. Java provides two powerful interfaces — Comparable and Comparator — that help developers define custom sorting logic for objects.

In this article, we’ll understand both interfaces, their differences, and when to use which one — a very popular Core Java Interview Question.

1. Comparable Interface

The Comparable interface is used to define the default natural sorting order of objects.
A class implements this interface to specify how its objects should be compared to each other.

public interface Comparable<T> {

    public int compareTo(T obj);

}

Example:
class Student implements Comparable<Student> {
    int id;
    String name;

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    // Defining natural sorting by id
    public int compareTo(Student s) {
        return this.id - s.id;
    }
}
Usage:
List<Student> list = new ArrayList<>();
list.add(new Student(3, "Amit"));
list.add(new Student(1, "Ravi"));
list.add(new Student(2, "Neha"));

Collections.sort(list); // uses compareTo()
Here, the sorting is based on the student id, which is the natural order.

2. Comparator Interface

The Comparator interface is used to define custom sorting logic outside the class.
This is useful when you want to sort objects in multiple different ways (e.g., by name, by id, etc.)

Syntax:

public interface Comparator<T> {

    public int compare(T obj1, T obj2);

}

Example:
class Student {
    int id;
    String name;

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

// Custom sorting by name
class NameComparator implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s1.name.compareTo(s2.name);
    }
}
Usage:
List<Student> list = new ArrayList<>();
list.add(new Student(3, "Amit"));
list.add(new Student(1, "Ravi"));
list.add(new Student(2, "Neha"));

Collections.sort(list, new NameComparator());

Java 8 Example using Lambda Expression

With Java 8, Comparator can be used in a more concise way:

Collections.sort(list, (s1, s2) -> s1.name.compareTo(s2.name));

Or simply:
list.sort(Comparator.comparing(Student::getName));

When to Use Which?

  • Use Comparable when you want natural sorting (e.g., sorting students by roll number).

  • Use Comparator when you want multiple sorting criteria (e.g., sort by name, then by marks, etc.).


Conclusion

Both Comparable and Comparator are essential for sorting custom objects in Java.

  • Comparable provides default natural order.

  • Comparator offers flexibility and customization for multiple sorting strategies.

By mastering these interfaces, you’ll be well-prepared for one of the most commonly asked Core Java interview questions.

Author:

Trilochan Tarai
Founder of java8s.com | Assistant Professor (CSE) at SoA University | Silan Software Pvt. Ltd.

No comments:

Post a Comment