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
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all elements from thisMap, leaving it empty.default VAttempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).default VcomputeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) If the specified key is not already associated with a value (or is mapped tonull), attempts to compute its value using the given mapping function and enters it into this map unlessnull.default VcomputeIfPresent(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.booleancontainsKey(Object key) Returns whether thisMapcontains the specified key.booleancontainsValue(Object value) Returns whether thisMapcontains the specified value.entrySet()Returns aSetcontaining all of the mappings in thisMap.booleanCompares the argument to the receiver, and returnstrueif the specified object is aMapand bothMaps contain the same mappings.default voidforEach(BiConsumer<? super K, ? super V> action) Performs the given action for each entry in this map.Returns the value of the mapping with the specified key.default VgetOrDefault(Object key, V defaultValue) Returns the value to which the specified key is mapped, ordefaultValueif this map contains no mapping for the key.inthashCode()Returns an integer hash code for the receiver.booleanisEmpty()Returns whether this map is empty.keySet()Returns a set of the keys contained in thisMap.default VIf the specified key is not already associated with a value or is associated withnull, associates it with the given non-null value.Maps the specified key to the specified value.voidCopies every mapping in the specifiedMapto thisMap.default VputIfAbsent(K key, V value) If the specified key is not already associated with a value (or is mapped tonull) associates it with the given value and returnsnull, else returns the current value.Removes a mapping with the specified key from thisMap.default booleanRemoves the entry for the specified key only if it is currently mapped to the specified value.default VReplaces the entry for the specified key only if it is currently mapped to some value.default booleanReplaces the entry for the specified key only if currently mapped to the specified value.default voidreplaceAll(BiFunction<? super K, ? super V, ? extends V> function) Replaces each entry's value with the result of invoking the given function on that entry.intsize()Returns the number of mappings in thisMap.values()Returns aCollectionof the values contained in thisMap.
-
Method Details
-
clear
void clear()Removes all elements from this
Map, leaving it empty.Throws
UnsupportedOperationException: if removing elements from thisMapis not supported.
See also
-
#isEmpty()
-
#size()
-
containsKey
Returns whether this
Mapcontains the specified key.Parameters
key: the key to search for.
Returns
- Returns:
trueif this map contains the specified key,falseotherwise.
-
containsValue
Returns whether this
Mapcontains the specified value.Parameters
value: the value to search for.
Returns
- Returns:
trueif this map contains the specified value,falseotherwise.
-
entrySet
-
equals
Compares the argument to the receiver, and returns
trueif the specified object is aMapand bothMaps contain the same mappings.Parameters
object: theObjectto compare with thisObject.
Returns
-
get
-
hashCode
-
isEmpty
boolean isEmpty()Returns whether this map is empty.
Returns
- Returns:
trueif this map has no elements,falseotherwise.See also
- #size()
-
keySet
-
put
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
nullif there was no mapping.Throws
-
UnsupportedOperationException: if adding to thisMapis not supported. -
ClassCastException: @throws ClassCastException if the class of the key or value is inappropriate for thisMap. -
IllegalArgumentException: if the key or value cannot be added to thisMap. -
NullPointerException: @throws NullPointerException if the key or value isnulland thisMapdoes not supportnullkeys or values.
-
-
-
putAll
Copies every mapping in the specified
Mapto thisMap.Parameters
map: theMapto copy mappings from.
Throws
-
UnsupportedOperationException: if adding to thisMapis not supported. -
ClassCastException: @throws ClassCastException if the class of a key or a value of the specifiedMapis inappropriate for thisMap. -
IllegalArgumentException: if a key or value cannot be added to thisMap. -
NullPointerException: @throws NullPointerException if a key or value isnulland thisMapdoes not supportnullkeys or values.
-
remove
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
nullif no mapping for the specified key was found.Throws
UnsupportedOperationException: if removing from thisMapis 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
Collectionof the values contained in thisMap. TheCollectionis backed by thisMapso changes to one are reflected by the other. TheCollectionsupportsCollection#remove,Collection#removeAll,Collection#retainAll, andCollection#clearoperations, and it does not supportCollection#addorCollection#addAlloperations.This method returns a
Collectionwhich is the subclass ofAbstractCollection. TheAbstractCollection#iteratormethod of this subclass returns a "wrapper object" over the iterator of thisMap's#entrySet(). TheAbstractCollection#sizemethod wraps thisMap's#sizemethod and theAbstractCollection#containsmethod wraps thisMap's#containsValuemethod.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
-
putIfAbsent
-
remove
-
replace
-
replace
-
forEach
Performs the given action for each entry in this map. -
replaceAll
Replaces each entry's value with the result of invoking the given function on that entry. -
computeIfAbsent
-
computeIfPresent
-
compute
-
merge
If the specified key is not already associated with a value or is associated withnull, 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 isnull.
-