Home » Java programming language

Java EnumMap remove() Method with Example

EnumMap Class remove() method: Here, we are going to learn about the remove() method of EnumMap Class with its syntax and example.
Submitted by Preeti Jain, on February 10, 2020

EnumMap Class remove() method

  • remove() method is available in java.util package.
  • remove() method is used to clear all mappings associated with the given key element (key_ele) when it exists.
  • remove() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • remove() method does not throw an exception at the time of returning an associated value with the given key element (key_ele).

Syntax:

    public Value remove(Object key_ele);

Parameter(s):

  • Object key_ele – represents the key element (key _ele) whose associated values is to be removed from this enum map.

Return value:

The return type of this method is Value, it returns the removed value mapped with the given key element (key_ele) when exists otherwise it returns null.

Example:

// Java program to demonstrate the example 
// of Value remove(Object key_ele)  method of EnumMap

import java.util.*;

public class RemoveOfEnumMap {
    public enum Colors {
        RED,
        BLUE,
        PINK,
        YELLOW
    };
    public static void main(String[] args) {
        // We are creating EnumMap object
        EnumMap < Colors, String > em =
            new EnumMap < Colors, String > (Colors.class);


        // By using put() method is to
        // add the linked values in an
        // EnumMap (em)
        em.put(Colors.RED, "1");
        em.put(Colors.BLUE, "2");
        em.put(Colors.PINK, "3");
        em.put(Colors.YELLOW, "4");

        // Display EnumMap
        System.out.println("EnumMap (em) :" + em);

        // By using remove(key_ele) method isto
        // remove the key-value for the given key
        // element (key_ele) when exists
        em.remove(Colors.YELLOW);

        // Display modified EnumMap
        System.out.println("em.remove(Colors.YELLOW): " + em);
    }
}

Output

EnumMap (em) :{RED=1, BLUE=2, PINK=3, YELLOW=4}
em.remove(Colors.YELLOW): {RED=1, BLUE=2, PINK=3}



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.