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.tutorialspoint;

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