From cdb37dddda62807df8686e7bb77cfcb0ba8cc4cd Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Wed, 3 Apr 2019 17:14:31 +0300 Subject: [PATCH] BAEL-2800_Copying_a_HashMap_in_Java - methods added --- .../baeldung/copyinghashmap/CopyHashMap.java | 25 ++++++++++-- .../copyinghashmap/CopyHashMapUnitTest.java | 38 ++++++++----------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/core-java-collections-2/src/main/java/com/baeldung/copyinghashmap/CopyHashMap.java b/core-java-collections-2/src/main/java/com/baeldung/copyinghashmap/CopyHashMap.java index e6935afbe2..a1dc4fe6bb 100644 --- a/core-java-collections-2/src/main/java/com/baeldung/copyinghashmap/CopyHashMap.java +++ b/core-java-collections-2/src/main/java/com/baeldung/copyinghashmap/CopyHashMap.java @@ -8,8 +8,17 @@ import java.util.stream.Collectors; import org.apache.commons.lang3.SerializationUtils; public class CopyHashMap { + + public static HashMap copyUsingConstructor(HashMap originalMap) { + return new HashMap(originalMap); + } + + public static HashMap copyUsingClone(HashMap originalMap) { + return (HashMap) originalMap.clone(); + } - public static HashMap basicCopy(HashMap originalMap, HashMap copyMap) { + public static HashMap copyUsingPut(HashMap originalMap) { + HashMap copyMap = new HashMap(); Set entries = originalMap.entrySet(); for(Map.Entry mapEntry: entries) { copyMap.put(mapEntry.getKey(), mapEntry.getValue()); @@ -18,8 +27,8 @@ public class CopyHashMap { return copyMap; } - - public static Map copyUsingPutAll(Map originalMap, Map copyMap) { + public static Map copyUsingPutAll(Map originalMap) { + HashMap copyMap = new HashMap(); copyMap.putAll(originalMap); return copyMap; @@ -34,8 +43,16 @@ public class CopyHashMap { return copyMap; } + public static HashMap copyMapAndConvertCmsToInches(HashMap originalMap) { + Set entries = originalMap.entrySet(); + HashMap copyMap = (HashMap) entries + .stream() + .collect(Collectors.toMap(mapEntry -> mapEntry.getKey(), mapEntry -> (int)mapEntry.getValue()/2.54)); + + return copyMap; + } + public static HashMap shallowCopy(HashMap originalMap) { - //return new HashMap(originalMap); return (HashMap) originalMap.clone(); } diff --git a/core-java-collections-2/src/test/java/com/baeldung/copyinghashmap/CopyHashMapUnitTest.java b/core-java-collections-2/src/test/java/com/baeldung/copyinghashmap/CopyHashMapUnitTest.java index 57fccadaef..d49317b3dc 100644 --- a/core-java-collections-2/src/test/java/com/baeldung/copyinghashmap/CopyHashMapUnitTest.java +++ b/core-java-collections-2/src/test/java/com/baeldung/copyinghashmap/CopyHashMapUnitTest.java @@ -12,27 +12,6 @@ import com.google.common.collect.ImmutableMap; public class CopyHashMapUnitTest { - @Test - public void givenHashmap_whenCopy_thenCopyContainsAllMappings() { - - HashMap colorMap = new HashMap<>(); - colorMap.put("1", "Red"); - colorMap.put("2", "Blue"); - colorMap.put("3", "Green"); - - System.out.println("ColorMap content : " + colorMap); - - HashMap colorMapCopy = new HashMap<>(); - colorMapCopy.put("1", "Orange"); - colorMapCopy.put("4", "Black"); - - colorMapCopy = CopyHashMap.basicCopy(colorMap, colorMapCopy); - - System.out.println("ColorMapCopy content : " +colorMapCopy); - - assertEquals(4, colorMapCopy.size()); - } - @Test public void givenHashMap_whenShallowCopy_thenCopyisNotSameAsOriginal() { @@ -80,7 +59,21 @@ public class CopyHashMapUnitTest { assertThat(employeeMapDeepCopy.get("employee1")) .isNotEqualTo(employeeMap.get("employee1")); - } + } + + @Test + public void givenHashMapWithValuesInCms_whenCopy_thenCopyMapShouldHaveValuesInInches() { + + HashMap heightMap = new HashMap<>(); + heightMap.put("emp1", 160); + heightMap.put("emp2", 165); + heightMap.put("emp3", 163); + HashMap heightMapInInches = CopyHashMap.copyMapAndConvertCmsToInches(heightMap); + + assertThat(heightMap).isNotEqualTo(heightMapInInches); + + assertThat(heightMap.get("emp1")/2.54).isEqualTo(heightMapInInches.get("emp1")); + } @Test public void givenImmutableMap_whenCopyUsingGuava_thenCopyShouldNotChange() { @@ -95,6 +88,5 @@ public class CopyHashMapUnitTest { assertThat(heightMapCopy).isSameAs(heightMap); } - }