BAEL-2800_Copying_a_HashMap_in_Java - methods added
This commit is contained in:
parent
4878cc2716
commit
cdb37dddda
|
@ -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<Map.Entry> 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<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) {
|
||||
//return new HashMap(originalMap);
|
||||
return (HashMap) originalMap.clone();
|
||||
}
|
||||
|
||||
|
|
|
@ -12,27 +12,6 @@ import com.google.common.collect.ImmutableMap;
|
|||
|
||||
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
|
||||
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<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
|
||||
public void givenImmutableMap_whenCopyUsingGuava_thenCopyShouldNotChange() {
|
||||
|
@ -95,6 +88,5 @@ public class CopyHashMapUnitTest {
|
|||
assertThat(heightMapCopy).isSameAs(heightMap);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue