In Java, comparator() returns the Comparator sorting the TreeSet, or null when it uses natural ordering (Comparable).
Checking comparator() lets you reuse a set's ordering rule when creating a second TreeSet and explains why descendingSet() always has a non-null comparator.
An instance method of java.util.TreeSet (Java 1.2+): a red-black tree NavigableSet backed by a TreeMap that keeps unique elements sorted in O(log n) time and is not thread-safe.
Syntax
Call comparator() on any TreeSet or SortedSet.
Java
TreeSet<Integer> natural = new TreeSet<>();
Comparator<? super Integer> c1 = natural.comparator(); // null
TreeSet<Integer> desc = new TreeSet<>(Comparator.reverseOrder());
Comparator<? super Integer> c2 = desc.comparator(); // reverseOrder comparator
Call
What it does
set.comparator()
Returns the custom Comparator, or null for natural order.
new TreeSet<>(set.comparator())
Creates an empty set with the same sort rule (even when null).
set.descendingSet().comparator()
Always non-null: returns the reversed comparator.
Comparator.nullsFirst(c)
Wraps a comparator so null elements are allowed.
Passing null to new TreeSet<>((Comparator<T>) null) selects natural ordering—so new TreeSet<>(other.comparator()) works for both custom and natural sets.
Library declarations
Java
// java.util.TreeSet<E>
public Comparator<? super E> comparator()
// Declared in SortedSet<E>
Parameters
Parameter
Description
None
comparator() takes no parameters.
Return Value
Return Value
Description
Comparator<? super E>
The comparator passed at construction (or reversed comparator on descendingSet()).
null
If the set uses the elements' natural ordering.
Compare with Similar Methods
Constructors and methods related to TreeSet ordering:
One constructor handles both. Passing template.comparator() works whether it is a custom comparator or null.
Descending rule preserved. Adding 9 and 5 to filtered outputs [9, 5] because it inherited reverseOrder().
Natural rule preserved too. When nat.comparator() returns null, new TreeSet<>((Comparator) null) uses natural order.
Example 3 — Avoid Dropping Equal-Length Words With thenComparing()
See how a length-only comparator collapses equal-length words, and fix it with thenComparing().
Java
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetComparatorTieBreak {
public static void main(String[] args) {
TreeSet<String> byLength = new TreeSet<>(Comparator.comparingInt(String::length));
byLength.addAll(Arrays.asList("pear", "fig", "plum", "kiwi"));
System.out.println("length only (drops plum, kiwi): " + byLength);
@SuppressWarnings("unchecked")
Comparator<String> current = (Comparator<String>) byLength.comparator();
Comparator<String> full = current.thenComparing(Comparator.naturalOrder());
TreeSet<String> fixed = new TreeSet<>(full);
fixed.addAll(Arrays.asList("pear", "fig", "plum", "kiwi"));
System.out.println("with tie-breaker: " + fixed);
}
}
Output
length only (drops plum, kiwi): [fig, pear]
with tie-breaker: [fig, kiwi, pear, plum]
How It Works
Equal length equals zero. Under comparingInt(String::length), "plum" and "kiwi" both have length 4 like "pear", so they are dropped as duplicates.
Chain a tie-breaker. Calling .thenComparing(Comparator.naturalOrder()) on the retrieved comparator breaks length ties alphabetically.
All four words kept. Now fixed sorts by length first (fig) and alphabetically within length 4 (kiwi, pear, plum).
Frequently Asked Questions
null means the TreeSet was created without a custom Comparator and uses the elements natural ordering (Comparable).
Pass original.comparator() to the new TreeSet constructor, or pass the SortedSet itself via new TreeSet<>(original).
Because descendingSet() reverses the natural order (or custom comparator), so it returns a non-null reverse Comparator.
TreeSet treats the second object as a duplicate and ignores it on add(). Add thenComparing() if you only want truly equal elements treated as duplicates.
No. The comparator is fixed at creation time; create a new TreeSet with the new comparator and call addAll(oldSet).