Interface Map<K,V>

All Known Subinterfaces:
NavigableMap<K,V>, SortedMap<K,V>
All Known Implementing Classes:
AbstractMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, Properties, TreeMap, WeakHashMap

public interface Map<K,V>

A Map is a data structure consisting of a set of keys and values in which each key is mapped to a single value. The class of the objects used as keys is declared when the Map is declared, as is the class of the corresponding values.

A Map provides helper methods to iterate through all of the keys contained in it, as well as various methods to access and update the key/value pairs.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Map.Entry is a key/value mapping contained in a Map.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all elements from this Map, leaving it empty.
    default V
    compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
    Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
    default V
    computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
    If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
    default V
    computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
    If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
    boolean
    Returns whether this Map contains the specified key.
    boolean
    Returns whether this Map contains the specified value.
    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 Maps contain the same mappings.
    default void
    forEach(BiConsumer<? super K, ? super V> action)
    Performs the given action for each entry in this map.
    get(Object key)
    Returns the value of the mapping with the specified key.
    default V
    getOrDefault(Object key, V defaultValue)
    Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
    int
    Returns an integer hash code for the receiver.
    boolean
    Returns whether this map is empty.
    Returns a set of the keys contained in this Map.
    default V
    merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
    If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
    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.
    default V
    putIfAbsent(K key, V value)
    If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
    Removes a mapping with the specified key from this Map.
    default boolean
    remove(Object key, Object value)
    Removes the entry for the specified key only if it is currently mapped to the specified value.
    default V
    replace(K key, V value)
    Replaces the entry for the specified key only if it is currently mapped to some value.
    default boolean
    replace(K key, V oldValue, V newValue)
    Replaces the entry for the specified key only if currently mapped to the specified value.
    default void
    replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
    Replaces each entry's value with the result of invoking the given function on that entry.
    int
    Returns the number of mappings in this Map.
    Returns a Collection of the values contained in this Map.
  • Method Details

    • clear

      void clear()

      Removes all elements from this Map, leaving it empty.

      Throws
      • UnsupportedOperationException: if removing elements from this Map is not supported.
      See also
      • #isEmpty()

      • #size()

    • containsKey

      boolean containsKey(Object key)

      Returns whether this Map contains the specified key.

      Parameters
      • key: the key to search for.
      Returns
      Returns:
      true if this map contains the specified key, false otherwise.
    • containsValue

      boolean containsValue(Object value)

      Returns whether this Map contains the specified value.

      Parameters
      • value: the value to search for.
      Returns
      Returns:
      true if this map contains the specified value, false otherwise.
    • entrySet

      Set<Map.Entry<K,V>> entrySet()

      Returns a Set containing all of the mappings in this Map. Each mapping is an instance of Map.Entry. As the Set is backed by this Map, changes in one will be reflected in the other.

      Returns

      a set of the mappings

    • equals

      boolean equals(Object object)

      Compares the argument to the receiver, and returns true if the specified object is a Map and both Maps contain the same mappings.

      Parameters
      • object: the Object to compare with this Object.
      Returns
      Overrides:
      equals in class Object
      Returns:

      boolean true if the Object is the same as this Object false if it is different from this Object.

      See also
      • #hashCode()

      • #entrySet()

    • get

      V get(Object key)

      Returns the value of the mapping with the specified key.

      Parameters
      • key: the key.
      Returns
      Returns:
      the value of the mapping with the specified key, or null if no mapping for the specified key is found.
    • hashCode

      int hashCode()

      Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.

      Returns

      the receiver's hash.

      See also
      • #equals(Object)
      Overrides:
      hashCode in class Object
    • isEmpty

      boolean isEmpty()

      Returns whether this map is empty.

      Returns
      Returns:

      true if this map has no elements, false otherwise.

      See also
      • #size()
    • keySet

      Set<K> keySet()

      Returns a set of the keys contained in this Map. The Set is backed by this Map so changes to one are reflected by the other. The Set does not support adding.

      Returns

      a set of the keys.

    • put

      V put(K key, V value)

      Maps the specified key to the specified value.

      Parameters
      • key: the key.

      • value: the value.

      Returns
      Returns:

      the value of any previous mapping with the specified key or null if there was no mapping.

      Throws
      • UnsupportedOperationException: if adding to this Map is not supported.

      • ClassCastException: @throws ClassCastException if the class of the key or value is inappropriate for this Map.

      • IllegalArgumentException: if the key or value cannot be added to this Map.

      • NullPointerException: @throws NullPointerException if the key or value is null and this Map does not support null keys or values.

    • putAll

      void putAll(Map<? extends K, ? extends V> map)

      Copies every mapping in the specified Map to this Map.

      Parameters
      • map: the Map to copy mappings from.
      Throws
      • UnsupportedOperationException: if adding to this Map is not supported.

      • ClassCastException: @throws ClassCastException if the class of a key or a value of the specified Map is inappropriate for this Map.

      • IllegalArgumentException: if a key or value cannot be added to this Map.

      • NullPointerException: @throws NullPointerException if a key or value is null and this Map does not support null keys or values.

    • remove

      V remove(Object key)

      Removes a mapping with the specified key from this Map.

      Parameters
      • key: the key of the mapping to remove.
      Returns
      Returns:

      the value of the removed mapping or null if no mapping for the specified key was found.

      Throws
      • UnsupportedOperationException: if removing from this Map is not supported.
    • size

      int size()

      Returns the number of mappings in this Map.

      Returns

      the number of mappings in this Map.

    • values

      Collection<V> values()

      Returns a Collection of the values contained in this Map. The Collection is backed by this Map so changes to one are reflected by the other. The Collection supports Collection#remove, Collection#removeAll, Collection#retainAll, and Collection#clear operations, and it does not support Collection#add or Collection#addAll operations.

      This method returns a Collection which is the subclass of AbstractCollection. The AbstractCollection#iterator method of this subclass returns a "wrapper object" over the iterator of this Map's #entrySet(). The AbstractCollection#size method wraps this Map's #size method and the AbstractCollection#contains method wraps this Map's #containsValue method.

      The collection is created when this method is called at first time and returned in response to all subsequent calls. This method may return different Collection when multiple calls to this method, since it has no synchronization performed.

      Returns

      a collection of the values contained in this map.

    • getOrDefault

      default V getOrDefault(Object key, V defaultValue)
      Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
    • putIfAbsent

      default V putIfAbsent(K key, V value)
      If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
    • remove

      default boolean remove(Object key, Object value)
      Removes the entry for the specified key only if it is currently mapped to the specified value.
    • replace

      default boolean replace(K key, V oldValue, V newValue)
      Replaces the entry for the specified key only if currently mapped to the specified value.
    • replace

      default V replace(K key, V value)
      Replaces the entry for the specified key only if it is currently mapped to some value.
    • forEach

      default void forEach(BiConsumer<? super K, ? super V> action)
      Performs the given action for each entry in this map.
    • replaceAll

      default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
      Replaces each entry's value with the result of invoking the given function on that entry.
    • computeIfAbsent

      default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
      If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
    • computeIfPresent

      default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
      If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
    • compute

      default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
    • merge

      default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
      If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the result of the given remapping function, or removes if the result is null.