EnumMap put() Method in Java with Examples
Last Updated :
16 Feb, 2024
The Java.util.EnumMap.put() method in Java associates specified key-value pairs.
In simple words, the put() method is used to add or modify entries in the EnumMap. If the values are repeated, the older values are replaced.
Example
Java
import java.util.EnumMap;
enum ProgLang {
Java, JavaScript, Python
}
public class Main {
public static void main(String[] args) {
EnumMap<ProgLang, String> PLM = new EnumMap<>(ProgLang. class );
PLM.put(ProgLang.Java, "Study at GeeksforGeeks" );
PLM.put(ProgLang.JavaScript, "Learn at GeeksforGeeks" );
PLM.put(ProgLang.Python, "Practice at GeeksforGeeks" );
PLM.put(ProgLang.Java, "Studying at GeeksforGeeks" );
System.out.println(PLM);
}
}
|
Output:
{Java=Studying at GeeksforGeeks, JavaScript=Learn at GeeksforGeeks, Python=Practice at GeeksforGeeks}
Syntax
Enum_Map.put(K key, V value)
Parameters
- key – It is the specified key with which the value is associated.
- value – It is the value associated with the specified key.
Returns
returns the old value associated with the specified key.
Exceptions
- NullPointerException- When the specified key is null.
EnumMap put() Method Examples
Let’s see some examples of how to use EnumMap put() Method in Java
Example 1
How to put/add new value for the specified key in EnumMap:
Java
import java.util.*;
enum GFG {
Global_today,
India_today,
China
}
class EnumDemo {
public static void main(String[] args) {
EnumMap<GFG, Integer> mp = new EnumMap<>(GFG. class );
mp.put(GFG.Global_today, 799 );
mp.put(GFG.India_today, 69 );
System.out.println( "The map is: " + mp);
Integer prevValue = mp.put(GFG.India_today, 72 );
System.out.println( "Previous value: " + prevValue);
System.out.println( "The final map is: " + mp);
}
}
|
Output
The map is: {Global_today=799, India_today=69}
Previous value: 69
The final map is: {Global_today=799, India_today=72}
Example 2
To show the working of the keyset() method in Java
Java
import java.util.*;
public enum gfg {
Global_today,
India_today,
China_today
}
;
class Enum_demo {
public static void main(String[] args)
{
EnumMap<gfg, Integer> mp = new EnumMap<gfg, Integer>(gfg. class );
mp.put(gfg.Global_today, 799 );
mp.put(gfg.India_today, 69 );
System.out.println("The map is: " + mp);
int prev_value = mp.put(gfg.Global_today, 800 );
System.out.println("Previous value: " + prev_value);
System.out.println("The final map is: " + mp);
}
}
|
Output
The map is: {Global_today=799, India_today=69}
Previous value: 799
The final map is: {Global_today=800, India_today=69}
Example 3
NullPointerException in EnumMap put() Method
Java
import java.util.*;
enum GFG {
Global_today,
India_today,
China
}
class EnumDemo {
public static void main(String[] args) {
EnumMap<GFG, Integer> mp = new EnumMap<>(GFG. class );
mp.put(GFG.Global_today, 799 );
mp.put( null , 69 );
System.out.println( "The map is: " + mp);
Integer prevValue = mp.put(GFG.India_today, 72 );
System.out.println( "Previous value: " + prevValue);
System.out.println( "The final map is: " + mp);
}
}
|
Output:
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.EnumMap.typeCheck(EnumMap.java:743)
at java.base/java.util.EnumMap.put(EnumMap.java:264)
at EnumDemo.main(EnumDemo.java:16)
Read More EnumMap Methods
Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of the put function and its uses in Java.
The put method in Java is a fundamental function for EnumMap manipulation. With this guide, you can easily put/add a new element to an EnumMap using the put method.