Say I have a map like
{one=1; two=1; three=1}
and another map like
{one=4; two=4; three=4}
I know that putAll() would add unique keys and replace existing keys. Will it be possible to do an addition of both the maps which would produce a result like adding the values whenever there is an existing keyword.
{one=5; two=5; three=5}
Create a new
Map
, then for all entriesx
inmap1
, ifmap2
contains keyx
, put the addition of both values into the new map with keyx
.Try this method. Works with a generic key type. Value type remains
Integer
as we are adding them.Extend the HashMap and override the putAll method.
}
Now, create objects of MyMap instead of HashMap. Created a fiddle Here
try this ,