EnumMap remove(Object) in Java

remove(Object): This method is available in java.util.EnumMap class of Java.

Syntax:

String java.util.EnumMap.remove(Object key)

This method takes one argument of type Object as its parameter. This method removes the mapping for this key from this map if present.

Parameters: One parameter is required for this method.

key: the key whose mapping is to be removed from the map.

Returns: the previous value associated with the specified key, or null if there was no entry for the key.

Exceptions: NA

Approach

Java

import java.util.EnumMap;

public class EnumMapremove {

    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumMap<Colour, String> enumMap =
new EnumMap<Colour, String>(Colour.class);

        enumMap.put(Colour.RED, "Red");
        enumMap.put(Colour.GREEN, "Green");

        enumMap.put(Colour.YELLOW, "Yellow");
        enumMap.put(Colour.ORANGE, "Orange");

        enumMap.remove(Colour.RED);

        System.out.println(enumMap);

    }
}

Output:

{GREEN=Green, YELLOW=Yellow, ORANGE=Orange}


No comments:

Post a Comment