How to Join or Merge Two Maps in Java

Learn merging two hashmaps in both cases – ignoring duplicate keys (overwrites the value) or handling duplicate keys.

1. Merge Two HashMaps Ignoring Duplicate Keys

This one is a simple solution. Use firstMap.putAll(secondMap) method that copies all of the mappings from the secondMap to firstMap.

As we know hashmap does not allow duplicate keys. So when we merge the maps in this way, for duplicate keys in firstMap the value is overwritten by the value for the same key in secondMap.

Let’s take an example. In the following example, both maps have an entry with key “4”. After merging, we have the entry from the second map in the final map.

//map 1
HashMap<Integer, String> firstMap = new HashMap<>();
firstMap.put(1, "A");
firstMap.put(2, "B");
firstMap.put(3, "C");
firstMap.put(4, "D");

//map 2
HashMap<Integer, String> secondMap = new HashMap<>();
secondMap.put(4, "F"); //It will replace D with F
secondMap.put(5, "G"); //A new pair to be added

//Merge maps
firstMap.putAll(secondMap);

System.out.println(firstMap);

Program output.

{1=A, 2=B, 3=C, 4=F, 5=G}

2. Merge Two Maps by Combining Values for Duplicate Keys

If we want to handle the cases where duplicate keys are present in the maps and we do not want to lose the data for any map and for any key. In this case, we can take the help of Map.merge() function added in Java 8.

The merge() function takes 3 arguments: key, value and a user-provided BiFunction to merge values for duplicate keys.

In our example, we want to append the values (from both maps) for a duplicate key “4”.

//map 1
HashMap<Integer, String> firstMap = new HashMap<>();
firstMap.put(1, "A");
firstMap.put(2, "B");
firstMap.put(3, "C");
firstMap.put(4, "D");

//map 2
HashMap<Integer, String> secondMap = new HashMap<>();
secondMap.put(4, "F"); //It will replace D with F
secondMap.put(5, "G"); //A new pair to be added

//Merge maps
secondMap.forEach((key, value) -> firstMap.merge(key, value, String::concat));

System.out.println(firstMap);

Program output.

{1=A, 2=B, 3=C, 4=DF, 5=G}

Notice the value of key "4". It had value D in the first map and F in the second map. In the merged map, it is a combined value as "DF".

We can write any sort of merge logic in provided BiFunction. For example, if we want to append the values and put a delimiter in between then we can write our own BiFunction

secondMap.forEach(
     (key, value) -> firstMap.merge(key, value, (v1, v2) -> v1.equalsIgnoreCase(v2) ? v1 : v1 + "," + v2)
);

Now the output is a concatenated value and a delimiter in between the values D and F.

{1=A, 2=B, 3=C, 4=D,F, 5=G}

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode