Overview of Java 8 additions to Map interface.
-
default V putIfAbsent(K key, V 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.
returns the previous value associated with the specified key, or
null if there was no mapping for the key.
Remark: putIfAbsent(1, null) will add an entry (supposing 1 is absent)
-
default V computeIfAbsent(K key, Function<? super K, ? extends V> 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.
returns the current (existing or computed) value associated with
the specified key, or null if the computed value is null
Remark: putIfAbsent(1, null) will NOT add an entry (supposing 1 is absent)
-
putIfAbsent vs computeIfAbsent: stackoverflow differences
- different returns,
- laziness of
computeIfAbsent - crucial while constructing expensive
objects, for example new ArrayList<>(),
- different approach to
null values (discussed above)
-
default boolean remove(Object key, Object value) - Removes the entry
for the specified key only if it is currently mapped to the specified value.
-
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> 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
Remark: Can't change the element type. To do that - use a stream and
collect to the new structure.
-
default boolean replace(K key, V oldValue, V newValue) - Replaces the
entry for the specified key only if currently mapped to the specified value.
-
default V replace(K key, V value) - Replaces the entry for the
specified key only if map contains key.
-
default V computeIfPresent(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.
If the function returns null, the mapping is removed.
returns the new value associated with the specified key, or
null if none
-
default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) -
Attempts to compute a mapping for the specified key and its current
mapped value (or null if there is no current mapping).
If the function returns null, the mapping is removed (or remains
absent if initially absent).
returns the new value associated with the specified key, or
null if none
-
default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> 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.
Otherwise, replaces the associated value with the results of the given
remapping function, or removes if the result is null.
If the function returns null the mapping is removed.
returns the new value associated with the specified key, or
null if no value is associated with the key
-
compute vs merge
- arguments of
BiFunction in compute: (key, value)
- arguments of
BiFunction in merge: (oldValue, value)
We provide tests for above mentioned methods.