当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java EnumMap containsValue()用法及代码示例


Java EnumMap 类的 containsValue() 方法用于检查一个或多个键是否与给定值相关联。如果指定的值由映射中的任何键映射,则此方法返回 true。

用法

public <strong>boolean</strong> containsValue(Object value)

参数

value- 要测试其在此Map中存在的值

返回

如果此映射将一个或多个键映射到此值,则此方法返回 true。

异常

NA

例子1

import java.util.EnumMap;
public class EnumMapContainsValueExample1 {
   // create an enum  
   public enum Tutorial {
   Java, Python, PHP, Android, AngularJS
   };
   public static void main(String[] args) {
   //create and populate enum map  
      EnumMap< Tutorial, String>  map =
              new EnumMap< Tutorial, String> (Tutorial.class);

      map.put(Tutorial.Java, "1");
      map.put(Tutorial.Python, "2");
      map.put(Tutorial.PHP, "3");
      map.put(Tutorial.Android, "4");
      map.put(Tutorial.AngularJS, "5");
     // create an enum
      System.out.println("Map:"+map);
      boolean contains = map.containsValue("1");
      System.out.println("Map contains '1':"+contains);
      contains = map.containsValue("5");
      System.out.println("Map contains '5':"+contains);
   }
}

输出:

Map:{Java=1, Python=2, PHP=3, Android=4, AngularJS=5}
Map contains '1':true
Map contains '5':true

例子2

import java.util.EnumMap;
public class EnumMapContainsValueExample2 {
    // create an enum  
   public enum Days {  
   Monday, Tuesday, Wednesday, Thursday  
   };  
   public static void main(String[] args) {  
   //create and populate enum map  
   EnumMap<Days, String> map = new EnumMap<Days, String>(Days.class);  

   map.put(Days.Monday,"1");  
   map.put(Days.Tuesday,"2");  
   map.put(Days.Wednesday,"3");  
   map.put(Days.Thursday,"4");  
   // print map
   System.out.println("Map:"+map);
   boolean contains = map.containsValue("2");
   System.out.println("Map contains '2':"+contains);
   contains = map.containsValue("4");
   System.out.println("Map contains '4':"+contains);
  }
}

输出:

Map:{Monday=1, Tuesday=2, Wednesday=3, Thursday=4}
Map contains '2':true
Map contains '4':true



相关用法


注:本文由纯净天空筛选整理自 Java EnumMap containsValue() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。