BAEL-2800_Copying_a_HashMap_in_Java

This commit is contained in:
Anshul Bansal 2019-04-01 19:32:50 +03:00
parent 728e5f39a9
commit 2bd55bb195
3 changed files with 184 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package com.baeldung.copyinghashmap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.SerializationUtils;
public class CopyHashMap {
public static HashMap basicCopy(HashMap originalMap, HashMap copyMap) {
Set<Map.Entry> entries = originalMap.entrySet();
for(Map.Entry mapEntry: entries) {
copyMap.put(mapEntry.getKey(), mapEntry.getValue());
}
return copyMap;
}
public static Map copyUsingPutAll(Map originalMap, Map copyMap) {
copyMap.putAll(originalMap);
return copyMap;
}
public static HashMap copyUsingJava8Stream(HashMap originalMap) {
Set<Map.Entry> entries = originalMap.entrySet();
HashMap copyMap = (HashMap) entries
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return copyMap;
}
public static HashMap shallowCopy(HashMap originalMap) {
//return new HashMap(originalMap);
return (HashMap) originalMap.clone();
}
public static HashMap deepCopy(HashMap originalMap) {
return SerializationUtils.clone(originalMap);
}
}

View File

@ -0,0 +1,100 @@
package com.baeldung.copyinghashmap;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
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() {
HashMap<String, Employee> employeeMap = new HashMap<>();
Employee emp1 = new Employee("John", "Smith");
Employee emp2 = new Employee("Norman", "Lewis");
employeeMap.put("employee1",emp1);
employeeMap.put("employee2",emp2);
HashMap employeeMapShallowCopy = CopyHashMap.shallowCopy(employeeMap);
assertThat(employeeMapShallowCopy).isNotSameAs(employeeMap);
}
@Test
public void givenHashMap_whenShallowCopyModifyingOriginalObject_thenCopyShouldChange() {
HashMap<String, Employee> employeeMap = new HashMap<>();
Employee emp1 = new Employee("John", "Smith");
Employee emp2 = new Employee("Norman", "Lewis");
employeeMap.put("employee1",emp1);
employeeMap.put("employee2",emp2);
HashMap employeeMapShallowCopy = CopyHashMap.shallowCopy(employeeMap);
emp1.setFirstName("Johny");
assertThat(employeeMapShallowCopy.get("employee1"))
.isEqualTo(employeeMap.get("employee1"));
}
@Test
public void givenHashMap_whenDeepCopyModifyingOriginalObject_thenCopyShouldNotChange() {
HashMap<String, Employee> employeeMap = new HashMap<>();
Employee emp1 = new Employee("John", "Smith");
Employee emp2 = new Employee("Norman", "Lewis");
employeeMap.put("employee1",emp1);
employeeMap.put("employee2",emp2);
HashMap employeeMapDeepCopy = CopyHashMap.deepCopy(employeeMap);
emp1.setFirstName("Johny");
assertThat(employeeMapDeepCopy.get("employee1"))
.isNotEqualTo(employeeMap.get("employee1"));
}
@Test
public void givenImmutableMap_whenCopyUsingGuava_thenCopyShouldNotChange() {
Map<String, Integer> heightMap = ImmutableMap.<String, Integer> builder()
.put("emp1", 160)
.put("emp2", 165)
.put("emp3", 163)
.build();
Map<String, Integer> heightMapCopy = ImmutableMap.copyOf(heightMap);
assertThat(heightMapCopy).isSameAs(heightMap);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.copyinghashmap;
import java.io.Serializable;
public class Employee implements Serializable{
private String firstName;
private String lastName;
public Employee(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return this.firstName + " " + this.lastName;
}
}