EnumMap put() Method in Java

The Java.util.EnumMap.put(key, value) method in Java is used to associated specified key-value pair. In this case, if the values are repeated, the older values are replaced.

Syntax:

Enum_Map.put(key, value)

Parameters Used: The method takes two parameters.



  • key – It is the specified key with which the value is associated.
  • value – It is the value associated with the specified key.

Return Value: The function returns the old value associated with the specified key.

Below programs illustrate the working of put(key, value) method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate keySet()
import java.util.*;
  
// An enum of geeksforgeeks
public enum gfg {
    Global_today,
    India_today,
    China
}
;
  
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.India_today, 72);
  
        // Prints the old value
        System.out.println("Previous value: " + prev_value);
  
       // Display the final map
       System.out.println("The final map is: " + mp);
    }
}

chevron_right


Output:

The map is: {Global_today=799, India_today=69}
Previous value: 69
The final map is: {Global_today=799, India_today=72}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

The map is: {Global_today=799, India_today=69}
Previous value: 799
The final map is: {Global_today=800, India_today=69}


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.