BAEL-2800_Copying_a_HashMap_in_Java - methods added
This commit is contained in:
parent
4878cc2716
commit
cdb37dddda
|
@ -9,7 +9,16 @@ import org.apache.commons.lang3.SerializationUtils;
|
||||||
|
|
||||||
public class CopyHashMap {
|
public class CopyHashMap {
|
||||||
|
|
||||||
public static HashMap basicCopy(HashMap originalMap, HashMap copyMap) {
|
public static HashMap copyUsingConstructor(HashMap originalMap) {
|
||||||
|
return new HashMap(originalMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HashMap copyUsingClone(HashMap originalMap) {
|
||||||
|
return (HashMap) originalMap.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HashMap copyUsingPut(HashMap originalMap) {
|
||||||
|
HashMap copyMap = new HashMap();
|
||||||
Set<Map.Entry> entries = originalMap.entrySet();
|
Set<Map.Entry> entries = originalMap.entrySet();
|
||||||
for(Map.Entry mapEntry: entries) {
|
for(Map.Entry mapEntry: entries) {
|
||||||
copyMap.put(mapEntry.getKey(), mapEntry.getValue());
|
copyMap.put(mapEntry.getKey(), mapEntry.getValue());
|
||||||
|
@ -18,8 +27,8 @@ public class CopyHashMap {
|
||||||
return copyMap;
|
return copyMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map copyUsingPutAll(Map originalMap) {
|
||||||
public static Map copyUsingPutAll(Map originalMap, Map copyMap) {
|
HashMap copyMap = new HashMap();
|
||||||
copyMap.putAll(originalMap);
|
copyMap.putAll(originalMap);
|
||||||
|
|
||||||
return copyMap;
|
return copyMap;
|
||||||
|
@ -34,8 +43,16 @@ public class CopyHashMap {
|
||||||
return copyMap;
|
return copyMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HashMap copyMapAndConvertCmsToInches(HashMap originalMap) {
|
||||||
|
Set<Map.Entry> 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) {
|
public static HashMap shallowCopy(HashMap originalMap) {
|
||||||
//return new HashMap(originalMap);
|
|
||||||
return (HashMap) originalMap.clone();
|
return (HashMap) originalMap.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,27 +12,6 @@ import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
public class CopyHashMapUnitTest {
|
public class CopyHashMapUnitTest {
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenHashmap_whenCopy_thenCopyContainsAllMappings() {
|
|
||||||
|
|
||||||
HashMap<String, String> colorMap = new HashMap<>();
|
|
||||||
colorMap.put("1", "Red");
|
|
||||||
colorMap.put("2", "Blue");
|
|
||||||
colorMap.put("3", "Green");
|
|
||||||
|
|
||||||
System.out.println("ColorMap content : " + colorMap);
|
|
||||||
|
|
||||||
HashMap<String, String> 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
|
@Test
|
||||||
public void givenHashMap_whenShallowCopy_thenCopyisNotSameAsOriginal() {
|
public void givenHashMap_whenShallowCopy_thenCopyisNotSameAsOriginal() {
|
||||||
|
|
||||||
|
@ -82,6 +61,20 @@ public class CopyHashMapUnitTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHashMapWithValuesInCms_whenCopy_thenCopyMapShouldHaveValuesInInches() {
|
||||||
|
|
||||||
|
HashMap<String, Integer> 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
|
@Test
|
||||||
public void givenImmutableMap_whenCopyUsingGuava_thenCopyShouldNotChange() {
|
public void givenImmutableMap_whenCopyUsingGuava_thenCopyShouldNotChange() {
|
||||||
|
|
||||||
|
@ -96,5 +89,4 @@ public class CopyHashMapUnitTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue