VOOZH about

URL: https://www.geeksforgeeks.org/java/hashmap-putall-method-in-java/

⇱ Java HashMap putAll() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java HashMap putAll() Method

Last Updated : 11 Jul, 2025

The putAll() method of the Java HashMap class is used to copy all key-value mappings from another map to the existing map. If a key exists in both maps, its value is updated with the value from the source map. Otherwise, new key-value pairs are added.

Example 1: This example demonstrates copying all the key-value pairs from one HashMap to another.


Output
HashMap: {1=A, 2=B}
Updated HashMap: {1=A, 2=B, 3=C, 4=D}

Syntax of HashMap putAll() Method

public void putAll(Map map)

Parameter: The map whose mapping are copied into the HashMap.

Example 2: This example demonstrates If a key exists in both maps, its value is updated with the value from the source map.


Output
HashMap: {1=Geeks, 2=For}
Updated HashMap: {1=Geeks, 2=Geeks, 3=Java}


Example 3: The below Java program demonstrates calling putAll(null) on a HashMap will throw a NullPointerException.

Output:

👁 Output
Comment