Merge pull request #6652 from SmartyAnsh/master
BAEL-2800 - BAEL-2800_Copying_a_HashMap_in_Java - new module added
This commit is contained in:
commit
9ff896fe4e
|
@ -0,0 +1,6 @@
|
||||||
|
=========
|
||||||
|
|
||||||
|
## Core Java Collections 2
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- Java - Copying a HashMap
|
|
@ -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-map</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-collections-map</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,55 @@
|
||||||
|
package com.baeldung.copyinghashmap;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.SerializationUtils;
|
||||||
|
|
||||||
|
public class CopyHashMap {
|
||||||
|
|
||||||
|
public static <String, Employee> HashMap<String, Employee> copyUsingConstructor(HashMap<String, Employee> originalMap) {
|
||||||
|
return new HashMap<String, Employee>(originalMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <String, Employee> HashMap<String, Employee> copyUsingClone(HashMap<String, Employee> originalMap) {
|
||||||
|
return (HashMap<String, Employee>) originalMap.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <String, Employee> HashMap<String, Employee> copyUsingPut(HashMap<String, Employee> originalMap) {
|
||||||
|
HashMap<String, Employee> shallowCopy = new HashMap<String, Employee>();
|
||||||
|
Set<Entry<String, Employee>> entries = originalMap.entrySet();
|
||||||
|
for(Map.Entry<String, Employee> mapEntry: entries) {
|
||||||
|
shallowCopy.put(mapEntry.getKey(), mapEntry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return shallowCopy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <String, Employee> HashMap<String, Employee> copyUsingPutAll(HashMap<String, Employee> originalMap) {
|
||||||
|
HashMap<String, Employee> shallowCopy = new HashMap<String, Employee>();
|
||||||
|
shallowCopy.putAll(originalMap);
|
||||||
|
|
||||||
|
return shallowCopy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <String, Employee> HashMap<String, Employee> copyUsingJava8Stream(HashMap<String, Employee> originalMap) {
|
||||||
|
Set<Entry<String, Employee>> entries = originalMap.entrySet();
|
||||||
|
HashMap<String, Employee> shallowCopy = (HashMap<String, Employee>) entries
|
||||||
|
.stream()
|
||||||
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||||
|
|
||||||
|
return shallowCopy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <String, Employee> HashMap<String, Employee> shallowCopy(HashMap<String, Employee> originalMap) {
|
||||||
|
return (HashMap<String, Employee>) originalMap.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <String, Employee> HashMap<String, Employee> deepCopy(HashMap<String, Employee> originalMap) {
|
||||||
|
return SerializationUtils.clone(originalMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.baeldung.copyinghashmap;
|
||||||
|
|
||||||
|
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_whenShallowCopy_thenCopyisNotSameAsOriginal() {
|
||||||
|
|
||||||
|
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<String, Employee> shallowCopy = CopyHashMap.shallowCopy(map);
|
||||||
|
|
||||||
|
assertThat(shallowCopy).isNotSameAs(map);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHashMap_whenShallowCopyModifyingOriginalObject_thenCopyShouldChange() {
|
||||||
|
|
||||||
|
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<String, Employee> shallowCopy = CopyHashMap.shallowCopy(map);
|
||||||
|
|
||||||
|
emp1.setName("Johny");
|
||||||
|
|
||||||
|
assertThat(shallowCopy.get("emp1")).isEqualTo(map.get("emp1"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHashMap_whenDeepCopyModifyingOriginalObject_thenCopyShouldNotChange() {
|
||||||
|
|
||||||
|
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<String, Employee> deepCopy = CopyHashMap.deepCopy(map);
|
||||||
|
|
||||||
|
emp1.setName("Johny");
|
||||||
|
|
||||||
|
assertThat(deepCopy.get("emp1")).isNotEqualTo(map.get("emp1"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImmutableMap_whenCopyUsingGuava_thenCopyShouldNotChange() {
|
||||||
|
Employee emp1 = new Employee("John");
|
||||||
|
Employee emp2 = new Employee("Norman");
|
||||||
|
|
||||||
|
Map<String, Employee> map = ImmutableMap.<String, Employee> builder()
|
||||||
|
.put("emp1",emp1)
|
||||||
|
.put("emp2",emp2)
|
||||||
|
.build();
|
||||||
|
Map<String, Employee> shallowCopy = ImmutableMap.copyOf(map);
|
||||||
|
|
||||||
|
assertThat(shallowCopy).isSameAs(map);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.baeldung.copyinghashmap;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Employee implements Serializable{
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Employee(String name) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
2
pom.xml
2
pom.xml
|
@ -385,6 +385,7 @@
|
||||||
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
|
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
|
||||||
<module>core-java-arrays</module>
|
<module>core-java-arrays</module>
|
||||||
<module>core-java-collections</module>
|
<module>core-java-collections</module>
|
||||||
|
<module>core-java-collections-map</module>
|
||||||
<module>core-java-collections-list</module>
|
<module>core-java-collections-list</module>
|
||||||
<module>core-java-concurrency-basic</module>
|
<module>core-java-concurrency-basic</module>
|
||||||
<module>core-java-concurrency-collections</module>
|
<module>core-java-concurrency-collections</module>
|
||||||
|
@ -1041,6 +1042,7 @@
|
||||||
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
|
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
|
||||||
<module>core-java-arrays</module>
|
<module>core-java-arrays</module>
|
||||||
<module>core-java-collections</module>
|
<module>core-java-collections</module>
|
||||||
|
<module>core-java-collections-map</module>
|
||||||
<module>core-java-collections-list</module>
|
<module>core-java-collections-list</module>
|
||||||
<module>core-java-concurrency-basic</module>
|
<module>core-java-concurrency-basic</module>
|
||||||
<module>core-java-concurrency-collections</module>
|
<module>core-java-concurrency-collections</module>
|
||||||
|
|
Loading…
Reference in New Issue