- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java EnumMap putAll() Method
Description
The Java 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
Adding Multiple Values to an EnumMap of Enum, Integer Pairs Example
The following example shows the usage of Java EnumMap putAll() method to populate the EnumMap instance. We've created a enum Numbers. Then Two EnumMap instance are created of enum Numbers and Integer. Few entries are added and enumMap is printed. Using putAll() method, enumMap is populated with values of first enumMap and printed.
package com.tutorialspoint; import java.util.EnumMap; public class EnumMapDemo { // create an enum public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; public static void main(String[] args) { EnumMap<Numbers,Integer> map = new EnumMap<>(Numbers.class); EnumMap<Numbers,Integer> map2 = new EnumMap<>(Numbers.class); // associate values in map map.put(Numbers.ONE, 1); map.put(Numbers.TWO, 2); map.put(Numbers.THREE,3); map.put(Numbers.FOUR, 4); // print the whole map System.out.println(map); // copy the map map2.putAll(map); // print the map2 System.out.println(map2); } }
Output
Let us compile and run the above program, this will produce the following result −
{ONE=1, TWO=2, THREE=3, FOUR=4} {ONE=1, TWO=2, THREE=3, FOUR=4}
Adding Multiple Values to an EnumMap of Enum, String Pairs Example
The following example shows the usage of Java EnumMap putAll() method to populate the EnumMap instance. We've created a enum Numbers. Then Two EnumMap instance are created of enum Numbers and Strings. Few entries are added and enumMap is printed. Using putAll() method, enumMap is populated with values of first enumMap and printed.
package com.tutorialspoint; import java.util.EnumMap; public class EnumMapDemo { // create an enum public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; public static void main(String[] args) { EnumMap<Numbers,String> map = new EnumMap<>(Numbers.class); EnumMap<Numbers,String> map2 = new EnumMap<>(Numbers.class); // associate values in map map.put(Numbers.ONE, "1"); map.put(Numbers.TWO, "2"); map.put(Numbers.THREE,"3"); map.put(Numbers.FOUR, "4"); // print the whole map System.out.println(map); // copy the map map2.putAll(map); // print the map2 System.out.println(map2); } }
Output
Let us compile and run the above program, this will produce the following result −
{ONE=1, TWO=2, THREE=3, FOUR=4} {ONE=1, TWO=2, THREE=3, FOUR=4}
Adding Multiple Values to an EnumMap of Enum, Object Pairs Example
The following example shows the usage of Java EnumMap putAll() method to populate the EnumMap instance. We've created a enum Numbers. Then Two EnumMap instance are created of enum Numbers and Student objects. Few entries are added and enumMap is printed. Using putAll() method, enumMap is populated with values of first enumMap and printed.
package com.tutorialspoint; import java.util.EnumMap; public class EnumMapDemo { // create an enum public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; public static void main(String[] args) { EnumMap<Numbers,Student> map = new EnumMap<>(Numbers.class); EnumMap<Numbers,Student> map2 = new EnumMap<>(Numbers.class); // associate values in map map.put(Numbers.ONE, new Student(1, "Julie")); map.put(Numbers.TWO, new Student(2, "Robert")); map.put(Numbers.THREE,new Student(3, "Adam")); map.put(Numbers.FOUR, new Student(4, "Jene")); // print the whole map System.out.println(map); // copy the map map2.putAll(map); // print the map2 System.out.println(map2); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }
Output
Let us compile and run the above program, this will produce the following result −
{ONE=[ 1, Julie ], TWO=[ 2, Robert ], THREE=[ 3, Adam ], FOUR=[ 4, Jene ]} {ONE=[ 1, Julie ], TWO=[ 2, Robert ], THREE=[ 3, Adam ], FOUR=[ 4, Jene ]}