BAEL-2800_Copying_a_HashMap_in_Java - generic types added
This commit is contained in:
parent
68ffec2c2c
commit
aae74f3b6c
|
@ -2,6 +2,7 @@ package com.baeldung.copyinghashmap;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -9,45 +10,45 @@ import org.apache.commons.lang3.SerializationUtils;
|
||||||
|
|
||||||
public class CopyHashMap {
|
public class CopyHashMap {
|
||||||
|
|
||||||
public static HashMap copyUsingConstructor(HashMap originalMap) {
|
public static <K, V> HashMap<K, V> copyUsingConstructor(HashMap<K, V> originalMap) {
|
||||||
return new HashMap(originalMap);
|
return new HashMap<K, V>(originalMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashMap copyUsingClone(HashMap originalMap) {
|
public static <K, V> HashMap<K, V> copyUsingClone(HashMap<K, V> originalMap) {
|
||||||
return (HashMap) originalMap.clone();
|
return (HashMap<K, V>) originalMap.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashMap copyUsingPut(HashMap originalMap) {
|
public static <K, V> HashMap<K, V> copyUsingPut(HashMap<K, V> originalMap) {
|
||||||
HashMap copyMap = new HashMap();
|
HashMap<K, V> copyMap = new HashMap<K, V>();
|
||||||
Set<Map.Entry> entries = originalMap.entrySet();
|
Set<Entry<K, V>> entries = originalMap.entrySet();
|
||||||
for(Map.Entry mapEntry: entries) {
|
for(Map.Entry<K, V> mapEntry: entries) {
|
||||||
copyMap.put(mapEntry.getKey(), mapEntry.getValue());
|
copyMap.put((K)mapEntry.getKey(), (V)mapEntry.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
return copyMap;
|
return copyMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map copyUsingPutAll(Map originalMap) {
|
public static <K, V> HashMap<K, V> copyUsingPutAll(HashMap<K, V> originalMap) {
|
||||||
HashMap copyMap = new HashMap();
|
HashMap<K, V> copyMap = new HashMap<K, V>();
|
||||||
copyMap.putAll(originalMap);
|
copyMap.putAll(originalMap);
|
||||||
|
|
||||||
return copyMap;
|
return copyMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashMap copyUsingJava8Stream(HashMap originalMap) {
|
public static <K, V> HashMap<K, V> copyUsingJava8Stream(HashMap<K, V> originalMap) {
|
||||||
Set<Map.Entry> entries = originalMap.entrySet();
|
Set<Entry<K, V>> entries = originalMap.entrySet();
|
||||||
HashMap copyMap = (HashMap) entries
|
HashMap<K, V> copyMap = (HashMap<K, V>) entries
|
||||||
.stream()
|
.stream()
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||||
|
|
||||||
return copyMap;
|
return copyMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashMap shallowCopy(HashMap originalMap) {
|
public static <K, V> HashMap<K, V> shallowCopy(HashMap<K, V> originalMap) {
|
||||||
return (HashMap) originalMap.clone();
|
return (HashMap<K, V>) originalMap.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashMap deepCopy(HashMap originalMap) {
|
public static <K, V> HashMap<K, V> deepCopy(HashMap<K, V> originalMap) {
|
||||||
return SerializationUtils.clone(originalMap);
|
return SerializationUtils.clone(originalMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package com.baeldung.copyinghashmap;
|
package com.baeldung.copyinghashmap;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -21,7 +20,7 @@ public class CopyHashMapUnitTest {
|
||||||
map.put("emp1",emp1);
|
map.put("emp1",emp1);
|
||||||
map.put("emp2",emp2);
|
map.put("emp2",emp2);
|
||||||
|
|
||||||
HashMap shallowCopy = CopyHashMap.shallowCopy(map);
|
HashMap<String, Employee> shallowCopy = CopyHashMap.shallowCopy(map);
|
||||||
|
|
||||||
assertThat(shallowCopy).isNotSameAs(map);
|
assertThat(shallowCopy).isNotSameAs(map);
|
||||||
|
|
||||||
|
@ -36,7 +35,7 @@ public class CopyHashMapUnitTest {
|
||||||
map.put("emp1",emp1);
|
map.put("emp1",emp1);
|
||||||
map.put("emp2",emp2);
|
map.put("emp2",emp2);
|
||||||
|
|
||||||
HashMap shallowCopy = CopyHashMap.shallowCopy(map);
|
HashMap<String, Employee> shallowCopy = CopyHashMap.shallowCopy(map);
|
||||||
|
|
||||||
emp1.setName("Johny");
|
emp1.setName("Johny");
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ public class CopyHashMapUnitTest {
|
||||||
Employee emp2 = new Employee("Norman");
|
Employee emp2 = new Employee("Norman");
|
||||||
map.put("emp1",emp1);
|
map.put("emp1",emp1);
|
||||||
map.put("emp2",emp2);
|
map.put("emp2",emp2);
|
||||||
HashMap deepCopy = CopyHashMap.deepCopy(map);
|
HashMap<String, Employee> deepCopy = CopyHashMap.deepCopy(map);
|
||||||
|
|
||||||
emp1.setName("Johny");
|
emp1.setName("Johny");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue