EnumMap putAll(map) Method in Java
Last Updated :
13 Jul, 2018
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:
import java.util.*;
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 );
mp1.put(gfg.Global_today, 799 );
mp1.put(gfg.India_today, 69 );
mp2.putAll(mp1);
System.out.println( "Mappings in Map1: " + mp1);
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:
import java.util.*;
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 );
mp1.put(gfg.India_today, "61.8%" );
mp1.put(gfg.United_States_today, "18.2%" );
mp2.putAll(mp1);
System.out.println( "Mappings in Map1: " + mp1);
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%}
Share your thoughts in the comments
Please Login to comment...