Map<Integer, Integer> hasmapA = new HashMap<>();
hasmapA.put("key1", "value1");
hasmapA.put("key2", "value2");
hasmapA.put("key3", "value3");
Copy By reference: If you assign one HashMap to other then both point to same reference in memory.
Map hasmapB;
hashmapB = hashmapA;
If you make changes in any of these, changes will reflect in both HashMap as both are referencing to same location.
- Copy By Value: If you wan to
clone/deepcopy/create separate memory location/create separate object
of hashmapB while copying content of hashmapA
Map<Integer, Integer> hashmapB = new HashMap<>();;
hashmapB.putAll(hashmapA)
Note: **
Have you noticed difference in both points for hashmapB declaration? In second point we have to call **HashMap constructor. So that we can putAll data of hashmapA into hashmapB.
Reference