![]() |
VOOZH | about |
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.
HashMap: {1=A, 2=B}
Updated HashMap: {1=A, 2=B, 3=C, 4=D}
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.
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: