BAEL-2800_Copying_a_HashMap_in_Java - code cleanup
This commit is contained in:
		
							parent
							
								
									8455bfc1d0
								
							
						
					
					
						commit
						b17c1c720f
					
				| @ -43,11 +43,11 @@ public class CopyHashMap { | |||||||
|         return copyMap; |         return copyMap; | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public static HashMap copyMapAndConvertCmsToInches(HashMap originalMap) { |     public static HashMap copyMapAndDivideValuesBy2(HashMap originalMap) { | ||||||
|         Set<Map.Entry> entries = originalMap.entrySet(); |         Set<Map.Entry> entries = originalMap.entrySet(); | ||||||
|         HashMap copyMap = (HashMap) entries |         HashMap copyMap = (HashMap) entries | ||||||
|             .stream() |             .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; |         return copyMap; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -15,64 +15,63 @@ public class CopyHashMapUnitTest { | |||||||
|     @Test |     @Test | ||||||
|     public void givenHashMap_whenShallowCopy_thenCopyisNotSameAsOriginal() { |     public void givenHashMap_whenShallowCopy_thenCopyisNotSameAsOriginal() { | ||||||
|          |          | ||||||
|         HashMap<String, Employee> employeeMap = new HashMap<>(); |         HashMap<String, Employee> map = new HashMap<>(); | ||||||
|         Employee emp1 = new Employee("John", "Smith"); |         Employee emp1 = new Employee("John"); | ||||||
|         Employee emp2 = new Employee("Norman", "Lewis"); |         Employee emp2 = new Employee("Norman"); | ||||||
|         employeeMap.put("employee1",emp1); |         map.put("emp1",emp1); | ||||||
|         employeeMap.put("employee2",emp2); |         map.put("emp2",emp2); | ||||||
|      |      | ||||||
|         HashMap employeeMapShallowCopy = CopyHashMap.shallowCopy(employeeMap); |         HashMap shallowCopy = CopyHashMap.shallowCopy(map); | ||||||
|          |          | ||||||
|         assertThat(employeeMapShallowCopy).isNotSameAs(employeeMap); |         assertThat(shallowCopy).isNotSameAs(map); | ||||||
|          |          | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     @Test |     @Test | ||||||
|     public void givenHashMap_whenShallowCopyModifyingOriginalObject_thenCopyShouldChange() { |     public void givenHashMap_whenShallowCopyModifyingOriginalObject_thenCopyShouldChange() { | ||||||
|          |          | ||||||
|         HashMap<String, Employee> employeeMap = new HashMap<>(); |         HashMap<String, Employee> map = new HashMap<>(); | ||||||
|         Employee emp1 = new Employee("John", "Smith"); |         Employee emp1 = new Employee("John"); | ||||||
|         Employee emp2 = new Employee("Norman", "Lewis"); |         Employee emp2 = new Employee("Norman"); | ||||||
|         employeeMap.put("employee1",emp1); |         map.put("emp1",emp1); | ||||||
|         employeeMap.put("employee2",emp2); |         map.put("emp2",emp2); | ||||||
|         HashMap employeeMapShallowCopy = CopyHashMap.shallowCopy(employeeMap); |  | ||||||
|          |          | ||||||
|         emp1.setFirstName("Johny"); |         HashMap shallowCopy = CopyHashMap.shallowCopy(map); | ||||||
|          |          | ||||||
|         assertThat(employeeMapShallowCopy.get("employee1")) |         emp1.setName("Johny"); | ||||||
|         .isEqualTo(employeeMap.get("employee1")); |          | ||||||
|  |         assertThat(shallowCopy.get("emp1")).isEqualTo(map.get("emp1")); | ||||||
|          |          | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     @Test |     @Test | ||||||
|     public void givenHashMap_whenDeepCopyModifyingOriginalObject_thenCopyShouldNotChange() { |     public void givenHashMap_whenDeepCopyModifyingOriginalObject_thenCopyShouldNotChange() { | ||||||
|          |          | ||||||
|         HashMap<String, Employee> employeeMap = new HashMap<>(); |         HashMap<String, Employee> map = new HashMap<>(); | ||||||
|         Employee emp1 = new Employee("John", "Smith"); |         Employee emp1 = new Employee("John"); | ||||||
|         Employee emp2 = new Employee("Norman", "Lewis"); |         Employee emp2 = new Employee("Norman"); | ||||||
|         employeeMap.put("employee1",emp1); |         map.put("emp1",emp1); | ||||||
|         employeeMap.put("employee2",emp2); |         map.put("emp2",emp2); | ||||||
|         HashMap employeeMapDeepCopy = CopyHashMap.deepCopy(employeeMap); |         HashMap deepCopy = CopyHashMap.deepCopy(map); | ||||||
|          |          | ||||||
|         emp1.setFirstName("Johny"); |         emp1.setName("Johny"); | ||||||
|          |          | ||||||
|         assertThat(employeeMapDeepCopy.get("employee1")) |         assertThat(deepCopy.get("emp1")).isNotEqualTo(map.get("emp1")); | ||||||
|         .isNotEqualTo(employeeMap.get("employee1")); |  | ||||||
|          |          | ||||||
|     }  |     }  | ||||||
|      |      | ||||||
|     @Test |     @Test | ||||||
|     public void givenHashMapWithValuesInCms_whenCopy_thenCopyMapShouldHaveValuesInInches() { |     public void givenHashMap_whenCopy_thenCopyMapShouldHaveValuesDivideBy2() { | ||||||
|          |          | ||||||
|         HashMap<String, Integer> heightMap = new HashMap<>(); |         HashMap<String, Integer> heightMap = new HashMap<>(); | ||||||
|         heightMap.put("emp1", 160); |         heightMap.put("emp1", 160); | ||||||
|         heightMap.put("emp2", 165); |         heightMap.put("emp2", 165); | ||||||
|         heightMap.put("emp3", 163); |         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 |     @Test | ||||||
|  | |||||||
| @ -4,33 +4,23 @@ import java.io.Serializable; | |||||||
| 
 | 
 | ||||||
| public class Employee implements Serializable{ | public class Employee implements Serializable{ | ||||||
|      |      | ||||||
|     private String firstName; |     private String name; | ||||||
|     private String lastName; |  | ||||||
|      |      | ||||||
|     public Employee(String firstName, String lastName) { |     public Employee(String name) { | ||||||
|         super(); |         super(); | ||||||
|         this.firstName = firstName; |         this.name = name; | ||||||
|         this.lastName = lastName; |  | ||||||
|     } |     } | ||||||
|     public String getFirstName() { |     public String getName() { | ||||||
|         return firstName; |         return name; | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public void setFirstName(String firstName) { |     public void setName(String name) { | ||||||
|         this.firstName = firstName; |         this.name = name; | ||||||
|     } |  | ||||||
|      |  | ||||||
|     public String getLastName() { |  | ||||||
|         return lastName; |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     public void setLastName(String lastName) { |  | ||||||
|         this.lastName = lastName; |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     @Override |     @Override | ||||||
|     public String toString() { |     public String toString() { | ||||||
|         return this.firstName + " " + this.lastName; |         return this.name; | ||||||
|     } |     } | ||||||
|   |   | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user