Java.util.EnumMap.remove() Method

Advertisements


Description

The java.util.EnumMap.remove(Object key) method removes the mapping for this key from this map if present.

Declaration

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

public V remove(Object key)

Parameters

  • key-- the key whose mapping is to be removed from the map

Return Value

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

Exception

  • NA

Example

The following example shows the usage of java.util.EnumMap.remove() method.

package com.tutorialspoint;

import java.util.*;

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, String>(Numbers.class);

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

      // print the map
      System.out.println("Map: " + map);

      // remove value from Numbers.THREE
      map.remove(Numbers.THREE);

      // print the updated map
      System.out.println("Updated Map: " + map);
   }
}


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

Map: {ONE=1, TWO=2, THREE=3, FOUR=4}
Map: {ONE=1, TWO=2, FOUR=4}



Advertisements
Advertisements