Java TreeMap Methods
TreeMap Methods
A TreeMap stores key/value pairs, and keeps them sorted by key. The methods below use this order, for example to get the first key, the last key, or all keys before a given key.
Some methods use the type of the keys and values. The type of the keys is called K, and the type of the values is called V in the tables.
| Method | Description | Return Type |
|---|---|---|
| firstKey() | Returns the first (lowest) key in the map | K |
| lastKey() | Returns the last (highest) key in the map | K |
| firstEntry() | Returns the entry (key/value pair) with the first (lowest) key, or null if the map is empty | Map.Entry |
| lastEntry() | Returns the entry (key/value pair) with the last (highest) key, or null if the map is empty | Map.Entry |
| floorKey() | Returns the greatest key that is less than or equal to the given key, or null if there is none | K |
| ceilingKey() | Returns the least key that is greater than or equal to the given key, or null if there is none | K |
| lowerKey() | Returns the greatest key that is less than the given key, or null if there is none | K |
| higherKey() | Returns the least key that is greater than the given key, or null if there is none | K |
| headMap() | Returns a part of the map, with all keys that are less than the given key | SortedMap|NavigableMap |
| tailMap() | Returns a part of the map, with all keys that are greater than or equal to the given key | SortedMap|NavigableMap |
| subMap() | Returns a part of the map, with all keys between two given keys | SortedMap|NavigableMap |
| pollFirstEntry() | Removes and returns the entry with the first (lowest) key, or null if the map is empty | Map.Entry |
| pollLastEntry() | Removes and returns the entry with the last (highest) key, or null if the map is empty | Map.Entry |
| descendingMap() | Returns the map with the keys in reverse (descending) order | NavigableMap |
Methods Shared with HashMap
A TreeMap also has the methods below. They work the same way as in a HashMap, so the links go to the HashMap reference pages. The difference is that a TreeMap keeps the keys sorted, so methods like keySet() and forEach() give you the keys in sorted order. A TreeMap also does not allow null as a key.
| Method | Description | Return Type |
|---|---|---|
| clear() | Removes all entries from the map | void |
| clone() | Creates a copy of the map | Object |
| compute() | Computes a new value for a key, from the key and its current value | V |
| computeIfAbsent() | Computes a value for a key, only if the key is not in the map yet | V |
| computeIfPresent() | Computes a new value for a key, only if the key is already in the map | V |
| containsKey() | Checks if the map has an entry with the specified key | boolean |
| containsValue() | Checks if the map has an entry with the specified value | boolean |
| entrySet() | Returns a set of all entries (key/value pairs) in the map | Set |
| forEach() | Performs an action on every entry in the map | void |
| get() | Returns the value of the entry with the specified key | V |
| getOrDefault() | Returns the value of the entry with the specified key, or a default value if the key is not found | V |
| isEmpty() | Checks if the map is empty | boolean |
| keySet() | Returns a set of all keys in the map | Set |
| merge() | Combines a new value with the current value of a key, or adds the entry if the key is not found | V |
| put() | Adds an entry to the map, or replaces the value if the key is already there | V |
| putAll() | Adds all entries from another map to this map | void |
| putIfAbsent() | Adds an entry to the map, only if the key is not in the map yet | V |
| remove() | Removes an entry from the map | V|boolean |
| replace() | Replaces the value of an entry, only if the key is in the map | V|boolean |
| replaceAll() | Replaces the value of every entry with the result of a function | void |
| size() | Returns the number of entries in the map | int |
| values() | Returns a collection of all values in the map | Collection |