public interface Map<K, V>
Known subtypesProperties, WeakHashMap, AbstractMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, NavigableMap, SortedMap, TreeMap
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 types
interface Map.Entry | Map.Entry is a key/value mapping contained in a Map. |
Methods
Method details
clear
public abstract void clear()Map, leaving it empty.Throws
UnsupportedOperationException- if removing elements from this
Mapis not supported.
See also
containsKey
public abstract boolean containsKey(Object key)Map contains the specified key.Parameters
keyObject- the key to search for.
Returns
true if this map contains the specified key,
false otherwise.containsValue
public abstract boolean containsValue(Object value)Map contains the specified value.Parameters
valueObject- the value to search for.
Returns
true if this map contains the specified value,
false otherwise.entrySet
public abstract Set<Map.Entry<K, V>> entrySet()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
equals
public abstract boolean equals(Object object)true if the
specified object is a Map and both Maps contain the same mappings.Parameters
objectObject- the
Objectto compare with thisObject.
Returns
true if the Object is the same as this Object
false if it is different from this Object.See also
get
public abstract V get(Object key)Parameters
keyObject- the key.
Returns
null
if no mapping for the specified key is found.hashCode
public abstract int hashCode()Objects which are equal
return the same value for this method.Returns
See also
isEmpty
public abstract boolean isEmpty()Returns
true if this map has no elements, false
otherwise.See also
keySet
public abstract Set<K> keySet()Map. The Set is backed by
this Map so changes to one are reflected by the other. The Set does not
support adding.Returns
put
public abstract V put(K key, V value)Parameters
keyK- the key.
valueV- the value.
Returns
null if there was no mapping.Throws
UnsupportedOperationException- if adding to this
Mapis not supported. 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- if the key or value is
nulland thisMapdoes not supportnullkeys or values.
putAll
public abstract void putAll(Map<? extends K, ? extends V> map)Map to this Map.Parameters
mapMap<? extends K, ? extends V>- the
Mapto copy mappings from.
Throws
UnsupportedOperationException- if adding to this
Mapis not supported. ClassCastException- if the class of a key or a value of the specified
Mapis inappropriate for thisMap. IllegalArgumentException- if a key or value cannot be added to this
Map. NullPointerException- if a key or value is
nulland thisMapdoes not supportnullkeys or values.
remove
public abstract V remove(Object key)Map.Parameters
keyObject- the key of the mapping to remove.
Returns
null if no mapping
for the specified key was found.Throws
UnsupportedOperationException- if removing from this
Mapis not supported.
size
public abstract int size()Map.Returns
Map.values
public abstract 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
getOrDefault
public default V getOrDefault(Object key, V defaultValue)defaultValue if this map contains no mapping for the key.putIfAbsent
public default V putIfAbsent(K key, V value)null) associates it with the given value and returns
null, else returns the current value.remove
public default boolean remove(Object key, Object value)replace
public default boolean replace(K key, V oldValue, V newValue)replace
public default V replace(K key, V value)forEach
public default void forEach(BiConsumer<? super K, ? super V> action)replaceAll
public default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)computeIfAbsent
public default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)null), attempts to compute its value using the
given mapping function and enters it into this map unless
null.computeIfPresent
public default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)compute
public default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)null if there is no current mapping).merge
public default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)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.