Skip to content
Related Articles

Related Articles

Improve Article

EnumMap putAll(map) Method in Java

  • Last Updated : 13 Jul, 2018
Geek Week

The Java.util.EnumMap.putAll(map) method in Java is used to copy all the mappings from one map to a newer one. Older mappings are replaced in a newer one.

Syntax:

void putAll(map)

Parameters: The method takes one parameter map. It is the map which is to be copied to the newer one.

Return Value The method does not return any value.

Below programs illustrate the putAll() method:

Program 1:






// Java program to demonstrate keySet()
import java.util.*;
  
// An enum of geeksforgeeks
public enum gfg {
    Global_today,
    India_today,
    China_today
};
  
class Enum_demo {
    public static void main(String[] args)
    {
  
        EnumMap<gfg, Integer> mp1 = 
                     new EnumMap<gfg, Integer>(gfg.class);
  
        EnumMap<gfg, Integer> mp2 = 
                     new EnumMap<gfg, Integer>(gfg.class);
  
        // Values are associated
        mp1.put(gfg.Global_today, 799);
        mp1.put(gfg.India_today, 69);
  
        // Copies all the mappings of mp1 to mp2
        mp2.putAll(mp1);
  
        // Prints the first map
        System.out.println("Mappings in Map1: " + mp1);
  
        // Prints the second map
        System.out.println("Mappings in Map2: " + mp2);
    }
}
Output:
Mappings in Map1: {Global_today=799, India_today=69}
Mappings in Map2: {Global_today=799, India_today=69}

Program 2:




// Java program to demonstrate the working of keySet()
import java.util.*;
  
// an enum of geeksforgeeks
// visitors in India and United States
public enum gfg {
  
    India_today,
    United_States_today
}
;
  
class Enum_demo {
    public static void main(String[] args)
    {
  
        EnumMap<gfg, String> mp1 = 
                     new EnumMap<gfg, String>(gfg.class);
  
        EnumMap<gfg, String> mp2 = 
                     new EnumMap<gfg, String>(gfg.class);
  
        // Values are associated
        mp1.put(gfg.India_today, "61.8%");
        mp1.put(gfg.United_States_today, "18.2%");
  
        // Copies all the mappings of mp1 to mp2
        mp2.putAll(mp1);
  
        // Prints the first map
        System.out.println("Mappings in Map1: " + mp1);
  
        // Prints the second map
        System.out.println("Mappings in Map2: " + mp2);
    }
}
Output:
Mappings in Map1: {India_today=61.8%, United_States_today=18.2%}
Mappings in Map2: {India_today=61.8%, United_States_today=18.2%}

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :