EnumMap size() in Java

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

Syntax:

int java.util.EnumMap.size()

This method returns the number of key-value mappings in this map.

Parameters: NA

Returns: the number of key-value mappings in this map.

Exceptions: NA

Approach

Java

public class EnumMapsize {

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

    }
}

Output:

4


No comments:

Post a Comment