Java EnumMap - putAll() Method
The java.util.EnumMap.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> m)
Here, K and V are the type of key and value respectively maintained by the container.
Parameters
m |
Specify mappings to be stored in this map. |
Return Value
void type.
Exception
Throws NullPointerException, if the specified map is null, or if one or more keys in the specified map are null.
Example:
In the example below, the java.util.EnumMap.putAll() method is used to copy all of the mappings from the Map2 to Map1.
import java.util.*; public class MyClass { //creating an enum public enum weekday{ MON, TUE, WED, THU, FRI } public static void main(String[] args) { //creating enum maps EnumMap<weekday,Integer> Map1 = new EnumMap<weekday,Integer>(weekday.class); EnumMap<weekday,Integer> Map2 = new EnumMap<weekday,Integer>(weekday.class); //associate values in Map2 Map2.put(weekday.MON, 1); Map2.put(weekday.TUE, 2); Map2.put(weekday.WED, 3); Map2.put(weekday.THU, 4); Map2.put(weekday.FRI, 5); //printing Map1 System.out.println("Before putAll, Map1 contains: " + Map1); //copy mapping from Map2 into Map1 Map1.putAll(Map2); //printing Map1 System.out.println("After putAll, Map1 contains: " + Map1); } }
The output of the above code will be:
Before putAll, Map1 contains: {} After putAll, Map1 contains: {MON=1, TUE=2, WED=3, THU=4, FRI=5}
❮ Java.util - EnumMap