Java Utility Library

Java EnumMap - size() Method



The java.util.EnumMap.size() method returns the total number of key-value mapping present in the map.

Syntax

public int size()

Parameters

No parameter is required.

Return Value

Returns the total number of key-value mapping in the map.

Exception

NA

Example:

In the example below, the java.util.EnumMap.size() method is used to find out the total number of key-value mapping in the EnumMap.

import java.util.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    MON, TUE, WED, THU, FRI
  }

  public static void main(String[] args) {
    //creating EnumMap
    EnumMap<weekday,Integer> MyMap = 
        new EnumMap<weekday,Integer>(weekday.class);

    //associate values in MyMap
    MyMap.put(weekday.MON, 1);
    MyMap.put(weekday.TUE, 2);
    MyMap.put(weekday.WED, 3);
    MyMap.put(weekday.THU, 4);

    System.out.println("Size of MyMap: "+ MyMap.size());    

    //adding one more pair in the map
    MyMap.put(weekday.FRI, 5);

    System.out.println("Size of MyMap: "+ MyMap.size());  
  }
}

The output of the above code will be:

Size of MyMap: 4
Size of MyMap: 5

❮ Java.util - EnumMap