Java EnumMap.put(K key, V value)

Syntax

EnumMap.put(K key, V value) has the following syntax.

public V put(K key, V value)

Example

In the following code shows how to use EnumMap.put(K key, V value) method.


//ww  w  .j a  v a 2s  . c om

import java.util.*;

enum Tutorial {

   CSS, Python, PHP, Java, Javascript
};


public class Main {
   public static void main(String[] args) {
      EnumMap<Tutorial, String>  map =
              new EnumMap<Tutorial, String> (Tutorial.class);

      map.put(Tutorial.CSS, "1");
      map.put(Tutorial.Python, "2");
      map.put(Tutorial.PHP, "3");
      map.put(Tutorial.Java, "4");

      System.out.println(map);
    
      String oldValue1 = map.put(Tutorial.Javascript, "5");

      String oldValue2 = map.put(Tutorial.CSS, "20");

      System.out.println("Updated Map: " + map);
      System.out.println("First update returns:" + oldValue1);
      System.out.println("Second update returns:" + oldValue2);

   }
}

The code above generates the following result.