Java.util.EnumMap.putAll() Method


Advertisements


Description

The java.util.EnumMap.putAll(Map<? extends K,? extends V> m) method copies all of the mappings from the specified map to this map. Older values are replaced.

Declaration

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

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

Parameters

  • m -- the mappings to be stored in this map

Return Value

This method does not return any value.

Exception

  • NullPointerException -- the specified map is null, or if one or more keys in the specified map are null

Example

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

package com.tutorialspoint;

import java.util.*;

public class EnumMapDemo {

   // create an enum
   public enum Numbers {

   ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {
   EnumMap<Numbers, String> map1 =
   new EnumMap<Numbers, String>(Numbers.class);
   EnumMap<Numbers, String> map2 =
   new EnumMap<Numbers, String>(Numbers.class);

   // associate values in map1
   map1.put(Numbers.ONE, "1");
   map1.put(Numbers.TWO, "2");
   map1.put(Numbers.THREE, "3");
   map1.put(Numbers.FOUR, "4");

   // print the maps
   System.out.println("Map1: " + map1);
   System.out.println("Map2: " + map2);

   // put all mapping from map1 to map 2
   map2.putAll(map1);

   // print the maps
   System.out.println("Map1: " + map1);
   System.out.println("Map2: " + map2);
   }
}

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

Map1: {ONE=1, TWO=2, THREE=3, FOUR=4}
Map2: {}
Map1: {ONE=1, TWO=2, THREE=3, FOUR=4}
Map2: {ONE=1, TWO=2, THREE=3, FOUR=4}

java_util_enummap.htm

Advertisements