How to Merge Two HashMaps in Java while Handling Conflicts? Last Updated : 21 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 ConflictsJava typically merges and resolves the conflict between HasMap by different methods: Using put() and putAll()Using a merge() methodProgram to Merge Two HashMaps While Handling Conflicts in JavaBelow 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 // 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 map1.putAll(map2); System.out.println("The resultant map is : " + map1); } } OutputThe 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() methodWhen the merge() method is used to merge two HashMap, we can specify a merging function to handle conflicts while merging two HashMap. Java // 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. map2.forEach( (key, value) -> map1.merge(key, value, Integer::sum)); } } OutputThe 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. Comment More infoAdvertise with us Next Article How to Merge Two HashMaps in Java while Handling Conflicts? P priyasha2323 Follow Improve Article Tags : Java Java Programs Java Examples Practice Tags : Java Similar Reads How to Convert Two Arrays Containing Keys and Values to HashMap in Java? HashMap is part of the Collections framework of java. It stores the data in the form of key-value pairs. These values of the HashMap can be accessed by using their respective keys or the key-value pairs can be accessed using their indexes (of Integer type). HashMap is similar to Hashtable in java. T 2 min read How to Copy One HashMap to Another HashMap in Java? HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to 4 min read Program to Convert HashMap to TreeMap in Java HashMap is a part of Javaâs collection since Java 1.2. It provides the basic implementation of Map interface of Java which stores the data in (Key, Value) pairs. To access a value in HashMap, one must know its key. HashMap is known as HashMap because it uses a technique Hashing for storage of data. 6 min read How does HashMap handle resizing when it reaches its capacity in Java? The capacity of a HashMap simply doubles when it reaches a certain threshold. The threshold depends on the initial capacity and the load factor of the HashMap. Consider Capacity as the number of spaces, a HashMap currently has to store elements and Load factor is a measure of how full the map is all 3 min read How to Merge Two LinkedHashMaps in Java? In Java, LinkedHashMap is a class that extends HashMap and maintains the order of elements based on the order of insertion. Merging two LinkedHashMaps involves combining their key-value pairs while ensuring that the order is preserved. In, this article we will explore different approaches to merging 3 min read Creating HashMap from Other Maps in Java Map interface present in java.util package represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore, it behaves a bit differently from the rest of the collection types. A map contains unique keys. There are three main types of maps in 3 min read How to Implement a Bidirectional Map Using Two HashSets in Java? Bidirectional maps are also known as two-way maps or dual maps. These provide a convenient way to establish a relationship between keys and values in both directions. In Java, we can implement these using HashSet. So, in this article, we will see how to implement bidirectional maps in Java using two 4 min read How to Eliminate Duplicate Keys in Hashtable in Java? HashTable class is part of the Collection framework in Java where the only major difference it has from HashMap is that it's synchronized. Hash table maps keys to values i.e. it internally uses buckets to store key-value pairs and the corresponding bucket to a key-value pair is determined by the key 4 min read How to handle Collisions when using a Custom Hash Function in a HashMap? A HashMap is a data structure that stores key-value pairs. So, the values ââcan be efficiently retrieved based on their associated keys. Hash functions are used to map keys to indices in an array. The Collisions occur when two or more different keys hash into the same index, resulting in potential d 5 min read Java Program to Sort a HashMap by Keys and Values HashMap<K, V> is a Java Collection and is a part of java.util package. It provides the basic implementation of the Map interface of Java. It stores the data in the form of Key, Value pairs, where the keys must be unique but there is no restriction for values. If we try to insert the duplicate 3 min read Like