Add contents of a Map to another Map in Java
This post will discuss how to add the contents of a Map to another Map in Java.
1. Using putIfAbsent() method
In Java 8, you can use Map.forEach() to iterate over each entry of the map and invoke the putIfAbsent() method for each mapping. The putIfAbsent() method associates the specified key with the specified value if the key is not already present in the map.
This method is particularly useful to copy all mappings of a map to another, and not replace the existing key-value mappings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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'); hm2.forEach((k, v) -> hm1.putIfAbsent(k, v)); System.out.println(hm1); } } |
Output:
{1=A, 2=B, 3=C, 4=D}
You can further shorten the code by passing the method reference of the putIfAbsent() method to forEach().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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'); hm2.forEach(hm1::putIfAbsent); System.out.println(hm1); } } |
Output:
{1=A, 2=B, 3=C, 4=D}
2. Using merge() method
Another alternative would be to use the merge() method to associate the specified key with a non-null value. It takes the remapping function to recompute the value if already present.
The following code example iterates over the map’s entries and merges its mappings into another map using the merge() method. The remapping function states that the old value takes precedence over the new value in case the key is already present.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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(2, 'C'); hm2.put(3, 'D'); for (Map.Entry<Integer, Character> entry : hm2.entrySet()) { hm1.merge(entry.getKey(), entry.getValue(), (oldval, newval) -> oldval); } System.out.println(hm1); } } |
Output:
{1=A, 2=B, 3=D}
The above code can be easily shortened using the Stream API:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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(2, 'C'); hm2.put(3, 'D'); hm2.forEach((k, v) -> hm1.merge(k, v, (oldval, newval) -> oldval)); System.out.println(hm1); } } |
Output:
{1=A, 2=B, 3=D}
3. Using put() method
You can also iterate over each entry in the map and invoke the put() method for each mapping if and only if the key is not already present in the other map.
|
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; 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'); hm2.forEach((key, value) -> { if (!hm1.containsKey(key)) { hm1.put(key, value); } }); System.out.println(hm1); } } |
Output:
{1=A, 2=B, 3=D}
That’s all about adding the contents of a Map to another Map 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 :)