Java EnumMap put() Method



Description

The Java EnumMap put(K key,V value) method associates the specified value with the specified key in this map. Older values are replaced.

Declaration

Following is the declaration for java.util.EnumMap.put() method

public V put(K key,V value)

Parameters

  • key − the key with which the specified value is to be associated

  • value − the value to be associated with the specified key

Return Value

This method returns the previous value associated with specified key, or null if there was no mapping for key.

Exception

NullPointerException − if the specified key is null

Adding a Value to an EnumMap of Enum, Integer Pairs Example

The following example shows the usage of Java EnumMap put(K,V) method to put a value in the EnumMap instance. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Integer. Few entries are added using put(K,V) and enumMap is printed. Using put() method again, a value of enumMap is replaced and map is printed again.

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

      // associate values in map
      map.put(Numbers.ONE, 1);
      map.put(Numbers.TWO, 2);
      map.put(Numbers.THREE,3);

      // print the whole map
      System.out.println(map); 

      map.put(Numbers.THREE, 4);
	  
      // print the updated map
      System.out.println(map);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{ONE=1, TWO=2, THREE=3}
{ONE=1, TWO=2, THREE=4}

Adding a Value to an EnumMap of Enum, String Pairs Example

The following example shows the usage of Java EnumMap put(K,V) method to put a value in the EnumMap instance. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Integer. Few entries are added using put(K,V) and enumMap is printed. Using put() method again, a value of enumMap is replaced and map is printed again.

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

      // associate values in map
      map.put(Numbers.ONE, "1");
      map.put(Numbers.TWO, "2");
      map.put(Numbers.THREE,"3");

      // print the whole map
      System.out.println(map); 

      map.put(Numbers.THREE, "4");
	  
      // print the updated map
      System.out.println(map);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{ONE=1, TWO=2, THREE=3}
{ONE=1, TWO=2, THREE=4}

Adding a Value to an EnumMap of Enum, Object Pairs Example

The following example shows the usage of Java EnumMap put(K,V) method to put a value in the EnumMap instance. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Integer. Few entries are added using put(K,V) and enumMap is printed. Using put() method again, a value of enumMap is replaced and map is printed again.

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

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

      // print the whole map
      System.out.println(map); 

      map.put(Numbers.THREE, new Student(4, "Jene"));
	  
      // print the updated map
      System.out.println(map);
   }
}
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 ]}
{ONE=[ 1, Julie ], TWO=[ 2, Robert ], THREE=[ 4, Jene ]}
java_util_enummap.htm
Advertisements