Java Utility Library

Java Hashtable - putAll() Method



The java.util.Hashtable.putAll() method is used to copy all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

Syntax

public void putAll(Map<? extends K,? extends V> t)

Here, K and V are the type of key and value respectively maintained by the container.


Parameters

t Specify mappings to be stored in this map.

Return Value

void type.

Exception

Throws NullPointerException, if the specified map is null.

Example:

In the example below, the java.util.Hashtable.putAll() method is used to copy all of the mappings from the Htable2 to Htable1.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating hashtables
    Hashtable<Integer, String> Htable1 = new Hashtable<Integer, String>();
    Hashtable<Integer, String> Htable2 = new Hashtable<Integer, String>();

    //populating Htable2
    Htable2.put(101, "John");
    Htable2.put(102, "Marry");
    Htable2.put(103, "Kim");
    Htable2.put(104, "Jo");

    //printing Htable1
    System.out.println("Before putAll, Htable1 contains: " + Htable1);    

    //copy mapping from Htable2 into Htable1
    Htable1.putAll(Htable2); 

    //printing Htable1
    System.out.println("After putAll, Htable1 contains: " + Htable1);  
  }
}

The output of the above code will be:

Before putAll, Htable1 contains: {}
After putAll, Htable1 contains: {104=Jo, 103=Kim, 102=Marry, 101=John}

❮ Java.util - Hashtable