Java TreeMap
In this page:
Introduction to TreeMap
TreeMap is an implementation of the Map interface that stores its key-value entries in a red-black tree, keeping the entries sorted by key at all times rather than in insertion or hash order.
Example: Introduction to TreeMap
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, Integer> map = new TreeMap<>(); // red-black tree, sorted by key
map.put("banana", 1);
map.put("apple", 2);
System.out.println(map);
}
}
Login to try C/C++/Java/PHP code in the editor
Automatic Key Sorting
Entries added to a TreeMap are automatically kept sorted by their key's natural ordering -- numeric keys sort numerically and String keys sort alphabetically -- regardless of the order they were inserted in.
Example: Automatic Key Sorting
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
map.put(3, "c");
map.put(1, "a");
map.put(2, "b");
System.out.println(map); // sorted by key: 1, 2, 3
}
}
Login to try C/C++/Java/PHP code in the editor
Adding and Removing Entries
TreeMap supports the same put, get, and remove operations as any other Map, but every insertion or removal also re-balances the underlying tree to keep the key order intact, and putting an existing key still overwrites its value.
Example: Adding and Removing Entries
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, Integer> map = new TreeMap<>();
map.put("apple", 3);
map.put("apple", 5); // overwrites
map.remove("apple");
System.out.println(map);
}
}
Login to try C/C++/Java/PHP code in the editor
Navigation Methods
TreeMap adds navigation methods beyond a plain Map, like firstKey, lastKey, higherKey, and floorKey, which use the guaranteed sort order to efficiently find the smallest, largest, or nearest keys relative to a given value.
Example: Navigation Methods
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
map.put(1, "a"); map.put(5, "b"); map.put(10, "c");
System.out.println(map.firstKey());
System.out.println(map.lastKey());
System.out.println(map.higherKey(5));
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Sort Order with Comparator
Passing a Comparator to a TreeMap's constructor overrides the default natural key ordering, letting entries be sorted by any custom rule, such as reverse order or a computed property like key length instead of alphabetical order.
Example: Custom Sort Order with Comparator
import java.util.TreeMap;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>(Collections.reverseOrder());
map.put(1, "a");
map.put(5, "b");
System.out.println(map); // reverse sorted
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting