BAEL-2800_Copying_a_HashMap_in_Java - code cleanup

This commit is contained in:
Anshul Bansal 2019-04-05 12:53:14 +03:00
parent 8455bfc1d0
commit b17c1c720f
3 changed files with 38 additions and 49 deletions

View File

@ -43,11 +43,11 @@ public class CopyHashMap {
return copyMap;
}
public static HashMap copyMapAndConvertCmsToInches(HashMap originalMap) {
public static HashMap copyMapAndDivideValuesBy2(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));
.collect(Collectors.toMap(mapEntry -> mapEntry.getKey(), mapEntry -> (int)mapEntry.getValue()/2));
return copyMap;
}

View File

@ -15,64 +15,63 @@ public class CopyHashMapUnitTest {
@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<String, Employee> map = new HashMap<>();
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Norman");
map.put("emp1",emp1);
map.put("emp2",emp2);
HashMap employeeMapShallowCopy = CopyHashMap.shallowCopy(employeeMap);
HashMap shallowCopy = CopyHashMap.shallowCopy(map);
assertThat(employeeMapShallowCopy).isNotSameAs(employeeMap);
assertThat(shallowCopy).isNotSameAs(map);
}
@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);
HashMap<String, Employee> map = new HashMap<>();
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Norman");
map.put("emp1",emp1);
map.put("emp2",emp2);
emp1.setFirstName("Johny");
HashMap shallowCopy = CopyHashMap.shallowCopy(map);
assertThat(employeeMapShallowCopy.get("employee1"))
.isEqualTo(employeeMap.get("employee1"));
emp1.setName("Johny");
assertThat(shallowCopy.get("emp1")).isEqualTo(map.get("emp1"));
}
@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);
HashMap<String, Employee> map = new HashMap<>();
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Norman");
map.put("emp1",emp1);
map.put("emp2",emp2);
HashMap deepCopy = CopyHashMap.deepCopy(map);
emp1.setFirstName("Johny");
emp1.setName("Johny");
assertThat(employeeMapDeepCopy.get("employee1"))
.isNotEqualTo(employeeMap.get("employee1"));
assertThat(deepCopy.get("emp1")).isNotEqualTo(map.get("emp1"));
}
@Test
public void givenHashMapWithValuesInCms_whenCopy_thenCopyMapShouldHaveValuesInInches() {
public void givenHashMap_whenCopy_thenCopyMapShouldHaveValuesDivideBy2() {
HashMap<String, Integer> heightMap = new HashMap<>();
heightMap.put("emp1", 160);
heightMap.put("emp2", 165);
heightMap.put("emp3", 163);
HashMap heightMapInInches = CopyHashMap.copyMapAndConvertCmsToInches(heightMap);
HashMap heightMapCopy = CopyHashMap.copyMapAndDivideValuesBy2(heightMap);
assertThat(heightMap).isNotEqualTo(heightMapInInches);
assertThat(heightMap).isNotEqualTo(heightMapCopy);
assertThat(heightMap.get("emp1")/2.54).isEqualTo(heightMapInInches.get("emp1"));
assertThat(heightMap.get("emp1")/2).isEqualTo(heightMapCopy.get("emp1"));
}
@Test

View File

@ -4,33 +4,23 @@ import java.io.Serializable;
public class Employee implements Serializable{
private String firstName;
private String lastName;
private String name;
public Employee(String firstName, String lastName) {
public Employee(String name) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.name = name;
}
public String getFirstName() {
return firstName;
public String getName() {
return name;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.firstName + " " + this.lastName;
return this.name;
}
}