Comparable vs Comparator
Comparable is the type's own natural order: this.compareTo(other). A Comparator is a separate object that ranks two values. Sorting and sorted maps use one or the other.
Detailed Explanation
String knows how to sort itself: lexicographic order. That is Comparable. compareTo returns a negative number, zero, or a positive number. Collections.sort(list) and new TreeMap<String, V>() use that method. You implement Comparable<T> when there is one obvious order for the type (ids, timestamps, priority).
Often there is more than one order: users by name, users by created date, users by name ignoring case. Those extra orders are Comparators. You pass them to sort, to TreeMap/TreeSet, and to PriorityQueue. Comparator.comparing(User::name), thenComparing, reversed, and nullsLast build them without writing compare by hand.
The two must not fight when they are used together. A TreeSet with a comparator that returns 0 for two elements treats them as duplicates even if equals is false. The Set contract wants consistency with equals. Natural order should match equals for the same reason: a.compareTo(b) == 0 iff a.equals(b).
compareTo should be consistent with itself: sign flip when you swap arguments, transitivity. Overflow is a classic bug: return this.n - other.n overflows for large ints. Use Integer.compare(this.n, other.n).
You reach for Comparable on value types with one order. You reach for Comparator at the call site, or as public static final constants on the class (User.BY_NAME). You do not implement Comparable just to sort once in one screen; a lambda comparator is enough.
The cost of a "natural" order that is not actually natural is surprise in TreeMap and in Collections.min. The cost of an anonymous comparator copied in five places is drift. Put shared orders next to the type. Keep compareTo short and allocation-free; it runs a lot during a sort.
Code Examples
record User(String name, int age) implements Comparable<User> {
@Override
public int compareTo(User other) {
return this.name.compareTo(other.name); // natural order: name
}
}
public class Natural {
public static void main(String[] args) {
User a = new User("Cy", 9);
User b = new User("Ana", 12);
System.out.println(a.compareTo(b) > 0); // prints: true
}
}import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ByAge {
record User(String name, int age) {}
public static void main(String[] args) {
List<User> users = new ArrayList<>(List.of(
new User("Cy", 9), new User("Ana", 12)));
users.sort(Comparator.comparingInt(User::age));
System.out.println(users.get(0).name()); // prints: Cy
users.sort(Comparator.comparing(User::name));
System.out.println(users.get(0).name()); // prints: Ana
}
}Common Gotchas
`this.n - other.n` overflows
use `Integer.compare` or `Comparator.comparingInt`.
`TreeMap` uses comparator-zero, not `equals`
inconsistent orders drop keys.
`Comparator.naturalOrder()` requires `Comparable`
it will throw if you pass a type that is not.