[JAVA-6143] java copy from one hashmap to another (#13497)
Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
parent
749a077478
commit
e11c4e3dbc
20
core-java-modules/core-java-collections-maps-6/pom.xml
Normal file
20
core-java-modules/core-java-collections-maps-6/pom.xml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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">
|
||||||
|
<artifactId>core-java-collections-maps-6</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-collections-maps-6</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<parent>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<spring.version>5.2.5.RELEASE</spring.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.map.hashmapcopy;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.common.collect.MapDifference;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
public class CopyingAHashMapToAnother {
|
||||||
|
public Map<String, String> copyByIteration(Map<String, String> sourceMap, Map<String, String> targetMap) {
|
||||||
|
for (Map.Entry<String, String> entry : sourceMap.entrySet()) {
|
||||||
|
if (!targetMap.containsKey(entry.getKey())) {
|
||||||
|
targetMap.put(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return targetMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> copyUsingPutAll(Map<String, String> sourceMap, Map<String, String> targetMap) {
|
||||||
|
sourceMap.keySet()
|
||||||
|
.removeAll(targetMap.keySet());
|
||||||
|
targetMap.putAll(sourceMap);
|
||||||
|
return targetMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> copyUsingPutIfAbsent(Map<String, String> sourceMap, Map<String, String> targetMap) {
|
||||||
|
for (Map.Entry<String, String> entry : sourceMap.entrySet()) {
|
||||||
|
targetMap.putIfAbsent(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
return targetMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> copyUsingPutIfAbsentForEach(Map<String, String> sourceMap, Map<String, String> targetMap) {
|
||||||
|
sourceMap.forEach(targetMap::putIfAbsent);
|
||||||
|
return targetMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> copyUsingMapMerge(Map<String, String> sourceMap, Map<String, String> targetMap) {
|
||||||
|
sourceMap.forEach((key, value) -> targetMap.merge(key, value, (oldVal, newVal) -> oldVal));
|
||||||
|
return targetMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> copyUsingGuavaMapDifference(Map<String, String> sourceMap, Map<String, String> targetMap) {
|
||||||
|
MapDifference<String, String> differenceMap = Maps.difference(sourceMap, targetMap);
|
||||||
|
targetMap.putAll(differenceMap.entriesOnlyOnLeft());
|
||||||
|
return targetMap;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.map.hashmapcopy;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.map.hashmapcopy.CopyingAHashMapToAnother;
|
||||||
|
|
||||||
|
public class CopyHashMapIntoAnotherUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenSourceAndTargetMapsWhenIteratedOverThenCopyingSuccess(){
|
||||||
|
CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
|
||||||
|
Assert.assertEquals(generateExpectedResultMap(), obj.copyByIteration(generateSourceMap(), generateTargetMap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSourceAndTargetMapsWhenUsedPutAllThenCopyingSuccess(){
|
||||||
|
CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
|
||||||
|
Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutAll(generateSourceMap(), generateTargetMap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSourceAndTargetMapsWhenUsedPutIfAbsentThenCopyingSuccess(){
|
||||||
|
CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
|
||||||
|
Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutIfAbsent(generateSourceMap(), generateTargetMap()));
|
||||||
|
Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutIfAbsentForEach(generateSourceMap(), generateTargetMap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSourceAndTargetMapsWhenUsedMapMergeThenCopyingSuccess(){
|
||||||
|
CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
|
||||||
|
Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingMapMerge(generateSourceMap(), generateTargetMap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSourceAndTargetMapsWhenMapDifferenceUsedThenCopyingSuccess(){
|
||||||
|
CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
|
||||||
|
Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingGuavaMapDifference(generateSourceMap(), generateTargetMap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> generateSourceMap(){
|
||||||
|
Map<String, String> sourceMap = new HashMap<>();
|
||||||
|
sourceMap.put("India", "Delhi");
|
||||||
|
sourceMap.put("United States", "Washington D.C.");
|
||||||
|
sourceMap.put("United Kingdom", "London DC");
|
||||||
|
return sourceMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> generateTargetMap(){
|
||||||
|
Map<String, String> targetMap = new HashMap<>();
|
||||||
|
targetMap.put("Zimbabwe", "Harare");
|
||||||
|
targetMap.put("Norway", "Oslo");
|
||||||
|
targetMap.put("United Kingdom", "London");
|
||||||
|
return targetMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> generateExpectedResultMap(){
|
||||||
|
Map<String, String> resultMap = new HashMap<>();
|
||||||
|
resultMap.put("India", "Delhi");
|
||||||
|
resultMap.put("United States", "Washington D.C.");
|
||||||
|
resultMap.put("United Kingdom", "London");
|
||||||
|
resultMap.put("Zimbabwe", "Harare");
|
||||||
|
resultMap.put("Norway", "Oslo");
|
||||||
|
return resultMap;
|
||||||
|
}
|
||||||
|
}
|
@ -132,6 +132,7 @@
|
|||||||
<module>core-java-regex-2</module>
|
<module>core-java-regex-2</module>
|
||||||
<module>core-java-uuid</module>
|
<module>core-java-uuid</module>
|
||||||
<module>pre-jpms</module>
|
<module>pre-jpms</module>
|
||||||
|
<module>core-java-collections-maps-6</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user