The TreeMap
class in Java is a part of the Java Collections Framework and implements the NavigableMap
interface. It is a Red-Black tree-based implementation that provides efficient means of sorting and maintaining key-value pairs in natural order. This makes it an excellent choice when sorted order of entries is required.
This guide covers various methods available in the TreeMap
class, offering a comprehensive understanding of how to manipulate and interact with tree maps in Java. These methods are crucial for efficient coding practices and help in performing operations like adding, removing, and checking for key-value pairs, along with navigating the map.
For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java Collections Tutorial.
TreeMap Class Methods
Method | Description |
---|---|
put() | Adds a key-value pair to the TreeMap. |
remove() | Removes the key-value pair for a specific key. |
get() | Retrieves the value associated with a specific key. |
containsKey() | Checks if the TreeMap contains a specific key. |
containsValue() | Checks if the TreeMap contains a specific value. |
size() | Returns the number of key-value pairs in the TreeMap. |
firstKey() | Returns the first (lowest) key in the TreeMap. |
lastEntry() | Returns a key-value mapping associated with the greatest key in the TreeMap. |
subMap() | Returns a view of the portion of this map whose keys range from fromKey to toKey. |
headMap() | Returns a view of the portion of this map whose keys are less than toKey. |
tailMap() | Returns a view of the portion of this map whose keys are greater than or equal to fromKey. |
values() | Returns a collection view of the values contained in the TreeMap. |
keySet() | Returns a set view of the keys contained in the TreeMap. |
ceilingKey() | Returns the least key greater than or equal to the given key, or null if there is no such key. |
floorEntry() | Returns a key-value mapping associated with the greatest key less than or equal to the given key. |
lowerKey() | Returns the greatest key less than the given key, or null if there is no such key. |
higherEntry() | Returns a key-value mapping associated with the least key strictly greater than the given key. |
pollLastEntry() | Removes and returns a key-value mapping associated with the greatest key in this map. |
navigableKeySet() | Returns a NavigableSet view of the keys contained in this map. |
putAll() | Copies all of the mappings from the specified map to this map. |
merge() | Merges the value for a key using a given mapping function if the key is already present. |
descendingMap() | Returns a reverse order view of the mappings contained in this map. |
descendingKeySet() | Returns a reverse order NavigableSet view of the keys contained in this map. |
computeIfPresent() | Computes a new value for the specified key if present in the map. |
ceilingEntry() | Returns a key-value mapping associated with the least key greater than or equal to the given key. |
floorKey() | Returns the greatest key less than or equal to the given key, or null if there is no such key. |
clear() | Removes all key-value pairs from the TreeMap. |
firstEntry() | Returns a key-value mapping associated with the least key in the TreeMap. |
putFirst() | Puts a key-value pair at the first position in the TreeMap. |
putLast() | Puts a key-value pair at the last position in the TreeMap. |
lowerEntry() | Returns a key-value mapping associated with the greatest key less than the given key. |
References: