java.util · java.base · since Java 1.2
TreeMap
public class TreeMap<K, V> extends AbstractMap<K, V>
implements NavigableMap<K, V>, Cloneable, SerializableRed-black-tree map with keys always sorted: O(log n) operations plus NavigableMap nearest-key and range queries hashes cannot answer.
Key methods
K firstKey() / K lastKey() | Extremes among keys — throws NoSuchElementException if empty. |
Map.Entry<K,V> firstEntry() / lastEntry() | Extremes as entries — null-returning, not throwing. |
K floorKey(K) / ceilingKey(K) | Nearest ≤ / ≥ key, or null. |
K lowerKey(K) / higherKey(K) | Nearest strictly < / > key, or null. |
Map.Entry<K,V> floorEntry(K) / ceilingEntry(K) | Nearest ≤ / ≥ entries. |
Map.Entry<K,V> pollFirstEntry() / pollLastEntry() | Retrieve-and-remove the extreme entries. |
NavigableMap<K,V> headMap/tailMap/subMap(…) | Range views over the sort order, live and write-through; boolean flags control endpoint inclusion. |
NavigableMap<K,V> descendingMap() | Reverse-order view backed by the same tree. |