Open In App

How to Merge Two HashMaps in Java while Handling Conflicts?

Last Updated : 21 Feb, 2024
Like Article

HashMap is a part of the collection framework found in Java. It is found in the java.util package and provides the basic implementation of the Map interface of Java. Conflict handling is very important in HashMap as if they are ignored the code will not work properly.

In this article, we will learn how to merge two HashMaps in Java while handling conflicts.

Approaches to Merge Two HashMaps while Handling Conflicts

Java typically merges and resolves the conflict between HasMap by different methods:

  • Using put() and putAll()
  • Using a merge() method

Program to Merge Two HashMaps While Handling Conflicts in Java

Below is the code implementation of the two approaches.

Approach 1: Using put() and putAll()

When we have two or more HashMap and want to merge them then we do that using put() and putAll() methods to merge two, the key which is present in both the HashMap.


// Java program to merge two HashMaps while handling conflicts using put() and putAll()
import java.util.*;
class GFG {
    public static void main(String[] args)
        // creating two maps to store key-value pair
        HashMap<String, Integer> map1 = new HashMap<>();
        HashMap<String, Integer> map2 = new HashMap<>();
        // adding values to the two maps
        // adding similar key in the two maps
        map1.put("key", 1);
        map2.put("key", 5);
        map1.put("Key1", 2);
        map2.put("key2", 4);
        map2.put("Key2", 4);
        // using the putAll function to add all elements in
        // one map
        System.out.println("The resultant map is : " + map1);


The resultant map is : {Key2=4, Key1=2, key2=4, key=5}

Explanation of the above Program:

  • Initially two hashmap has been created.
  • One key-value pair have been added which is same initially in both the maps.
  • The putAll() is used to merge both the maps.
  • The map2 key-value which is already present in map1 replaces the initial value.

Approach 2: Using merge() method

When the merge() method is used to merge two HashMap, we can specify a merging function to handle conflicts while merging two HashMap.


// Java program to merge two HashMaps
// While handling conflicts using merge()
import java.util.*;
// Driver Class
class GFG {
      // Main Function
    public static void main(String[] args)
        HashMap<String, Integer> map1 = new HashMap<>();
        HashMap<String, Integer> map2 = new HashMap<>();
        map1.put("key", 11);
        map1.put("key1", 2);
        map1.put("key3", 13);
        map2.put("key", 10);
        map2.put("key4", 5);
        map2.put("key5", 14);
        // Function for merging two maps
        mergeFun(map1, map2);
        System.out.println("The resultant map is : "
                           + map1);
    public static void
    mergeFun(HashMap<String, Integer> map1,
             HashMap<String, Integer> map2)
        // Adding values for same keys.
            (key, value)
                -> map1.merge(key, value, Integer::sum));


The resultant map is : {key1=2, key5=14, key3=13, key4=5, key=21}

Explanation of the Program:

  • Initially two hashmaps are created and values are updated in the two maps.
  • Then we have used the merge() function and specify to add the two values which have the same key in the merged map.
  • The code updates the sum of the two values in the merged map.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads