bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java HashMap putAll() Method

❮ HashMap Methods

Example

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.

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

ParameterDescription
mapRequired. Another map containing entries to be added to the map.

Technical Details

Throws:NullPointerException - If the map argument is null .

Previous

Java HashMap put() Method

Next

Java HashMap putIfAbsent() Method