Module java.base
Package java.util

Class TreeMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.TreeMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>, NavigableMap<K,V>, SortedMap<K,V>

public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializable
A multi-key node Red-Black tree based NavigableMap implementation.
See Also:
  • Nested Class Summary

    Nested classes/interfaces declared in class java.util.AbstractMap

    AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>

    Nested classes/interfaces declared in interface java.util.Map

    Map.Entry<K,V>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new empty instance of TreeMap.
    TreeMap(Comparator<? super K> theComparator)
    Constructs a new empty instance of TreeMap which uses the specified Comparator.
    TreeMap(Map<? extends K,? extends V> map)
    Constructs a new instance of TreeMap containing the mappings from the specified Map and using the natural ordering.
    TreeMap(SortedMap<K,? extends V> map)
    Constructs a new instance of TreeMap containing the mappings from the specified SortedMap and using the same Comparator.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.
    Returns the least key greater than or equal to the given key, or null if there is no such key.
    void
    Removes all mappings from this TreeMap, leaving it empty.
    Answers a new TreeMap with the same mappings, size and comparator as this TreeMap.
    Comparator<? super K>
    Answers the Comparator used to compare elements in this TreeMap.
    boolean
    Searches this TreeMap for the specified key.
    boolean
    Searches this TreeMap for the specified value.
    Returns a reverse order NavigableSet view of the keys contained in this map.
    Returns a reverse order view of the mappings contained in this map.
    Returns a Set view of the mappings contained in this map.
    Returns a key-value mapping associated with the least key in this map, or null if the map is empty.
    Answer the first sorted key in this TreeMap.
    Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.
    floorKey(K key)
    Returns the greatest key less than or equal to the given key, or null if there is no such key.
    get(Object key)
    Answers the value of the mapping with the specified key.
    headMap(K toKey)
    Returns a view of the portion of this map whose keys are strictly less than toKey.
    headMap(K toKey, boolean inclusive)
    Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.
    Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.
    higherKey(K key)
    Returns the least key strictly greater than the given key, or null if there is no such key.
    Answers a Set of the keys contained in this TreeMap.
    Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
    Answer the last sorted key in this TreeMap.
    Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
    lowerKey(K key)
    Returns the greatest key strictly less than the given key, or null if there is no such key.
    Returns a NavigableSet view of the keys contained in this map.
    Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
    Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
    put(K key, V value)
    Maps the specified key to the specified value.
    void
    putAll(Map<? extends K,? extends V> map)
    Copies every mapping in the specified Map to this TreeMap.
    Removes a mapping with the specified key from this TreeMap.
    int
    Answers the number of mappings in this TreeMap.
    subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
    Returns a view of the portion of this map whose keys range from fromKey to toKey.
    subMap(K fromKey, K toKey)
    Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
    tailMap(K fromKey)
    Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
    tailMap(K fromKey, boolean inclusive)
    Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.
    Returns a Collection view of the values contained in this map.

    Methods declared in class java.util.AbstractMap

    equals, hashCode, isEmpty, toString, values

    Methods declared in class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TreeMap

      public TreeMap()
      Constructs a new empty instance of TreeMap.
    • TreeMap

      public TreeMap(Comparator<? super K> theComparator)
      Constructs a new empty instance of TreeMap which uses the specified Comparator.
      Parameters:
      theComparator - the Comparator
    • TreeMap

      public TreeMap(Map<? extends K,? extends V> map)
      Constructs a new instance of TreeMap containing the mappings from the specified Map and using the natural ordering.
      Parameters:
      map - the mappings to add
      Throws:
      ClassCastException - when a key in the Map does not implement the Comparable interface, or they keys in the Map cannot be compared
    • TreeMap

      public TreeMap(SortedMap<K,? extends V> map)
      Constructs a new instance of TreeMap containing the mappings from the specified SortedMap and using the same Comparator.
      Parameters:
      map - the mappings to add
  • Method Details

    • clear

      public void clear()
      Removes all mappings from this TreeMap, leaving it empty.
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
      See Also:
    • clone

      public Object clone()
      Answers a new TreeMap with the same mappings, size and comparator as this TreeMap.
      Overrides:
      clone in class AbstractMap<K,V>
      Returns:
      a shallow copy of this TreeMap
      See Also:
    • comparator

      public Comparator<? super K> comparator()
      Answers the Comparator used to compare elements in this TreeMap.
      Specified by:
      comparator in interface SortedMap<K,V>
      Returns:
      a Comparator or null if the natural ordering is used
    • containsKey

      public boolean containsKey(Object key)
      Searches this TreeMap for the specified key.
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
      Parameters:
      key - the object to search for
      Returns:
      true if key is a key of this TreeMap, false otherwise
      Throws:
      ClassCastException - when the key cannot be compared with the keys in this TreeMap
      NullPointerException - when the key is null and the comparator cannot handle null
    • containsValue

      public boolean containsValue(Object value)
      Searches this TreeMap for the specified value.
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
      Parameters:
      value - the object to search for
      Returns:
      true if value is a value of this TreeMap, false otherwise
    • firstKey

      public K firstKey()
      Answer the first sorted key in this TreeMap.
      Specified by:
      firstKey in interface SortedMap<K,V>
      Returns:
      the first sorted key
      Throws:
      NoSuchElementException - when this TreeMap is empty
    • get

      public V get(Object key)
      Answers the value of the mapping with the specified key.
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
      Parameters:
      key - the key
      Returns:
      the value of the mapping with the specified key
      Throws:
      ClassCastException - when the key cannot be compared with the keys in this TreeMap
      NullPointerException - when the key is null and the comparator cannot handle null
    • keySet

      public Set<K> keySet()
      Answers a Set of the keys contained in this TreeMap. The set is backed by this TreeMap so changes to one are reflected by the other. The set does not support adding.
      Specified by:
      keySet in interface Map<K,V>
      Specified by:
      keySet in interface SortedMap<K,V>
      Overrides:
      keySet in class AbstractMap<K,V>
      Returns:
      a Set of the keys
    • lastKey

      public K lastKey()
      Answer the last sorted key in this TreeMap.
      Specified by:
      lastKey in interface SortedMap<K,V>
      Returns:
      the last sorted key
      Throws:
      NoSuchElementException - when this TreeMap is empty
    • put

      public V put(K key, V value)
      Maps the specified key to the specified value.
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
      Parameters:
      key - the key
      value - the value
      Returns:
      the value of any previous mapping with the specified key or null if there was no mapping
      Throws:
      ClassCastException - when the key cannot be compared with the keys in this TreeMap
      NullPointerException - when the key is null and the comparator cannot handle null
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
      Copies every mapping in the specified Map to this TreeMap.
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class AbstractMap<K,V>
      Parameters:
      map - the Map to copy mappings from
      Throws:
      ClassCastException - when a key in the Map cannot be compared with the keys in this TreeMap
      NullPointerException - when a key in the Map is null and the comparator cannot handle null
    • remove

      public V remove(Object key)
      Removes a mapping with the specified key from this TreeMap.
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class AbstractMap<K,V>
      Parameters:
      key - the key of the mapping to remove
      Returns:
      the value of the removed mapping or null if key is not a key in this TreeMap
      Throws:
      ClassCastException - when the key cannot be compared with the keys in this TreeMap
      NullPointerException - when the key is null and the comparator cannot handle null
    • size

      public int size()
      Answers the number of mappings in this TreeMap.
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<K,V>
      Returns:
      the number of mappings in this TreeMap
    • firstEntry

      public Map.Entry<K,V> firstEntry()
      Returns a key-value mapping associated with the least key in this map, or null if the map is empty.
      Specified by:
      firstEntry in interface NavigableMap<K,V>
      Returns:
      an entry with the least key, or null if this map is empty
      Since:
      1.6
      See Also:
    • lastEntry

      public Map.Entry<K,V> lastEntry()
      Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
      Specified by:
      lastEntry in interface NavigableMap<K,V>
      Returns:
      an entry with the greatest key, or null if this map is empty
      Since:
      1.6
      See Also:
    • pollFirstEntry

      public Map.Entry<K,V> pollFirstEntry()
      Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
      Specified by:
      pollFirstEntry in interface NavigableMap<K,V>
      Returns:
      the removed first entry of this map, or null if this map is empty
      Since:
      1.6
      See Also:
    • pollLastEntry

      public Map.Entry<K,V> pollLastEntry()
      Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
      Specified by:
      pollLastEntry in interface NavigableMap<K,V>
      Returns:
      the removed last entry of this map, or null if this map is empty
      Since:
      1.6
      See Also:
    • higherEntry

      public Map.Entry<K,V> higherEntry(K key)
      Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.
      Specified by:
      higherEntry in interface NavigableMap<K,V>
      Parameters:
      key - the key
      Returns:
      an entry with the least key greater than key, or null if there is no such key
      Since:
      1.6
      See Also:
    • higherKey

      public K higherKey(K key)
      Returns the least key strictly greater than the given key, or null if there is no such key.
      Specified by:
      higherKey in interface NavigableMap<K,V>
      Parameters:
      key - the key
      Returns:
      the least key greater than key, or null if there is no such key
      Since:
      1.6
      See Also:
    • lowerEntry

      public Map.Entry<K,V> lowerEntry(K key)
      Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
      Specified by:
      lowerEntry in interface NavigableMap<K,V>
      Parameters:
      key - the key
      Returns:
      an entry with the greatest key less than key, or null if there is no such key
      Since:
      1.6
      See Also:
    • lowerKey

      public K lowerKey(K key)
      Returns the greatest key strictly less than the given key, or null if there is no such key.
      Specified by:
      lowerKey in interface NavigableMap<K,V>
      Parameters:
      key - the key
      Returns:
      the greatest key less than key, or null if there is no such key
      Since:
      1.6
      See Also:
    • ceilingEntry

      public Map.Entry<K,V> ceilingEntry(K key)
      Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.
      Specified by:
      ceilingEntry in interface NavigableMap<K,V>
      Parameters:
      key - the key
      Returns:
      an entry with the least key greater than or equal to key, or null if there is no such key
      Since:
      1.6
      See Also:
    • ceilingKey

      public K ceilingKey(K key)
      Returns the least key greater than or equal to the given key, or null if there is no such key.
      Specified by:
      ceilingKey in interface NavigableMap<K,V>
      Parameters:
      key - the key
      Returns:
      the least key greater than or equal to key, or null if there is no such key
      Since:
      1.6
      See Also:
    • floorEntry

      public Map.Entry<K,V> floorEntry(K key)
      Description copied from interface: NavigableMap
      Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.
      Specified by:
      floorEntry in interface NavigableMap<K,V>
      Parameters:
      key - the key
      Returns:
      an entry with the greatest key less than or equal to key, or null if there is no such key
    • floorKey

      public K floorKey(K key)
      Description copied from interface: NavigableMap
      Returns the greatest key less than or equal to the given key, or null if there is no such key.
      Specified by:
      floorKey in interface NavigableMap<K,V>
      Parameters:
      key - the key
      Returns:
      the greatest key less than or equal to key, or null if there is no such key
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Description copied from interface: SortedMap
      Returns a Set view of the mappings contained in this map. The set's iterator returns the entries in ascending key order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in interface SortedMap<K,V>
      Returns:
      a set view of the mappings contained in this map, sorted in ascending key order
    • descendingKeySet

      public NavigableSet<K> descendingKeySet()
      Description copied from interface: NavigableMap
      Returns a reverse order NavigableSet view of the keys contained in this map. The set's iterator returns the keys in descending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
      Specified by:
      descendingKeySet in interface NavigableMap<K,V>
      Returns:
      a reverse order navigable set view of the keys in this map
    • descendingMap

      public NavigableMap<K,V> descendingMap()
      Description copied from interface: NavigableMap
      Returns a reverse order view of the mappings contained in this map. The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa. If either map is modified while an iteration over a collection view of either map is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.

      The returned map has an ordering equivalent to Collections.reverseOrder(comparator()). The expression m.descendingMap().descendingMap() returns a view of m essentially equivalent to m.

      Specified by:
      descendingMap in interface NavigableMap<K,V>
      Returns:
      a reverse order view of this map
    • subMap

      public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
      Description copied from interface: NavigableMap
      Returns a view of the portion of this map whose keys range from fromKey to toKey. If fromKey and toKey are equal, the returned map is empty unless fromInclusive and toInclusive are both true. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

      The returned map will throw an IllegalArgumentException on an attempt to insert a key outside of its range, or to construct a submap either of whose endpoints lie outside its range.

      Specified by:
      subMap in interface NavigableMap<K,V>
      Parameters:
      fromKey - low endpoint of the keys in the returned map
      fromInclusive - true if the low endpoint is to be included in the returned view
      toKey - high endpoint of the keys in the returned map
      toInclusive - true if the high endpoint is to be included in the returned view
      Returns:
      a view of the portion of this map whose keys range from fromKey to toKey
    • headMap

      public NavigableMap<K,V> headMap(K toKey, boolean inclusive)
      Description copied from interface: NavigableMap
      Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

      The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

      Specified by:
      headMap in interface NavigableMap<K,V>
      Parameters:
      toKey - high endpoint of the keys in the returned map
      inclusive - true if the high endpoint is to be included in the returned view
      Returns:
      a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey
    • tailMap

      public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)
      Description copied from interface: NavigableMap
      Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

      The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

      Specified by:
      tailMap in interface NavigableMap<K,V>
      Parameters:
      fromKey - low endpoint of the keys in the returned map
      inclusive - true if the low endpoint is to be included in the returned view
      Returns:
      a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey
    • subMap

      public SortedMap<K,V> subMap(K fromKey, K toKey)
      Description copied from interface: NavigableMap
      Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. (If fromKey and toKey are equal, the returned map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

      The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

      Equivalent to subMap(fromKey, true, toKey, false).

      Specified by:
      subMap in interface NavigableMap<K,V>
      Specified by:
      subMap in interface SortedMap<K,V>
      Parameters:
      fromKey - low endpoint (inclusive) of the keys in the returned map
      toKey - high endpoint (exclusive) of the keys in the returned map
      Returns:
      a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive
    • headMap

      public SortedMap<K,V> headMap(K toKey)
      Description copied from interface: NavigableMap
      Returns a view of the portion of this map whose keys are strictly less than toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

      The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

      Equivalent to headMap(toKey, false).

      Specified by:
      headMap in interface NavigableMap<K,V>
      Specified by:
      headMap in interface SortedMap<K,V>
      Parameters:
      toKey - high endpoint (exclusive) of the keys in the returned map
      Returns:
      a view of the portion of this map whose keys are strictly less than toKey
      Throws:
      ClassCastException - if toKey is not compatible with this map's comparator (or, if the map has no comparator, if toKey does not implement Comparable). Implementations may, but are not required to, throw this exception if toKey cannot be compared to keys currently in the map.
      NullPointerException - if toKey is null and this map uses natural ordering, or its comparator does not permit null keys
      IllegalArgumentException - if this map itself has a restricted range, and toKey lies outside the bounds of the range
    • tailMap

      public SortedMap<K,V> tailMap(K fromKey)
      Description copied from interface: NavigableMap
      Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

      The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

      Equivalent to tailMap(fromKey, true).

      Specified by:
      tailMap in interface NavigableMap<K,V>
      Specified by:
      tailMap in interface SortedMap<K,V>
      Parameters:
      fromKey - low endpoint (inclusive) of the keys in the returned map
      Returns:
      a view of the portion of this map whose keys are greater than or equal to fromKey
      Throws:
      ClassCastException - if fromKey is not compatible with this map's comparator (or, if the map has no comparator, if fromKey does not implement Comparable). Implementations may, but are not required to, throw this exception if fromKey cannot be compared to keys currently in the map.
      NullPointerException - if fromKey is null and this map uses natural ordering, or its comparator does not permit null keys
      IllegalArgumentException - if this map itself has a restricted range, and fromKey lies outside the bounds of the range
    • values

      public Collection<V> values()
      Description copied from class: AbstractMap
      Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
      Specified by:
      values in interface Map<K,V>
      Specified by:
      values in interface SortedMap<K,V>
      Overrides:
      values in class AbstractMap<K,V>
      Returns:
      a collection view of the values contained in this map