This interface takes the place of the Dictionary
class, which
was a totally abstract class rather than an interface.
The Map
interface provides three collection views, which
allow a map's contents to be viewed as a set of keys, collection of values,
or set of key-value mappings. The order of a map is defined as
the order in which the iterators on the map's collection views return their
elements. Some map implementations, like the TreeMap
class, make
specific guarantees as to their encounter order; others, like the
HashMap
class, do not. Maps with a defined
encounter order
are generally subtypes of the SequencedMap
interface.
Note
great care must be exercised if mutable objects are used as map
keys. The behavior of a map is not specified if the value of an object is
changed in a manner that affects equals
comparisons while the
object is a key in the map. A special case of this prohibition is that it
is not permissible for a map to contain itself as a key. While it is
permissible for a map to contain itself as a value, extreme caution is
advised: the equals
and hashCode
methods are no longer
well defined on such a map.
All general-purpose map implementation classes should provide two
"standard" constructors: a void (no arguments) constructor which creates an
empty map, and a constructor with a single argument of type Map
,
which creates a new map with the same key-value mappings as its argument.
In effect, the latter constructor allows the user to copy any map,
producing an equivalent map of the desired class. There is no way to
enforce this recommendation (as interfaces cannot contain constructors) but
all of the general-purpose map implementations in the JDK comply.
The "destructive" methods contained in this interface, that is, the
methods that modify the map on which they operate, are specified to throw
UnsupportedOperationException
if this map does not support the
operation. If this is the case, these methods may, but are not required
to, throw an UnsupportedOperationException
if the invocation would
have no effect on the map. For example, invoking the putAll(Map)
method on an unmodifiable map may, but is not required to, throw the
exception if the map whose mappings are to be "superimposed" is empty.
Some map implementations have restrictions on the keys and values they
may contain. For example, some implementations prohibit null keys and
values, and some have restrictions on the types of their keys. Attempting
to insert an ineligible key or value throws an unchecked exception,
typically NullPointerException
or ClassCastException
.
Attempting to query the presence of an ineligible key or value may throw an
exception, or it may simply return false; some implementations will exhibit
the former behavior and some will exhibit the latter. More generally,
attempting an operation on an ineligible key or value whose completion
would not result in the insertion of an ineligible element into the map may
throw an exception or it may succeed, at the option of the implementation.
Such exceptions are marked as "optional" in the specification for this
interface.
Many methods in Collections Framework interfaces are defined
in terms of the equals
method. For
example, the specification for the containsKey(Object key)
method says: "returns true
if and
only if this map contains a mapping for a key k
such that
(key==null ? k==null : key.equals(k))
." This specification should
not be construed to imply that invoking Map.containsKey
with a non-null argument key
will cause key.equals(k)
to
be invoked for any key k
. Implementations are free to
implement optimizations whereby the equals
invocation is avoided,
for example, by first comparing the hash codes of the two keys. (The
Object#hashCode()
specification guarantees that two objects with
unequal hash codes cannot be equal.) More generally, implementations of
the various Collections Framework interfaces are free to take advantage of
the specified behavior of underlying Object
methods wherever the
implementor deems it appropriate.
Some map operations which perform recursive traversal of the map may fail
with an exception for self-referential instances where the map directly or
indirectly contains itself. This includes the clone()
,
equals()
, hashCode()
and toString()
methods.
Implementations may optionally handle the self-referential scenario, however
most current implementations do not do so.
The Map.
,
Map.
, and
Map.
static factory methods provide a convenient way to create unmodifiable maps.
The Map
instances created by these methods have the following characteristics:
UnsupportedOperationException
to be thrown.
However, if the contained keys or values are themselves mutable, this may cause the
Map to behave inconsistently or its contents to appear to change.
null
keys and values. Attempts to create them with
null
keys or values result in NullPointerException
.
IllegalArgumentException
.
This interface is a member of the Java Collections Framework.
HashMap
, TreeMap
, Hashtable
, SortedMap
, Collection
, Set
Modifier and Type | Class and Description |
---|---|
public static interface |
Modifier and Type | Method and Description |
---|---|
public void | |
public default V | Returns: the new value associated with the specified key, or null if nonekey with which the specified value is to be associated key, BiFunction<? super K, ? super V, ? extends V> the remapping function to compute a value remappingFunction)Attempts to compute a mapping for the specified key and its current
mapped value, or |
public default V | Returns: the current (existing or computed) value associated with the specified key, or null if the computed value is nullkey with which the specified value is to be associated key, Function<? super K, ? extends V> the mapping function to compute a value mappingFunction)If the specified key is not already associated with a value (or is mapped
to |
public default V | Returns: the new value associated with the specified key, or null if nonekey with which the specified value is to be associated key, BiFunction<? super K, ? super V, ? extends V> the remapping function to compute a value 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 (optional operation). |
public boolean | Returns: true if this map contains a mapping for the specified
keykey whose presence in this map is to be tested key)Returns |
public boolean | Returns: true if this map maps one or more keys to the
specified valuevalue whose presence in this map is to be tested value)Returns |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the entries of the given Map a map)Map from which entries are drawn, must be non-nullReturns an unmodifiable Map containing the entries of the given Map. |
public static < the key's type K, the value's type V> Map. | |
public Set | |
public boolean | |
public default void | forEach(BiConsumer<? super K, ? super V>
The action to be performed for each entry action)Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. |
public V | |
public default V | Returns: the value to which the specified key is mapped, ordefaultValue if this map contains no mapping for the keythe key whose associated value is to be returned key, V the default mapping of the key defaultValue)Returns the value to which the specified key is mapped, or
|
public int | |
public boolean | Returns: true if this map contains no key-value mappingsReturns |
public Set | |
public default V | Returns: the new value associated with the specified key, or null if no value is associated with the keykey with which the resulting value is to be associated key, V the non-null value to be merged with the existing value
associated with the key or, if no existing value or a null value
is associated with the key, to be associated with the key value, BiFunction<? super V, ? super V, ? extends V> the remapping function to recompute a value if
present 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 (optional operation). |
public static < the K, Map 's key typethe V> MapMap 's value type | |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingthe mapping's key k1, V the mapping's value v1)Returns an unmodifiable map containing a single mapping. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2)Returns an unmodifiable map containing two mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2, K the third mapping's key k3, V the third mapping's value v3)Returns an unmodifiable map containing three mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2, K the third mapping's key k3, V the third mapping's value v3, K the fourth mapping's key k4, V the fourth mapping's value v4)Returns an unmodifiable map containing four mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2, K the third mapping's key k3, V the third mapping's value v3, K the fourth mapping's key k4, V the fourth mapping's value v4, K the fifth mapping's key k5, V the fifth mapping's value v5)Returns an unmodifiable map containing five mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2, K the third mapping's key k3, V the third mapping's value v3, K the fourth mapping's key k4, V the fourth mapping's value v4, K the fifth mapping's key k5, V the fifth mapping's value v5, K the sixth mapping's key k6, V the sixth mapping's value v6)Returns an unmodifiable map containing six mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2, K the third mapping's key k3, V the third mapping's value v3, K the fourth mapping's key k4, V the fourth mapping's value v4, K the fifth mapping's key k5, V the fifth mapping's value v5, K the sixth mapping's key k6, V the sixth mapping's value v6, K the seventh mapping's key k7, V the seventh mapping's value v7)Returns an unmodifiable map containing seven mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2, K the third mapping's key k3, V the third mapping's value v3, K the fourth mapping's key k4, V the fourth mapping's value v4, K the fifth mapping's key k5, V the fifth mapping's value v5, K the sixth mapping's key k6, V the sixth mapping's value v6, K the seventh mapping's key k7, V the seventh mapping's value v7, K the eighth mapping's key k8, V the eighth mapping's value v8)Returns an unmodifiable map containing eight mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2, K the third mapping's key k3, V the third mapping's value v3, K the fourth mapping's key k4, V the fourth mapping's value v4, K the fifth mapping's key k5, V the fifth mapping's value v5, K the sixth mapping's key k6, V the sixth mapping's value v6, K the seventh mapping's key k7, V the seventh mapping's value v7, K the eighth mapping's key k8, V the eighth mapping's value v8, K the ninth mapping's key k9, V the ninth mapping's value v9)Returns an unmodifiable map containing nine mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | Returns: aMap containing the specified mappingsthe first mapping's key k1, V the first mapping's value v1, K the second mapping's key k2, V the second mapping's value v2, K the third mapping's key k3, V the third mapping's value v3, K the fourth mapping's key k4, V the fourth mapping's value v4, K the fifth mapping's key k5, V the fifth mapping's value v5, K the sixth mapping's key k6, V the sixth mapping's value v6, K the seventh mapping's key k7, V the seventh mapping's value v7, K the eighth mapping's key k8, V the eighth mapping's value v8, K the ninth mapping's key k9, V the ninth mapping's value v9, K the tenth mapping's key k10, V the tenth mapping's value v10)Returns an unmodifiable map containing ten mappings. |
public static < the K, Map 's key typethe V> MapMap 's value type | |
public V | Returns: the previous value associated withkey , or
null if there was no mapping for key .
(A null return can also indicate that the map
previously associated null with key ,
if the implementation supports null values.)key with which the specified value is to be associated key, V value to be associated with the specified key value)Associates the specified value with the specified key in this map (optional operation). |
public void | |
public default V | Returns: the previous value associated with the specified key, ornull if there was no mapping for the key.
(A null return can also indicate that the map
previously associated null with the key,
if the implementation supports null values.)key with which the specified value is to be associated key, V value to be associated with the specified key value)If the specified key is not already associated with a value (or is mapped
to |
public V | |
public default boolean | |
public default boolean | Returns: true if the value was replacedkey with which the specified value is associated key, V value expected to be associated with the specified key oldValue, V value to be associated with the specified key newValue)Replaces the entry for the specified key only if currently mapped to the specified value (optional operation). |
public default V | Returns: the previous value associated with the specified key, ornull if there was no mapping for the key.
(A null return can also indicate that the map
previously associated null with the key,
if the implementation supports null values.)key with which the specified value is associated key, V value to be associated with the specified key value)Replaces the entry for the specified key only if it is currently mapped to some value (optional operation). |
public default void | replaceAll(BiFunction<? super K, ? super V, ? extends V>
the function to apply to each entry function)Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception (optional operation). |
public int | Returns: the number of key-value mappings in this mapReturns the number of key-value mappings in this map. |
public Collection | Returns: a collection view of the values contained in this mapReturns a |