In Java, putAll(m) copies every mapping from the map m into this HashMap. It has the same effect as calling put(k, v) for each entry of m.
Keys that exist only in this map are kept. Keys that exist in both end up with the value from m: the old value is overwritten, not combined. The source map is not changed.
An instance method of java.util.HashMap (class since Java 1.2): a hash table of key/value pairs with no guaranteed order, one null key, any number of null values, and no thread safety.
Syntax
Pass any Map whose keys and values fit this map's types.
Java
Map<String, String> settings = new HashMap<>();
settings.putAll(defaults); // copy the defaults
settings.putAll(user); // user values overwrite matching keys
Call
What it does
map.putAll(other)
Adds or overwrites every mapping from other.
new HashMap<>(other)
Creates a new map with other's mappings.
other.forEach((k, v) -> map.merge(k, v, f))
Combines values instead of overwriting.
map.putAll(new HashMap<>())
Copying an empty map changes nothing.
There is no return value. To know how many keys were new, compare size() before and after the call.
Library declarations
Java
// java.util.HashMap<K, V>
public void putAll(Map<? extends K, ? extends V> m)
// Declared in Map<K, V>
Parameters
Parameter
Description
m (Map<? extends K, ? extends V>)
The mappings to copy. Must not be null; may contain null keys and values.
HashMap iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Start from defaults. The first putAll copies all three default settings into the empty settings map.
Later call wins. The second putAll copies theme=dark, overwriting light; the other two defaults are left alone.
Source untouched. Copying never changes the source map, so defaults still says light.
Copies may be sized differently.putAll into an empty map pre-sizes its table, and that sizing rule changed in JDK 19; these three keys were chosen because they print in the same order either way.
Example 2 — putAll Overwrites; merge Adds Up
Combine the stock of two shops: putAll replaces the shared item, while merge adds the quantities.
Java
import java.util.HashMap;
import java.util.Map;
public class HashMapPutAllOverwrite {
public static void main(String[] args) {
Map<String, Integer> shopA = new HashMap<>();
shopA.put("rice", 10);
shopA.put("salt", 4);
Map<String, Integer> shopB = new HashMap<>();
shopB.put("salt", 9);
shopB.put("sugar", 6);
Map<String, Integer> summed = new HashMap<>(shopA);
shopB.forEach((item, qty) -> summed.merge(item, qty, Integer::sum));
int before = shopA.size();
shopA.putAll(shopB);
System.out.println("putAll salt: " + shopA.get("salt"));
System.out.println("merge salt: " + summed.get("salt"));
System.out.println("new keys added by putAll: " + (shopA.size() - before));
System.out.println("rice kept: " + shopA.get("rice"));
}
}
Output
putAll salt: 9
merge salt: 13
new keys added by putAll: 1
rice kept: 10
How It Works
Summing copy first.summed starts as a copy of shop A, and merge(..., Integer::sum) adds each of shop B's quantities to it.
putAll replaces. Shop A's salt (4) is overwritten by shop B's 9; nothing is added together.
merge combines. In the summing copy, salt becomes 4 + 9 = 13.
Counting new keys. Only sugar was new to shop A, so the size grows from 2 to 3, and rice keeps its own value.
Example 3 — null Maps, Empty Maps, and null Entries
Pass a null map, an empty map, and a map containing a null key and a null value.
putAll(null) -> NullPointerException
after empty putAll: {Ann=5}
after nulls: {Ann=5, null=1, Bo=null} size 3
HashMap iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
No source at all.putAll needs a real map to read from, so a null argument throws NullPointerException.
Empty source. Copying an empty map is allowed and leaves scores exactly as it was.
null entries are fine. The source's null key and Bo's null value are copied like any other mapping, because HashMap accepts both.
Shared bucket. The size is 3. "Ann" happens to hash to bucket 0 as well, and it was stored earlier, so it prints before the null key.
Frequently Asked Questions
Call target.putAll(source) to copy all mappings, letting source values win. Use source.forEach((k, v) -> target.merge(k, v, f)) to combine values instead.
Yes. For every key in the argument map, the value in the argument replaces the existing value.
It throws NullPointerException.
No. It only reads from the source.
No. Keys and values are shared references, so mutable values are visible through both maps.