Java HashMap putAll() Method
Example
Copy entries from one map into another:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
HashMap<String, String> moreCities = new HashMap<String, String>();
moreCities.put("Canada", "Ottawa");
moreCities.put("Japan", "Tokyo");
capitalCities.putAll(moreCities);
System.out.println(capitalCities);
}
}
Definition and Usage
The putAll()
method writes all of the entries from another map into the map. If entries exist with the same keys then the values of these entries will be changed.
Syntax
One of the following:
public void putAll(Map map)
K
and V
refer to the data types of the keys and values of the map.
Parameter Values
Parameter | Description |
---|---|
map | Required. Another map containing entries to be added to the map. |
Technical Details
Throws: |
NullPointerException - If the map argument is null .
|
---|
Related Pages
❮ HashMap Methods