# Interface SortedMap<K,V>

All Superinterfaces:
  `Map<K,V>`

All Known Subinterfaces:
  `NavigableMap<K,V>`

All Known Implementing Classes:
  `TreeMap`

public interface SortedMap<K,V> extends Map<K,V>
A map that has its keys ordered. The sorting is according to either the natural ordering of its keys or the ordering given by a specified comparator.

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from interface Map

  `Map.Entry<K,V>`
  Modifier and Type
  Interface
  Description
  `static interface`
  `Map.Entry<K,V>`
  `Map.Entry` is a key/value mapping contained in a `Map`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Comparator<? super K>`
  `comparator()`
  Returns the comparator used to compare keys in this sorted map.
  `K`
  `firstKey()`
  Returns the first key in this sorted map.
  `SortedMap<K,V>`
  `headMap(K endKey)`
  Returns a sorted map over a range of this sorted map with all keys that are less than the specified `endKey`.
  `K`
  `lastKey()`
  Returns the last key in this sorted map.
  `SortedMap<K,V>`
  `subMap(K startKey, K endKey)`
  Returns a sorted map over a range of this sorted map with all keys greater than or equal to the specified `startKey` and less than the specified `endKey`.
  `SortedMap<K,V>`
  `tailMap(K startKey)`
  Returns a sorted map over a range of this sorted map with all keys that are greater than or equal to the specified `startKey`.

  ### Methods inherited from interface Map

  `clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values`
  Modifier and Type
  Method
  Description
  `void`
  `clear()`
  Removes all elements from this `Map`, leaving it empty.
  `boolean`
  `containsKey(Object key)`
  Returns whether this `Map` contains the specified key.
  `boolean`
  `containsValue(Object value)`
  Returns whether this `Map` contains the specified value.
  `Set<Map.Entry<K,V>>`
  `entrySet()`
  Returns a `Set` containing all of the mappings in this `Map`.
  `boolean`
  `equals(Object object)`
  Compares the argument to the receiver, and returns `true` if the specified object is a `Map` and both `Map`s contain the same mappings.
  `V`
  `get(Object key)`
  Returns the value of the mapping with the specified key.
  `int`
  `hashCode()`
  Returns an integer hash code for the receiver.
  `boolean`
  `isEmpty()`
  Returns whether this map is empty.
  `Set<K>`
  `keySet()`
  Returns a set of the keys contained in this `Map`.
  `V`
  `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 `Map`.
  `V`
  `remove(Object key)`
  Removes a mapping with the specified key from this `Map`.
  `int`
  `size()`
  Returns the number of mappings in this `Map`.
  `Collection<V>`
  `values()`
  Returns a `Collection` of the values contained in this `Map`.

- ## Method Details

  - ### comparator

    Comparator<? super K> comparator()
    Returns the comparator used to compare keys in this sorted map.
    Returns:
      the comparator or `null` if the natural order is used.

  - ### firstKey

    K firstKey()
    Returns the first key in this sorted map.
    Returns:
      the first key in this sorted map.
    Throws:
      `NoSuchElementException` - if this sorted map is empty.

  - ### headMap

    SortedMap<K,V> headMap(K endKey)
    Returns a sorted map over a range of this sorted map with all keys that are less than the specified `endKey`. Changes to the returned sorted map are reflected in this sorted map and vice versa.

    Note: The returned map will not allow an insertion of a key outside the specified range.

    Parameters:
      `endKey` - the high boundary of the range specified.
    Returns:
      a sorted map where the keys are less than `endKey`.
    Throws:
      `ClassCastException` - if the class of the end key is inappropriate for this sorted map.
      `NullPointerException` - if the end key is `null` and this sorted map does not support `null` keys.
      `IllegalArgumentException` - if this map is itself a sorted map over a range of another map and the specified key is outside of its range.

  - ### lastKey

    K lastKey()
    Returns the last key in this sorted map.
    Returns:
      the last key in this sorted map.
    Throws:
      `NoSuchElementException` - if this sorted map is empty.

  - ### subMap

    SortedMap<K,V> subMap(K startKey, K endKey)
    Returns a sorted map over a range of this sorted map with all keys greater than or equal to the specified `startKey` and less than the specified `endKey`. Changes to the returned sorted map are reflected in this sorted map and vice versa.

    Note: The returned map will not allow an insertion of a key outside the specified range.

    Parameters:
      `startKey` - the low boundary of the range (inclusive).
      `endKey` - the high boundary of the range (exclusive),
    Returns:
      a sorted map with the key from the specified range.
    Throws:
      `ClassCastException` - if the class of the start or end key is inappropriate for this sorted map.
      `NullPointerException` - if the start or end key is `null` and this sorted map does not support `null` keys.
      `IllegalArgumentException` - if the start key is greater than the end key, or if this map is itself a sorted map over a range of another sorted map and the specified range is outside of its range.

  - ### tailMap

    SortedMap<K,V> tailMap(K startKey)
    Returns a sorted map over a range of this sorted map with all keys that are greater than or equal to the specified `startKey`. Changes to the returned sorted map are reflected in this sorted map and vice versa.

    Note: The returned map will not allow an insertion of a key outside the specified range.

    Parameters:
      `startKey` - the low boundary of the range specified.
    Returns:
      a sorted map where the keys are greater or equal to `startKey`.
    Throws:
      `ClassCastException` - if the class of the start key is inappropriate for this sorted map.
      `NullPointerException` - if the start key is `null` and this sorted map does not support `null` keys.
      `IllegalArgumentException` - if this map itself a sorted map over a range of another map and the specified key is outside of its range.