Open In App

EnumMap put() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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




// Java Program to demonstrate the usage of EnumMap
 
import java.util.EnumMap;
 
// Enumeration representing programming languages
enum ProgLang {
    Java, JavaScript, Python
}
 
// Driver Class
public class Main {
      // Main Function
    public static void main(String[] args) {
        // Create an EnumMap to store language-program pairs
        EnumMap<ProgLang, String> PLM = new EnumMap<>(ProgLang.class);
         
        // Add entries to the EnumMap
        PLM.put(ProgLang.Java, "Study at GeeksforGeeks");
        PLM.put(ProgLang.JavaScript, "Learn at GeeksforGeeks");
        PLM.put(ProgLang.Python, "Practice at GeeksforGeeks");
         
        // Overwrite an entry for Java
        PLM.put(ProgLang.Java, "Studying at GeeksforGeeks");
 
        // Print the EnumMap
        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.*;
 
// An enum of geeksforgeeks
enum GFG {
    Global_today,
    India_today,
    China
}
 
class EnumDemo {
    public static void main(String[] args) {
        EnumMap<GFG, Integer> mp = new EnumMap<>(GFG.class);
 
        // Values are associated
        mp.put(GFG.Global_today, 799);
        mp.put(GFG.India_today, 69);
 
        // Display the initial map
        System.out.println("The map is: " + mp);
 
        // Stores the old value associated with the key
        Integer prevValue = mp.put(GFG.India_today, 72);
 
        // Prints the old value
        System.out.println("Previous value: " + prevValue);
 
        // Display the final map
        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




// Java program to demonstrate the working of keySet()
import java.util.*;
 
// an enum of geeksforgeeks
// ranking globally and in india
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);
 
        // Values are associated
        mp.put(gfg.Global_today, 799);
        mp.put(gfg.India_today, 69);
 
       // Display the initial map
       System.out.println("The map is: " + mp);
 
        // Stores the old value associated with the key
        int prev_value = mp.put(gfg.Global_today, 800);
 
        // Prints the old value
        System.out.println("Previous value: " + prev_value);
 
       // Display the final map
       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.*;
 
// An enum of geeksforgeeks
enum GFG {
    Global_today,
    India_today,
    China
}
 
class EnumDemo {
    public static void main(String[] args) {
        EnumMap<GFG, Integer> mp = new EnumMap<>(GFG.class);
 
        // Values are associated
        mp.put(GFG.Global_today, 799);
        mp.put(null, 69);
 
        // Display the initial map
        System.out.println("The map is: " + mp);
 
        // Stores the old value associated with the key
        Integer prevValue = mp.put(GFG.India_today, 72);
 
        // Prints the old value
        System.out.println("Previous value: " + prevValue);
 
        // Display the final map
        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.



Last Updated : 16 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads