In Java, putAll(m) copies every mapping from the map m into this TreeMap. The effect is the same as calling put(k, v) once for each entry of m: new keys are inserted in sorted order, and keys that already exist get the value from m.
The target map's ordering wins. It does not matter whether m is a HashMap, a LinkedHashMap, or a TreeMap sorted another way: after the copy, every key sits where this map's comparator puts it. The source map is not changed.
An instance method of java.util.TreeMap, part of TreeMap since Java 1.2. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe.
Syntax
Pass any Map whose key and value types fit this map.
Java
TreeMap<String, Integer> catalog = new TreeMap<>();
catalog.putAll(prices); // copy and sort
catalog.putAll(newPrices); // later values overwrite matching keys
Call
What it does
tree.putAll(hashMap)
Copies an unordered map and sorts its keys.
new TreeMap<>(otherMap)
Creates a sorted copy in one step.
tree.putAll(other)
Overwrites values for keys found in both maps.
tree.putAll(null)
Throws NullPointerException.
There is no return value. Compare size() before and after the call to find out how many keys were new.
Library declarations
Java
// java.util.TreeMap<K, V>
public void putAll(Map<? extends K, ? extends V> map)
// Declared in Map<K, V>
Parameters
Parameter
Description
map (Map<? extends K, ? extends V>)
The mappings to copy. Must not be null. Its keys must be comparable with this map's keys.
Copy a second price list into the catalog; the shared item takes the newer price.
Java
import java.util.TreeMap;
public class TreeMapPutAllCatalog {
public static void main(String[] args) {
TreeMap<String, Integer> catalog = new TreeMap<>();
catalog.put("lamp", 25);
catalog.put("desk", 120);
TreeMap<String, Integer> newArrivals = new TreeMap<>();
newArrivals.put("chair", 45);
newArrivals.put("lamp", 22);
newArrivals.put("shelf", 60);
catalog.putAll(newArrivals);
System.out.println(catalog);
System.out.println("lamp now costs " + catalog.get("lamp"));
System.out.println("newArrivals still has " + newArrivals.size() + " items");
}
}
Output
{chair=45, desk=120, lamp=22, shelf=60}
lamp now costs 22
newArrivals still has 3 items
How It Works
Two new products slot in. chair and shelf did not exist in the catalog, so they are inserted around desk and lamp in alphabetical order.
The shared product is overwritten. lamp appears in both maps; the copy behaves like put, so 22 from newArrivals replaces 25.
Read-only for the source.newArrivals keeps its three items; putAll only reads from the map you pass in.
Example 2 — Sort a HashMap by Copying It
City populations (in thousands) live in a HashMap; copying them into a TreeMap puts the cities in A-to-Z order.
Java
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class TreeMapPutAllSortHashMap {
public static void main(String[] args) {
Map<String, Integer> population = new HashMap<>();
population.put("Oslo", 709);
population.put("Bergen", 291);
population.put("Trondheim", 212);
population.put("Alta", 21);
TreeMap<String, Integer> sorted = new TreeMap<>();
sorted.putAll(population);
System.out.println("A to Z: " + sorted);
System.out.println("first city: " + sorted.firstKey() + ", last city: " + sorted.lastKey());
}
}
Output
A to Z: {Alta=21, Bergen=291, Oslo=709, Trondheim=212}
first city: Alta, last city: Trondheim
How It Works
Source order is irrelevant. The HashMap hands out its entries in hash order, but each one is placed by comparing city names, so the result is always the same.
Sorted by key, not by value. Trondheim (212) comes last although Oslo (709) is larger: a TreeMap orders keys only.
Ends of the range.firstKey() and lastKey() now give the alphabetically first and last city, something a HashMap cannot answer cheaply.
Example 3 — The Target Map's Comparator Decides the Order
Copy a year-sorted map into a map that sorts newest first, then pass null by mistake.