java.util.HashMap.putAll() Method


Advertisements


Description

The putAll(Map<? extends K,? extends V> m) method is used to copy all of the mappings from the specified map to this map.

Declaration

Following is the declaration for java.util.HashMap.putAll() method.

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

Parameters

  • m -- This is the mappings to be stored in this map.

Return Value

  • NA

Exception

  • NullPointerException -- This is thrown if the specified map is null.

Example

The following example shows the usage of java.util.HashMap.putAll()

package com.tutorialspoint;

import java.util.*;

public class HashMapDemo {
   public static void main(String args[]) {
   // create two hash maps
   HashMap newmap1 = new HashMap();
   HashMap newmap2 = new HashMap();
      
   // populate hash map
   newmap1.put(1, "tutorials");
   newmap1.put(2, "point");
   newmap1.put(3, "is best");
      
   System.out.println("Values in newmap1: "+ newmap1);
      
   // put all values in newmap2
   newmap2.putAll(newmap1);

   System.out.println("Values in newmap2: "+ newmap2);
   }    
}

Let us compile and run the above program, this will produce the following result.

Values in newmap1: {1=tutorials, 2=point, 3=is best}
Values in newmap2: {1=tutorials, 2=point, 3=is best}

java_util_hashmap.htm

Advertisements