Comparator Vs Comparable
Advance Java
Comparator and Comparable are two interfaces in Java used to compare objects. They are essential for sorting objects in collections such as ArrayList, TreeSet, or TreeMap.
1. Comparable Interface
Comparable is an interface in the java.lang package used to define the natural ordering of objects. A class that implements Comparable must override the compareTo() method.
public int compareTo(T obj);Returns:
- A negative integer if the current object is less than the specified object.
- Zero if the current object is equal to the specified object.
- A positive integer if the current object is greater than the specified object.
Example:
import java.util.*;
class Student implements Comparable<Student> {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public int compareTo(Student other) {
return this.id - other.id; // Sort by id in ascending order
}
@Override
public String toString() {
return "Student{id=" + id + ", name='" + name + "'}";
}
}
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student(3, "Alice"));
students.add(new Student(1, "Bob"));
students.add(new Student(2, "Charlie"));
Collections.sort(students); // Uses compareTo method
System.out.println(students);
}
}
/*
[Student{id=1, name='Bob'}, Student{id=2, name='Charlie'}, Student{id=3, name='Alice'}]
*/
2. Comparator Interface
Comparator is an interface in the java.util package that provides multiple ways to compare objects. It is more flexible than Comparable because the comparison logic is external to the object being compared.
public int compare(T obj1, T obj2);Returns:
- A negative integer if
obj1is less thanobj2. - Zero if
obj1is equal toobj2. - A positive integer if
obj1is greater thanobj2.
Get Mayesha Mariza’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
import java.util.*;
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Student{id=" + id + ", name='" + name + "'}";
}
}
class NameComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s1.name.compareTo(s2.name); // Sort by name in alphabetical order
}
}
class IdComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s1.id - s2.id; // Sort by id in ascending order
}
}
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student(3, "Alice"));
students.add(new Student(1, "Bob"));
students.add(new Student(2, "Charlie"));
// Sort by name
Collections.sort(students, new NameComparator());
System.out.println("Sorted by Name: " + students);
// Sort by id
Collections.sort(students, new IdComparator());
System.out.println("Sorted by ID: " + students);
}
}
/*
Sorted by Name: [Student{id=3, name='Alice'}, Student{id=1, name='Bob'}, Student{id=2, name='Charlie'}]
Sorted by ID: [Student{id=1, name='Bob'}, Student{id=2, name='Charlie'}, Student{id=3, name='Alice'}]
*/
Key Differences
Comparator:
- java.util
- compare(T obj1, T obj2)
- Implemented as a separate class
- Multiple custom orderings
- Sorting by different criteria (e.g., by Name, Age)
- When multiple sorting strategies are required
Comparable:
- java.lang
- compareTo(T obj)
- Implemented in the class itself
- Single natural ordering
- Sorting by default (e.g., by ID)
- When objects have a natural order









