In Java, comparator() returns the Comparator a TreeMap uses to order its keys. If the map was created without one, it uses the keys' natural ordering (compareTo) and the method returns null.
The method comes from the SortedMap interface. It returns the very object passed to the constructor, so you can compare it with == or reuse it to compare keys the same way the map does.
An instance method of java.util.TreeMap, part of TreeMap since Java 1.2. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe.
Syntax
No arguments; returns the comparator or null.
Java
TreeMap<Integer, String> natural = new TreeMap<>();
TreeMap<Integer, String> reversed = new TreeMap<>(Comparator.reverseOrder());
natural.comparator(); // null
reversed.comparator(); // the reverse-order comparator
Call
What it does
map.comparator() == null
true for natural ordering.
map.comparator().compare(a, b)
Compares two keys like the map does (only if not null).
new TreeMap<>(sortedMap)
Copies and keeps the comparator.
Do not print a comparator: its toString() is an internal class name and differs between JDKs. Test it with == null or == against a known comparator instead.
Library declarations
Java
// java.util.TreeMap<K, V>
public Comparator<? super K> comparator()
// Declared in SortedMap<K, V>
Parameters
Parameter
Description
None
comparator() takes no parameters.
Return Value
Return Value
Description
Comparator<? super K>
The comparator given to the constructor (or copied from a SortedMap).
Example 1 — Tell Natural and Custom Ordering Apart
Two lap-time maps, one natural and one reversed, filled with the same keys.
Java
import java.util.Comparator;
import java.util.TreeMap;
public class TreeMapComparatorCheck {
public static void main(String[] args) {
TreeMap<Integer, String> natural = new TreeMap<>();
TreeMap<Integer, String> reversed = new TreeMap<>(Comparator.reverseOrder());
for (int lap = 1; lap <= 3; lap++) {
natural.put(lap, "lap " + lap);
reversed.put(lap, "lap " + lap);
}
System.out.println("natural comparator is null: " + (natural.comparator() == null));
System.out.println("reversed comparator is null: " + (reversed.comparator() == null));
System.out.println("natural first: " + natural.firstKey() + ", reversed first: " + reversed.firstKey());
}
}
Output
natural comparator is null: true
reversed comparator is null: false
natural first: 1, reversed first: 3
How It Works
No comparator given.natural relies on Integer.compareTo, so comparator() reports null.
A comparator was given.reversed holds the Comparator.reverseOrder() object, so the result is not null.
Visible effect. The same keys 1 to 3 start at 1 in one map and at 3 in the other, because "first" means "smallest by the comparator".
Example 2 — Compare Titles Exactly Like the Map Does
A bookshelf ignores case. Fetch its comparator and use it outside the map.
Java
import java.util.Comparator;
import java.util.TreeMap;
public class TreeMapComparatorReuse {
public static void main(String[] args) {
TreeMap<String, Integer> shelf = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
shelf.put("dune", 1965);
shelf.put("Emma", 1815);
System.out.println(shelf);
Comparator<? super String> order = shelf.comparator();
System.out.println("same object: " + (order == String.CASE_INSENSITIVE_ORDER));
System.out.println("\"apple\" before \"Banana\": " + (order.compare("apple", "Banana") < 0));
System.out.println("\"DUNE\" equals \"dune\" here: " + (order.compare("DUNE", "dune") == 0));
}
}
Output
{dune=1965, Emma=1815}
same object: true
"apple" before "Banana": true
"DUNE" equals "dune" here: true
How It Works
Shelf order ignores case. dune prints before Emma because d comes before e once case is ignored; natural order would put Emma first.
The same instance.comparator() hands back exactly String.CASE_INSENSITIVE_ORDER, so == is true.
Consistent decisions. Using order.compare elsewhere, "apple" sorts before "Banana" and "DUNE" counts as the same title as "dune", just as inside the map.
Example 3 — Which Copy Keeps the Comparator?
Copy a case-insensitive map twice: once as a TreeMap and once through a Map variable.
Constructor chosen at compile time.relaxed is declared as a TreeMap, so Java picks the TreeMap(SortedMap) constructor, which copies the comparator.
A Map variable hides it.asMap has the static type Map, so TreeMap(Map) is called; that constructor always starts with natural ordering.
The order shows it. The second copy prints Cherry before banana, because uppercase C sorts before lowercase b in natural String order.
Frequently Asked Questions
null. Natural ordering means the keys' own compareTo method is used.
You can, but the text is an internal class name that differs between JDK versions. Compare it with == null or with a known comparator instead.
Only when otherMap's compile-time type is SortedMap or TreeMap. Through a Map-typed variable, the TreeMap(Map) constructor is used and the copy is naturally ordered.
Yes. A clone uses the same comparator object as the original.
No. It is fixed at construction. Create a new TreeMap with the new comparator and putAll the old entries.