EnumMap keySet() in Java

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

Syntax:

Set<K> java.util.EnumMap.keySet()

This method returns a Set view of the keys contained in this map.

Parameters: NA

Returns: a set view of the keys contained in this enum map.

Exceptions: NA

Approach

Java

public class EnumMapkeySet {

    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.keySet());

    }
}

Output:

[RED, GREEN, YELLOW, ORANGE]


No comments:

Post a Comment