JavaCollections
TreeMap
1. Introduction
TreeMap is a Map implementation that stores entries in sorted order of keys.
If you need key-value storage along with automatic sorting by key, TreeMap is a strong choice.
2. Key Features
- Stores key-value pairs
- Keys are sorted automatically
- Duplicate keys are not allowed
- Values can be duplicated
- Usually does not allow
nullkeys - Is not synchronized
Example:
import java.util.Map;
import java.util.TreeMap;
public class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new TreeMap<>();
map.put(3, "Three");
map.put(1, "One");
map.put(2, "Two");
System.out.println(map);
}
}Output:
{1=One, 2=Two, 3=Three}3. How TreeMap Works
TreeMap uses a Red-Black Tree internally.
That is why keys stay sorted automatically.
Because of this structure:
- Insertions are
O(log n) - Retrieval is
O(log n) - Deletion is
O(log n)
4. Natural Ordering of Keys
By default, keys are sorted using natural ordering.
Examples:
Integerkeys are sorted numericallyStringkeys are sorted alphabetically
TreeMap<String, Integer> marks = new TreeMap<>();
marks.put("Math", 95);
marks.put("English", 88);
marks.put("Science", 91);
System.out.println(marks);5. Common Methods
5.1 put(), get(), remove()
TreeMap<Integer, String> map = new TreeMap<>();
map.put(101, "Navin");
map.put(102, "Kiran");
System.out.println(map.get(101));
map.remove(102);5.2 firstKey() and lastKey()
System.out.println(map.firstKey());
System.out.println(map.lastKey());5.3 higherKey(), lowerKey(), ceilingKey(), floorKey()
TreeMap<Integer, String> data = new TreeMap<>();
data.put(10, "A");
data.put(20, "B");
data.put(30, "C");
System.out.println(data.higherKey(20)); // 30
System.out.println(data.lowerKey(20)); // 10
System.out.println(data.ceilingKey(25)); // 30
System.out.println(data.floorKey(25)); // 206. Iterating Over a TreeMap
for (Map.Entry<Integer, String> entry : data.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}Since keys are sorted, iteration also happens in sorted order.
7. Custom Sorting with Comparator
You can provide a custom comparator while creating the TreeMap.
import java.util.Collections;
import java.util.TreeMap;
public class Demo {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>(Collections.reverseOrder());
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
System.out.println(map);
}
}Output:
{3=Three, 2=Two, 1=One}8. null Keys and Values
TreeMap generally does not allow null keys when natural ordering is used.
TreeMap<Integer, String> map = new TreeMap<>();
map.put(null, "Value"); // exceptionHowever, null values are allowed.
map.put(1, null);9. TreeMap vs HashMap
| Feature | HashMap | TreeMap |
|---|---|---|
| Order | No guaranteed order | Sorted by keys |
| Speed | Faster on average | Slower |
| Internal structure | Hash table | Red-Black Tree |
null key | Allowed | Usually not allowed |
Use TreeMap when sorted keys matter.
10. Practical Example
import java.util.Map;
import java.util.TreeMap;
public class StudentRank {
public static void main(String[] args) {
TreeMap<Integer, String> ranks = new TreeMap<>();
ranks.put(3, "Riya");
ranks.put(1, "Navin");
ranks.put(2, "Kiran");
for (Map.Entry<Integer, String> entry : ranks.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}11. Summary
TreeMapstores key-value pairs in sorted order of keys.- It uses a Red-Black Tree internally.
- Operations usually take
O(log n)time. - It is useful when you need sorted data or navigation methods on keys.
- For custom sorting, provide a
Comparator.
Written By: Shiva Srivastava
How is this guide?
Last updated on
