diff --git a/core-java-modules/core-java-collections-maps-6/pom.xml b/core-java-modules/core-java-collections-maps-6/pom.xml new file mode 100644 index 0000000000..9910d08691 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-6/pom.xml @@ -0,0 +1,20 @@ + + + core-java-collections-maps-6 + 0.1.0-SNAPSHOT + core-java-collections-maps-6 + jar + + core-java-modules + com.baeldung.core-java-modules + 0.0.1-SNAPSHOT + + 4.0.0 + + + 5.2.5.RELEASE + + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps-6/src/main/java/com/baeldung/map/hashmapcopy/CopyingAHashMapToAnother.java b/core-java-modules/core-java-collections-maps-6/src/main/java/com/baeldung/map/hashmapcopy/CopyingAHashMapToAnother.java new file mode 100644 index 0000000000..77a6402a75 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-6/src/main/java/com/baeldung/map/hashmapcopy/CopyingAHashMapToAnother.java @@ -0,0 +1,47 @@ +package com.baeldung.map.hashmapcopy; + +import java.util.Map; + +import com.google.common.collect.MapDifference; +import com.google.common.collect.Maps; + +public class CopyingAHashMapToAnother { + public Map copyByIteration(Map sourceMap, Map targetMap) { + for (Map.Entry entry : sourceMap.entrySet()) { + if (!targetMap.containsKey(entry.getKey())) { + targetMap.put(entry.getKey(), entry.getValue()); + } + } + return targetMap; + } + + public Map copyUsingPutAll(Map sourceMap, Map targetMap) { + sourceMap.keySet() + .removeAll(targetMap.keySet()); + targetMap.putAll(sourceMap); + return targetMap; + } + + public Map copyUsingPutIfAbsent(Map sourceMap, Map targetMap) { + for (Map.Entry entry : sourceMap.entrySet()) { + targetMap.putIfAbsent(entry.getKey(), entry.getValue()); + } + return targetMap; + } + + public Map copyUsingPutIfAbsentForEach(Map sourceMap, Map targetMap) { + sourceMap.forEach(targetMap::putIfAbsent); + return targetMap; + } + + public Map copyUsingMapMerge(Map sourceMap, Map targetMap) { + sourceMap.forEach((key, value) -> targetMap.merge(key, value, (oldVal, newVal) -> oldVal)); + return targetMap; + } + + public Map copyUsingGuavaMapDifference(Map sourceMap, Map targetMap) { + MapDifference differenceMap = Maps.difference(sourceMap, targetMap); + targetMap.putAll(differenceMap.entriesOnlyOnLeft()); + return targetMap; + } +} diff --git a/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/hashmapcopy/CopyHashMapIntoAnotherUnitTest.java b/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/hashmapcopy/CopyHashMapIntoAnotherUnitTest.java new file mode 100644 index 0000000000..b11f470eb3 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/hashmapcopy/CopyHashMapIntoAnotherUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.map.hashmapcopy; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.baeldung.map.hashmapcopy.CopyingAHashMapToAnother; + +public class CopyHashMapIntoAnotherUnitTest { + @Test + public void givenSourceAndTargetMapsWhenIteratedOverThenCopyingSuccess(){ + CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother(); + Assert.assertEquals(generateExpectedResultMap(), obj.copyByIteration(generateSourceMap(), generateTargetMap())); + } + + @Test + public void givenSourceAndTargetMapsWhenUsedPutAllThenCopyingSuccess(){ + CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother(); + Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutAll(generateSourceMap(), generateTargetMap())); + } + + @Test + public void givenSourceAndTargetMapsWhenUsedPutIfAbsentThenCopyingSuccess(){ + CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother(); + Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutIfAbsent(generateSourceMap(), generateTargetMap())); + Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutIfAbsentForEach(generateSourceMap(), generateTargetMap())); + } + + @Test + public void givenSourceAndTargetMapsWhenUsedMapMergeThenCopyingSuccess(){ + CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother(); + Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingMapMerge(generateSourceMap(), generateTargetMap())); + } + + @Test + public void givenSourceAndTargetMapsWhenMapDifferenceUsedThenCopyingSuccess(){ + CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother(); + Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingGuavaMapDifference(generateSourceMap(), generateTargetMap())); + } + + private Map generateSourceMap(){ + Map sourceMap = new HashMap<>(); + sourceMap.put("India", "Delhi"); + sourceMap.put("United States", "Washington D.C."); + sourceMap.put("United Kingdom", "London DC"); + return sourceMap; + } + + private Map generateTargetMap(){ + Map targetMap = new HashMap<>(); + targetMap.put("Zimbabwe", "Harare"); + targetMap.put("Norway", "Oslo"); + targetMap.put("United Kingdom", "London"); + return targetMap; + } + + private Map generateExpectedResultMap(){ + Map resultMap = new HashMap<>(); + resultMap.put("India", "Delhi"); + resultMap.put("United States", "Washington D.C."); + resultMap.put("United Kingdom", "London"); + resultMap.put("Zimbabwe", "Harare"); + resultMap.put("Norway", "Oslo"); + return resultMap; + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 612e607a38..1033213cd1 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -132,6 +132,7 @@ core-java-regex-2 core-java-uuid pre-jpms + core-java-collections-maps-6