add one map to another
@@ -28,6 +28,24 @@ public class MergeMaps {
streamOf();
streamEx();
+
+ streamMerge();
+ }
+
+ private static void streamMerge() {
+
+ Map<String, Employee> map3 = map2.entrySet()
+ .stream()
+ .collect(
+ Collectors.toMap(
+ Map.Entry::getKey,
+ Map.Entry::getValue,
+ (v1, v2) -> new Employee(v1.getId(), v2.getName()),
+ () -> new HashMap<>(map1)
+ )
+ );
+
+ System.out.println(map3);
}
private static void streamEx() {
@@ -75,7 +93,6 @@ public class MergeMaps {
}
-
private static void initialize() {
Employee employee1 = new Employee(1L, "Henry");
map1.put(employee1.getName(), employee1);
