Java 8
brought some of the major changes in the Collection Framework
, Specifically in Map Interface
such as.
computeIfPresent
If a key
is present in Map
then it needs to be updated with a new value
which must be computed via another Function
.
computeIfAbsent
A lot of times if a key
is not present in Map
then it needs to be added to the Map
and its value
must be computed via another Function
.
getOrDefault
Sometimes situations arise where a key
needs to be updated from a value
from the map
, and if not present
then a default value
must be assigned.
compute
Most of the time, a value
from a map needs to be updated, the traditional way is to get the value from the map if the value is present then update the value and put it back on to map.
forEach
Looping
or Iteration
over the Collections
and Map
is one of the important aspects of Collections
. Prior to Java 8
, List
and Set
could be looped via enhanced for loop
and Map
could be iterated using Entry Set
.
Replace and ReplaceAll
Certain times the single entire value
or the entire value
of the map
needs to be updated, in the case of a single update, the traditional way is to get the value
based on the key
or for the entire value
, iterate the whole Map
over the entry set
and update each entry
.
let's understand each of them one by one.