java.util.EnumMap.containsKey(Object key) 方法確定如果此對映包含指定鍵的對映關係。
以下是java.util.EnumMap.containsKey()方法的宣告
public boolean containsKey(Object key)
key-- 其是否存在於此對映中的鍵
如果此對映包含指定鍵的對映此方法返回true。
NA
下面的範例演示java.util.EnumMap.containsKey()方法的用法。
package com.yiibai; import java.util.EnumMap; 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); // associate 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 whole map System.out.println(map); // check if map contains a mapping at specified key boolean contains = map.containsKey(Numbers.ONE); // print the result System.out.println("Numbers.ONE has a mapping:" + contains); // check if map contains a mapping at another specified key contains = map.containsKey(Numbers.FIVE); // print the result System.out.println("Numbers.FIVE has a mapping:" + contains); } }
讓我們來編譯和執行上面的程式,這將產生以下結果:
{ONE=1, TWO=2, THREE=3, FOUR=4} Numbers.ONE has a mapping:true Numbers.FIVE has a mapping:false