Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

putAll()

Example

Transfer entries from one map to 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 copies all entries from another map into the current map. If entries with the same keys exist, their values will be overwritten.

Syntax

Any of the following:

public void putAll(Map map)

K and V denote the data types of the keys and values in the map.

Parameter Values

Parameter

Description

map

Required: Another map containing entries to be added to the current map.

Technical Details

throws:

NullPointerException: If the provided map argument is null.