Java.util.EnumMap.size() Method


Advertisements


Description

The java.util.EnumMap.size() method returns how many key-value mappings exist in this map.

Declaration

Following is the declaration for java.util.EnumMap.size() method

public int size()

Parameters

  • NA

Return Value

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

Exception

  • NA

Example

The following example shows the usage of java.util.EnumMap.size() method.

package com.tutorialspoint;

import java.util.*;

public class EnumMapDemo {

   // create an enum
   public enum Numbers {

   ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {
   EnumMap<Numbers, String> map =
   new EnumMap<Numbers, String>(Numbers.class);

   // assosiate values in map
   map.put(Numbers.ONE, "1");
   map.put(Numbers.TWO, "2");
   map.put(Numbers.THREE, "3");
   map.put(Numbers.FOUR, "4");

   // print the map
   System.out.println("Map: " + map);

   // print the number of mappings of this map
   System.out.println("Number of mappings:" + map.size());

   // remove value from Numbers.THREE
   map.put(Numbers.FIVE, "5");

   // print the new number of mappings of this map
   System.out.println("Number of mappings:" + map.size());
   }
}

Let us compile and run the above program, this will produce the following result:

Map: {ONE=1, TWO=2, THREE=3, FOUR=4}
Number of mappings:4
Number of mappings:5

java_util_enummap.htm

Advertisements