Method and Description
|
int size()
size()
method returns the total number of key-value mappings in this map.
|
boolean isEmpty()
isEmpty()
method returns if map is empty or not i.e. is there are no key-value mappings
then it returns true else it returns false.
|
boolean containsKey(Object key)
containsKey()
method is used to check whether this map contains a mapping for the key
provided in argument. So it returns true if and only if (key == null ? k ==
null : key.equals(k)). If such mapping exists, then it returns true else it
returns false. There can be at most one such mapping.
|
boolean containsValue(Object value)
containsValue()
method is used to check whether map contains the value passed as parameter or
not. So it returns true if (value == null ? v == null : value.equals(v)).
This method requires scan of entire map at worst case so it might require a
linear time to search a value. As Map implementations allow duplicate values
there can be more than one values in this map.
|
V get(Object key)
get()
method is one of the most used method of Map. It is used to get the value
that is mapped to the key. So if this map contains a mapping of key k to
value v such that
(key
== null ? k== null : key.equals(k)), then this method returns value v,
otherwise it will returns null.
Now
Map allows you to insert null values. Then return value null does not necessarily mean that key does not exists. So using
containsKey() method prior to get() method would be better.
|
V put(K key, V value)
Creates
a mapping for given key to value. If there exists a key in Map, then old
value is replaced with specified value in argument.
|
V remove(Object key)
remove()
method is used to remove the mapping of key-value from this Map. So, if this
map contains a mapping from key k to value v such that
(key == null ? k ==
null : key.equals(k)) then this mapping is removed. It returns the value
associated with the key that is removed else it returns null.
If
this map allows null values, then return value null does not indicate that
key k was not found in this map. It is possible that key is explicitly mapped
to a null value.
|
void putAll(Map<? extends K, ? extends V>
m)
putAll
method is used to copy all the mapping from map m to this map. The effect of
this method is same as calling put(K k, V v) method called for every mapping
of map m.
|
void clear()
clear()
method is used to remove all the mappings from map. This will also set the
size of map as 0.
|
Set<K> keySet()
keySet()
method is View method of Map. It returns a Set<E> view of keys
contained in this particular map. As Set is backed by Map the changes to Map
are reflected to Set and changes to Set are reflected to Map.
|
Collection<V> values()
values()
method is also a View method of Map. This method returns Collection view of
values of Map. The Collection is backed by Map so changes made to Map are visible
in Collection and changes made to Collection are visible in Map.
|
Set<Map.Entry<K, V>> entrySet()
entrySet()
is 3rd and final View method of Map. This method returns
Set<> of type Map.Entry<K, V>. Entry is sub-interface of Map interface
i.e. Entry interface inside Map interface. As Set is backed by Map the
changes to Map are reflected to Set and changes to Set are reflected to Map.
|
boolean equals(Object o)
equals()
method is overridden method from Object class. equals() and hashCode() are
used for comparison and hashing. equals() method compares specified Object with
this Map for equality. This method will return true if and only if specified
Object is Map and both Map has same Key-Value mappings.
|
int hashCode()
hashCode()
method is used to return hash code of this Map. The hash code of this map is
defined as summation of all hash code of each entry in this map. Entries are
taken from entrySet() method.
|
default V getOrDefault(Object key, V
defaultValue)
getOrDefault()
method is used to return the value of key mapped to value or returns
defaultValue as passed in parameter.
|
default void forEach(BiConsumer<? super K, ?
super V> action)
BiConsumer
interface performs given action on all the entries of this map until all
entries are processed or throws an Exception.
|
default void replaceAll(BiFunction<? super K,
?
super V,
? extends V> function)
BiFunction
interface in replaceAll method will replace each entry’s value with result of
invoking the given function on that entry until all entries are processed or
function throws an Exception.
|
default V putIfAbsent (K key, V value)
putIfAbsent()
method is used to put the value in Map if the key is not associated with a value
or if key is mapped to null.
|
default boolean remove (Object key, Object value)
remove()
method is used to remove the entry for the key if it is mapped to specified
value.
|
default boolean replace(K key, V oldValue, V
newValue)
replace()
method is used to replace the entry for the key if it is mapped to specified
value.
|
default V replace(K key, V value)
replace()
method is used to replace the entry for the key if it is mapped to some
value.
|
default V computeIfAbsent(K key, Function<?
super K,
? extends V> mappingFunction)
computeifAbsent()
method is used to map a key that is not associated with value or mapped to
null. It attempts to compute its value using given mapping function.
|
default V
computeIfPresent(K key, BiFunction<? super K,
? super V,
? extends V> remappingFunction)
computeIfPresent()
method is opposite of computeIfAbsent(). If the value of key is present and not-null then it attempts to compute
new mapping given the key and its current mapped value.
|
default V
compute(K key, BiFunction<? super K,
?
super V,
? extends V> remappingFunction)
compute()
method is used to compute a mapping for specified key and its current mapped
value if there is no current mapping.
|
default V
merge(K key, V value, BiFunction<?
super V,
? super V,
? extends V> remappingFunction)
merge()
method is helpful when combining multiple mapped values for key. If the key
is not already associated with a value or is associate with null, then it is
associated with non-null value. Otherwise it replaces the associated value
with results of remapping function or removes it result is null. If the
function returns null, the mapping is removed.
|