EnumMap containsKey(Object) in Java

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

Syntax:

boolean java.util.EnumMap.containsKey(Object key)

This method takes one argument of type Object as its parameter. This method returns true if this map contains a mapping for the specified key.

Parameters: One parameter is required for this method.

key: the key whose presence in this map is to be tested.

Returns: true if this map contains a mapping for the specified key.

Exceptions: NA

Approach

Java

import java.util.EnumMap;

public class EnumMapcontainsKey {

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

        System.out.println(enumMap.containsKey(Colour.RED));

    }
}

Output:

true


No comments:

Post a Comment