CKTIS / Java
Comparator.comparing — compare objects by a key
Build an object comparator from a key extractor. Use the keys’ natural order or supply a separate comparator to define their order, including where `null` keys belong.
Version: Java SE 21
Syntax
import java.util.Comparator;
import java.util.function.Function;
// Static method signatures on java.util.Comparator (Java SE 21)
static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor)
static <T, U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)In Java SE 21, Comparator.comparing is a static method on the java.util.Comparator interface. Comparator.comparing() is shorthand for the method name in prose, not a valid zero-argument call. The method returns a Comparator<T>; it does not sort anything itself. When comparing two objects of type T, the returned comparator extracts a key of type U from each object and compares those keys.
The one-argument overload uses the keys’ natural order, so U must implement Comparable<? super U>. The method reference Ticket::getId is the key extractor: given a Ticket, it returns its String ID. The natural order of String is lexicographic, not numeric, so T10 comes before T2.
The two-argument overload uses the supplied comparator for the keys and does not require U to implement Comparable. In either overload, passing null as a method argument throws NullPointerException. A key returned by the extractor is different: the one-argument overload does not handle null keys. With the two-argument overload, Comparator.nullsLast(Comparator.naturalOrder()) places null keys after non-null keys. This does not protect against a null list element: Ticket::getId would still try to call a method on null. If elements can also be null, wrap the entire Ticket comparator in Comparator.nullsLast.
Examples
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.StringJoiner;
public class Main {
static final class Ticket {
private final String id;
Ticket(String id) {
this.id = id;
}
String getId() {
return id;
}
}
private static void printIds(List<Ticket> tickets) {
StringJoiner ids = new StringJoiner(", ", "[", "]");
for (Ticket ticket : tickets) {
ids.add(String.valueOf(ticket.getId()));
}
System.out.println(ids.toString());
}
public static void main(String[] args) {
List<Ticket> tickets = new ArrayList<>(List.of(
new Ticket("T2"), new Ticket("T10")));
Comparator<Ticket> byId = Comparator.comparing(Ticket::getId);
tickets.sort(byId);
printIds(tickets);
tickets.add(0, new Ticket(null));
Comparator<Ticket> byIdNullsLast = Comparator.comparing(
Ticket::getId, Comparator.nullsLast(Comparator.naturalOrder()));
tickets.sort(byIdNullsLast);
printIds(tickets);
}
}Result
[T10, T2] [T10, T2, null]
`List.sort` is what changes the list order. The first sort uses the natural order of the `String` IDs. Before the second sort, a `Ticket` with a `null` ID is inserted at the front; the second sort moves it to the end.
Related lessons
- Java · Lesson 42 · Basic · Version: 2
- Java · Lesson 48 · Basic · Version: 2
- Java · Lesson 35 · Intermediate · Version: 2