java.util.EnumMap.size() 方法返回此對映中存在多少個key-value對映。
以下是java.util.EnumMap.size()方法的宣告
public int size()
NA
此方法返回鍵- 值對映關係在對映的大小。
NA
下面的範例演示java.util.EnumMap.size()方法的用法。
package com.yiibai; 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()); } }
讓我們來編譯和執行上面的程式,這將產生以下結果:
Map: {ONE=1, TWO=2, THREE=3, FOUR=4} Number of mappings:4 Number of mappings:5