Merge two maps in Java
This post will discuss how to merge two maps of the same type in Java. The solution returns a new map, containing all mappings in both maps. The merge operation will handle common keys present in both maps.
1. Using putAll() method
A simple solution is to use the Map.putAll() method to copy all mappings from the original map to another map.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Integer, Character> hm1 = new HashMap<>(); hm1.put(1, 'A'); hm1.put(2, 'B'); Map<Integer, Character> hm2 = new HashMap<>(); hm2.put(3, 'C'); hm2.put(4, 'D'); Map<Integer, Character> map = new HashMap<>(); map.putAll(hm1); map.putAll(hm2); System.out.println(map); } } |
Output:
{1=A, 2=B, 3=C, 4=D}
Note that if a key k exists in both maps, the value in hm2 will overwrite the value in hm1. i.e., map[k] = hm2[k]. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Integer, Character> hm1 = new HashMap<>(); hm1.put(1, 'A'); hm1.put(2, 'B'); Map<Integer, Character> hm2 = new HashMap<>(); hm2.put(3, 'C'); hm2.put(2, 'D'); Map<Integer, Character> map = new HashMap<>(); map.putAll(hm1); map.putAll(hm2); System.out.println(map); } } |
Output:
{1=A, 2=D, 3=C}
We can avoid making an extra call to the putAll() method by passing the first map to the HashMap constructor.
|
1 2 |
Map<Integer, Character> map = new HashMap<>(hm1); map.putAll(hm2); |
If you need to add contents of a map hm1 to map hm2, you can just do:
|
1 |
hm1.putAll(hm2); |
2. Using Guava
Guava also provides a builder for creating immutable map instances using the putAll() method, whose successive method invocations can be chained:
|
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 |
import com.google.common.collect.ImmutableMap; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Integer, Character> hm1 = new HashMap<>(); hm1.put(1, 'A'); hm1.put(2, 'B'); Map<Integer, Character> hm2 = new HashMap<>(); hm2.put(3, 'C'); hm2.put(4, 'D'); Map<Integer, Character> map = ImmutableMap.<Integer, Character>builder() .putAll(hm1) .putAll(hm2) .build(); System.out.println(map); } } |
Output:
{1=A, 2=B, 3=C, 4=D}
3. Using Java 8
In Java 8 and above, you can get the stream of elements of both maps, and call the Collectors.toMap() to collect all the map elements into a new map by applying the specified mapping functions to it, to help identify the keys and values. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Map<Integer, Character> hm1 = new HashMap<>(); hm1.put(1, 'A'); hm1.put(2, 'B'); Map<Integer, Character> hm2 = new HashMap<>(); hm2.put(3, 'C'); hm2.put(4, 'D'); Map<Integer, Character> map = Stream.of(hm1, hm2) .flatMap(m -> m.entrySet().stream()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); System.out.println(map); } } |
Output:
{1=A, 2=B, 3=C, 4=D}
The above code throws java.lang.IllegalStateException if a duplicate key is present in both maps. To handle this, provide a merge function to resolve collisions between values associated with the same key.
The following code provides a BinaryOperator to merge values for duplicate keys, where the old value takes precedence over the new value in the stream.
|
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 |
import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Map<Integer, Character> hm1 = new HashMap<>(); hm1.put(1, 'A'); hm1.put(2, 'B'); Map<Integer, Character> hm2 = new HashMap<>(); hm2.put(2, 'C'); hm2.put(3, 'D'); Map<Integer, Character> map = Stream.of(hm1, hm2) .flatMap(m -> m.entrySet().stream()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue)); System.out.println(map); } } |
Output:
{1=A, 2=B, 3=D}
That’s all about merging two maps of the same types in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)