java.util.EnumMap.containsKey()方法實例
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