forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaps.java
More file actions
42 lines (29 loc) · 1.12 KB
/
Copy pathMaps.java
File metadata and controls
42 lines (29 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package modern.challenge;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class Maps {
private Maps() {
throw new AssertionError("Cannot be instantiated");
}
public static <K, V> Map<K, V> mergeMapsV1(Map<K, V> map1, Map<K, V> map2) {
if (map1 == null || map2 == null) {
throw new IllegalArgumentException("None of the maps can be null");
}
Map<K, V> map = new HashMap<>(map1);
map2.forEach(
(key, value) -> map.merge(key, value, (v1, v2) -> v2));
return map;
}
public static <K, V> Map<K, V> mergeMapsV2(Map<K, V> map1, Map<K, V> map2) {
if (map1 == null || map2 == null) {
throw new IllegalArgumentException("None of the maps can be null");
}
Stream<Map.Entry<K, V>> combined
= Stream.concat(map1.entrySet().stream(), map2.entrySet().stream());
Map<K, V> map = combined.collect(
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2));
return map;
}
}