Java HashMap.putAll()

In this tutorial, we will learn about the Java HashMap.putAll() function, and learn how to use this function to copy mappings from the specified map to this map, with the help of examples.

putAll()

HashMap.putAll() copies all of the mappings from the given map (as argument) to this HashMap.

If a mapping with specific key from m is already present in this HashMap, then the value for the key is updated in this HashMap.

The syntax of putAll() function is

</>
Copy
putAll(m)

where

ParameterDescription
mA map (of key-value pairs) to be put in this HashMap.
Map<? extends K, ? extends V>

Returns

The function returns void.

Examples

1. putAll() basic example

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 put all the mappings from hashMap2 into the hashMap1 by calling putAll() on hashMap1 with hashMap2 passed as argument. Since, all the keys of hashMap2 are not present in hashMap1, all the mappings of hashMap2 will be put in hashMap1.

Java Program

</>
Copy
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");
		hashMap1.put(4, "D");
		System.out.println("HashMap 1  : " + hashMap1);
		
		HashMap<Integer, String> hashMap2 = new HashMap<>();
		hashMap2.put(5, "E");
		hashMap2.put(6, "F");
		hashMap2.put(7, "G");
		System.out.println("HashMap 2  : " + hashMap2);
		
		hashMap1.putAll(hashMap2);
		System.out.println("HashMap 1 after putAll() : " + hashMap1);
	}
}

Output

HashMap 1  : {1=A, 2=B, 3=C, 4=D}
HashMap 2  : {5=E, 6=F, 7=G}
HashMap 1 after putAll() : {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G}

2. putAll() – Map with same keys

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

We put all the mappings from hashMap2 into the hashMap1 by calling putAll() on hashMap1 with hashMap2 passed as argument. Since, the keys of hashMap2 , 3 and 4 are present in hashMap1, the mappings of hashMap2 will update the mappings in hashMap1.

Java Program

</>
Copy
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");
		hashMap1.put(4, "D");
		System.out.println("HashMap 1  : " + hashMap1);
		
		HashMap<Integer, String> hashMap2 = new HashMap<>();
		hashMap2.put(3, "E");
		hashMap2.put(4, "F");
		System.out.println("HashMap 2  : " + hashMap2);
		
		hashMap1.putAll(hashMap2);
		System.out.println("HashMap 1 after putAll() : " + hashMap1);
	}
}

Output

HashMap 1  : {1=A, 2=B, 3=C, 4=D}
HashMap 2  : {3=E, 4=F}
HashMap 1 after putAll() : {1=A, 2=B, 3=E, 4=F}

3. putAll() – When given argument is null

In this example, we will initialize two HashMaps hashMap1 and hashMap2. hashMap1 has mappings in it and hashMap2 is null.

When we pass a null HashMap object to putAll() method, it throws java.lang.NullPointerException.

Java Program

</>
Copy
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");
		hashMap1.put(4, "D");
		System.out.println("HashMap 1  : " + hashMap1);
		
		HashMap<Integer, String> hashMap2 = null;
		
		hashMap1.putAll(hashMap2);
		System.out.println("HashMap 1 after putAll() : " + hashMap1);
	}
}

Output

HashMap 1  : {1=A, 2=B, 3=C, 4=D}
Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.HashMap.putMapEntries(Unknown Source)
	at java.base/java.util.HashMap.putAll(Unknown Source)
	at Example.main(Example.java:14)

Conclusion

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