Java Collection Tutorial - Java HashMap.putAll(Map <? extends K ,? extends V > m)








Syntax

HashMap.putAll(Map <? extends K ,? extends V > m) has the following syntax.

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

Example

In the following code shows how to use HashMap.putAll(Map <? extends K ,? extends V > m) method.

/*  ww w .  j a v a  2  s  .  c o m*/
import java.util.HashMap;

public class Main {
   public static void main(String args[]) {

      
      HashMap<Integer, String> newmap2 = new HashMap<Integer, String>();      
      HashMap<Integer, String> newmap1 = new HashMap<Integer, String>();      
      
      // populate hash map
      newmap1.put(1, "tutorials");
      newmap1.put(2, "from");
      newmap1.put(3, "java2s.com");
      
      System.out.println("Values in newmap1: "+ newmap1);
      
      // put all values in newmap2
      newmap2.putAll(newmap1);

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

The code above generates the following result.