BAEL-2800_Copying_a_HashMap_in_Java new module added

This commit is contained in:
Anshul Bansal
2019-04-03 15:06:33 +03:00
parent 2bd55bb195
commit 4878cc2716
6 changed files with 86 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
=========
## Core Java Collections 2
### Relevant Articles:
- Java - Copying a HashMap
+78
View File
@@ -0,0 +1,78 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>${eclipse.collections.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${openjdk.jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${openjdk.jmh.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>${commons-exec.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<openjdk.jmh.version>1.19</openjdk.jmh.version>
<junit.platform.version>1.2.0</junit.platform.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<commons-collections4.version>4.1</commons-collections4.version>
<collections-generic.version>4.01</collections-generic.version>
<avaitility.version>1.7.0</avaitility.version>
<assertj.version>3.11.1</assertj.version>
<eclipse.collections.version>7.1.0</eclipse.collections.version>
<commons-exec.version>1.3</commons-exec.version>
</properties>
</project>
@@ -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);
}
}
@@ -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);
}
}
@@ -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;
}
}