Java 8 - Merging Two Maps Example

In this example, we’ll demonstrate how to merge two maps using the Java 8 capabilities.
We will demonstrate merging two maps with different approaches.
  1. Map.merge()
  2. Stream.concat()
  3. Stream.of()
  4. Simple Streaming
Java 8 adds a new merge() function into the java.util.Map interface.

Merging Two Maps Example

package net.javaguides.hibernate;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MergeMaps {

    private static Map < String, Employee > map1 = new HashMap < > ();
    private static Map < String, Employee > map2 = new HashMap < > ();

    public static void main(String[] args) {

        initialize();

        mergeFunction();

        streamConcat();

        streamOf();

        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 streamOf() {
        Map < String, Employee > map3 = Stream.of(map1, map2)
            .flatMap(map - > map.entrySet().stream())
            .collect(
                Collectors.toMap(
                    Map.Entry::getKey,
                    Map.Entry::getValue,
                    (v1, v2) - > new Employee(v1.getId(), v2.getName())
                )
            );

        map3.entrySet().forEach(System.out::println);
    }

    private static void streamConcat() {
        Map < String, Employee > result = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream()).collect(Collectors.toMap(
            Map.Entry::getKey,
            Map.Entry::getValue,
            (value1, value2) - > new Employee(value2.getId(), value1.getName())
        ));

        result.entrySet().forEach(System.out::println);
    }

    private static void mergeFunction() {
        Map < String, Employee > map3 = new HashMap < > (map1);

        map2.forEach(
            (key, value) - > map3.merge(key, value, (v1, v2) - >
                new Employee(v1.getId(), v2.getName()))
        );

        map3.entrySet().forEach(System.out::println);
    }


    private static void initialize() {
        Employee employee1 = new Employee(1 L, "Henry");
        map1.put(employee1.getName(), employee1);
        Employee employee2 = new Employee(22 L, "Annie");
        map1.put(employee2.getName(), employee2);
        Employee employee3 = new Employee(8 L, "John");
        map1.put(employee3.getName(), employee3);

        Employee employee4 = new Employee(2 L, "George");
        map2.put(employee4.getName(), employee4);
        Employee employee5 = new Employee(3 L, "Henry");
        map2.put(employee5.getName(), employee5);
    }

}

class Employee {

    private Long id;
    private String name;

    public Employee(Long id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
Output
John=net.javaguides.hibernate.Employee@776ec8df
Annie=net.javaguides.hibernate.Employee@4eec7777
George=net.javaguides.hibernate.Employee@3b07d329
Henry=net.javaguides.hibernate.Employee@41629346
George=net.javaguides.hibernate.Employee@3b07d329
John=net.javaguides.hibernate.Employee@776ec8df
Annie=net.javaguides.hibernate.Employee@4eec7777
Henry=net.javaguides.hibernate.Employee@27d6c5e0
George=net.javaguides.hibernate.Employee@3b07d329
John=net.javaguides.hibernate.Employee@776ec8df
Annie=net.javaguides.hibernate.Employee@4eec7777
Henry=net.javaguides.hibernate.Employee@5b480cf9
{John=net.javaguides.hibernate.Employee@776ec8df,
 Annie=net.javaguides.hibernate.Employee@4eec7777,
 George=net.javaguides.hibernate.Employee@3b07d329, 
Henry=net.javaguides.hibernate.Employee@2f4d3709}



Comments