Java TreeMap
The TreeMap
class in Java is part of the Java Collection Framework and implements the Map
and SortedMap
interfaces. It stores key-value pairs in sorted order based on the natural ordering of keys or by a custom comparator.
What is TreeMap?#
A TreeMap
is a Red-Black Tree-based implementation of the NavigableMap
interface. It automatically keeps the keys in sorted ascending order.
It implements:#
Map
Interface – Stores key-value pairs.SortedMap
Interface – Maintains keys in sorted order.
Here TreeMap implements NavigableMap and NavigableMap extends SortedMap, therefore TreeMap indirectly implements SortedMap interface.
SortedMap Interface#
SortedMap
is a subinterface of Map
that provides additional methods for dealing with sorted keys. A TreeMap
uses this interface to keep its keys automatically sorted in natural order (or using a custom comparator).
Characteristics of TreeMap:#
- Stores data as key-value pairs.
- Maintains keys in ascending sorted order.
- Does not allow
null
keys (throwsNullPointerException
). - Allows multiple null values.
- Not thread-safe (must be synchronized externally if used in multi-threaded environment).
- Slower than
HashMap
andLinkedHashMap
due to sorting overhead.
TreeMap Syntax#
Commonly Used Methods#
Method | Description |
---|---|
put(K key, V value) | Adds a key-value pair. |
get(Object key) | Returns value associated with the key. |
remove(Object key) | Removes the entry by key. |
containsKey(Object key) | Checks if a key exists. |
containsValue(Object value) | Checks if a value exists. |
size() | Returns number of key-value pairs. |
clear() | Removes all entries. |
firstKey() | Returns the first (lowest) key. |
lastKey() | Returns the last (highest) key. |
headMap(K toKey) | Returns view of map whose keys are less than toKey . |
tailMap(K fromKey) | Returns view of map whose keys are greater than or equal to fromKey . |
subMap(K fromKey, K toKey) | Returns view of map whose keys are in the range. |
Example: Basic TreeMap#
Output:
Explanation#
The keys are automatically sorted in ascending order.
Example: Using firstKey(), lastKey(), headMap(), tailMap(), subMap()#
Output:
Performance#
Operation | Time Complexity |
---|---|
put() | O(log n) |
get() | O(log n) |
remove() | O(log n) |
containsKey() | O(log n) |
TreeMap operations are slower than HashMap
and LinkedHashMap
because they require maintaining sorting order via a Red-Black Tree.
When to Use TreeMap#
- When sorted ordering of keys is required.
- When you need range-based queries like
subMap()
,headMap()
, etc. - When you don't care about insertion order.
Conclusion#
In this blog, we covered the TreeMap
class in Java in detail. We explored its key features, performance, and examples of important methods like put()
, get()
, firstKey()
, headMap()
, tailMap()
, and more. We also looked at how TreeMap
implements the SortedMap
interface to maintain sorted key order. Use TreeMap
when you need sorted data, and are okay with the performance trade-offs compared to HashMap
or LinkedHashMap
.