Java HashMap.putAll() – Examples

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(Map<K, V> m)

HashMap.putAll() copies all of the mappings from the specified map 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.

Syntax

The syntax of putAll() function is

putAll(Map<? extends K,? extends V> m)

where

ParameterDescription
mMap (of key-value pairs) to be put in this HashMap.

Returns

The function returns void.

Example 1 – putAll() – Map with New Keys

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

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}

Example 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 will 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

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}

Example 3 – putAll() – Null Map

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

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.