Top Description Inners Methods
java.util

public Interface Map<K, V>

Known Direct Subinterfaces
java.util.SequencedMap, java.lang.classfile.components.ClassPrinter.MapNode, java.util.concurrent.ConcurrentMap
Known Direct Implementers
java.util.WeakHashMap, java.util.jar.Attributes, java.util.stream.Collectors.Partition, jdk.internal.util.ReferencedKeyMap, java.util.AbstractMap, java.util.Collections.UnmodifiableMap, java.util.Collections.SynchronizedMap, java.util.Collections.CheckedMap, java.util.HashMap, java.util.Hashtable, java.util.IdentityHashMap
Type Parameters
<K>
the type of keys maintained by this map
<V>
the type of mapped values
Imports
java.util.function.BiConsumer, .BiFunction, .Function, java.io.Serializable

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

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.

Unmodifiable Maps

The Map.of, Map.ofEntries, and Map.copyOf static factory methods provide a convenient way to create unmodifiable maps. The Map instances created by these methods have the following characteristics:

This interface is a member of the Java Collections Framework.

Author
Josh Bloch
Since
1.2
See Also
HashMap, TreeMap, Hashtable, SortedMap, Collection, Set

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Map.Entry<
the type of the key
K
,
the type of the value
V
>

A map entry (key-value pair).

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Removes all of the mappings from this map (optional operation).

public default V

Returns:

the new value associated with the specified key, or null if none
compute
(K
key 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 null if there is no current mapping (optional operation).

public default V

Returns:

the current (existing or computed) value associated with the specified key, or null if the computed value is null
computeIfAbsent
(K
key 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 null), attempts to compute its value using the given mapping function and enters it into this map unless null (optional operation).

public default V

Returns:

the new value associated with the specified key, or null if none
computeIfPresent
(K
key 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 key
containsKey
(Object
key whose presence in this map is to be tested
key
)

Returns true if this map contains a mapping for the specified key.

public boolean

Returns:

true if this map maps one or more keys to the specified value
containsValue
(Object
value whose presence in this map is to be tested
value
)

Returns true if this map maps one or more keys to the specified value.

public static <
the Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the entries of the given Map
copyOf
(Map<? extends K, ? extends V>
a Map from which entries are drawn, must be non-null
map
)

Returns an unmodifiable Map containing the entries of the given Map.

public static <
the key's type
K
,
the value's type
V
>
Map.Entry<K, V>

Returns:

an Entry containing the specified key and value
entry
(K
the key
k
,
V
the value
v
)

Returns an unmodifiable Entry containing the given key and value.

public Set<Map.Entry<K, V>>

Returns:

a set view of the mappings contained in this map
entrySet
()

Returns a Set view of the mappings contained in this map.

public boolean

Returns:

true if the specified object is equal to this map
equals
(Object
object to be compared for equality with this map
o
)

Compares the specified object with this map for equality.

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

Returns:

the value to which the specified key is mapped, or null if this map contains no mapping for the key
get
(Object
the key whose associated value is to be returned
key
)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

public default V

Returns:

the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
getOrDefault
(Object
the 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 defaultValue if this map contains no mapping for the key.

public int

Returns:

the hash code value for this map
hashCode
()

Returns the hash code value for this map.

public boolean

Returns:

true if this map contains no key-value mappings
isEmpty
()

Returns true if this map contains no key-value mappings.

public Set<K>

Returns:

a set view of the keys contained in this map
keySet
()

Returns a Set view of the keys contained in this map.

public default V

Returns:

the new value associated with the specified key, or null if no value is associated with the key
merge
(K
key 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

an empty Map
of
()

Returns an unmodifiable map containing zero mappings.

public static <
the Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mapping
of
(K
the mapping's key
k1
,
V
the mapping's value
v1
)

Returns an unmodifiable map containing a single mapping.

public static <
the Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
of
(K
the 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 Map's key type
K
,
the Map's value type
V
>
Map<K, V>

Returns:

a Map containing the specified mappings
ofEntries
(Map.Entry<? extends K, ? extends V>...
Map.Entrys containing the keys and values from which the map is populated
entries
)

Returns an unmodifiable map containing keys and values extracted from the given entries.

public V

Returns:

the previous value associated with key, 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.)
put
(K
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
putAll(Map<? extends K, ? extends V>
mappings to be stored in this map
m
)

Copies all of the mappings from the specified map to this map (optional operation).

public default V

Returns:

the previous value associated with the specified key, or null 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.)
putIfAbsent
(K
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 null) associates it with the given value and returns null, else returns the current value (optional operation).

public V

Returns:

the previous value associated with key, or null if there was no mapping for key.
remove
(Object
key whose mapping is to be removed from the map
key
)

Removes the mapping for a key from this map if it is present (optional operation).

public default boolean

Returns:

true if the value was removed
remove
(Object
key with which the specified value is associated
key
,
Object
value expected to be associated with the specified key
value
)

Removes the entry for the specified key only if it is currently mapped to the specified value (optional operation).

public default boolean

Returns:

true if the value was replaced
replace
(K
key 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, or null 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.)
replace
(K
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 map
size
()

Returns the number of key-value mappings in this map.

public Collection<V>

Returns:

a collection view of the values contained in this map
values
()

Returns a Collection view of the values contained in this map.