Java HashMap.merge()

In this tutorial, we will learn about the Java HashMap.merge() function, and learn how to use this function with the help of examples.

merge()

HashMap.merge() associates key with the given non-null value, if the specified key is not already associated with a value or is associated with null.

The syntax of merge() function is

merge(key, value, remappingFunction)

where

ParameterDescription
keyA key.
valueA value.
remappingFunctionA function that takes two values as arguments and returns a value.

Returns

The function returns V

ADVERTISEMENT

Examples

1. merge() basic example

In this example, we will initialize a HashMap hashMap1 with keys 1, 2 and 3. We will take a key value pair 3, "DDD" and merge this with haspMap1 using mapping function which assigns the largest of the values for given key 3: "DDD" from the given value and "C" from hashMap1.

Java Program

import java.util.HashMap;

public class Example {  
	public static void main(String[] args) {  
		
		 HashMap<Integer,String> hashMap1=new HashMap<>();
		 hashMap1.put(1,"A");
		 hashMap1.put(2,"B");
		 hashMap1.put(3,"C");
		 System.out.println("HashMap 1 : " + hashMap1);

		 hashMap1.merge(3, "DDD", (v1, v2) -> v1.length() > v2.length() ? v1 : v2); 
		 System.out.println("HashMap 1 after Merge : " + hashMap1);
    }  
}

Output

HashMap 1 : {1=A, 2=B, 3=C}
HashMap 1 after Merge : {1=A, 2=B, 3=DDD}

2. merge() – Merge entries of one HashMap with another (Keys are not present)

In this example, we will initialize two HashMaps hashMap1 and hashMap2. We will take the mappings in these HashMaps such that no key of hashMap2 is present in hashMap1.

We will use merge() method to append the values from hashMap2 to that of hashMap1 for similar keys. The keys that are not present in hashMap1, but present in hashMap2 will be added to hashMap1.

Java Program

import java.util.HashMap;

public class Example {  
	public static void main(String[] args) {  
		
		 HashMap<Integer,String> hashMap1 = new HashMap<>();
		 hashMap1.put(1,"A");
		 hashMap1.put(2,"B");
		 hashMap1.put(3,"C");
		 System.out.println("HashMap 1 : " + hashMap1);
		 
		 HashMap<Integer,String> hashMap2 = new HashMap<>();
		 hashMap2.put(1,"D");
		 hashMap2.put(2,"E");
		 hashMap2.put(3,"F");
		 System.out.println("HashMap 2 : " + hashMap2);
		 
		 hashMap2.forEach((key, value) -> hashMap1.merge( key, value,(v1, v2) 
				           -> v1.equalsIgnoreCase(v2) ? v1 : v1 + v2)); 
		 System.out.println("HashMap 1 after Merge : " + hashMap1);
    }  
}

Output

HashMap 1 : {1=A, 2=B, 3=C}
HashMap 2 : {1=D, 2=E, 3=F}
HashMap 1 after Merge : {1=AD, 2=BE, 3=CF}

3. merge() – Merge entries of one HashMap with another (Keys are present)

In this example, we will initialize two HashMaps hashMap1 and hashMap2. We will take the mappings in these HashMaps such that the keys of hashMap2 are not present in hashMap1.

We will use merge() method to append the values from hashMap2 to that of hashMap1 for similar keys. The keys that are not present in hashMap1, but present in hashMap2 will be added to hashMap1.

Java Program

import java.util.HashMap;

public class Example {  
	public static void main(String[] args) {  
		
		 HashMap<Integer,String> hashMap1 = new HashMap<>();
		 hashMap1.put(1,"A");
		 hashMap1.put(2,"B");
		 hashMap1.put(3,"C");
		 System.out.println("HashMap 1 : " + hashMap1);
		 
		 HashMap<Integer,String> hashMap2 = new HashMap<>();
		 hashMap2.put(4,"D");
		 hashMap2.put(5,"E");
		 hashMap2.put(6,"F");
		 System.out.println("HashMap 2 : " + hashMap2);
		 
		 hashMap2.forEach((key, value) -> hashMap1.merge( key, value,(v1, v2) 
				           -> v1.equalsIgnoreCase(v2) ? v1 : v1 + v2)); 
		 System.out.println("HashMap 1 after Merge : " + hashMap1);
    }  
}

Output

HashMap 1 : {1=A, 2=B, 3=C}
HashMap 2 : {4=D, 5=E, 6=F}
HashMap 1 after Merge : {1=A, 2=B, 3=C, 4=D, 5=E, 6=F}

Conclusion

In this Java Tutorial, we have learnt the syntax of Java HashMap.merge() function, and also learnt how to use this function with the help of examples.