Java.util.EnumMap.containsKey() 方法


Java.util.EnumMap.containsKey() 方法

package com.codingdict;

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);
   }
}