Java中的 EnumMap containsValue(value) 方法
Java中的Java .util.EnumMap.containsValue( value ) 方法用于确定映射的一个或多个键是否与给定值相关联。它将值作为参数,如果该值由 EnumMap 中的任何键映射,则返回 True。
句法:
boolean containsValue(Object value)
参数:该方法接受一个参数值,该参数值是指要由任何键检查其映射的值。
返回值:当一个或多个键映射到相同的值时返回true。
下面的程序说明了 containsValue() 方法:
方案一:
// Java program to demonstrate containsValue() method
import java.util.*;
// An enum of gfg ranking is created
public enum rank_countries {
India,
United_States,
China,
Japan,
Canada,
Russia
};
class Enum_map {
public static void main(String[] args)
{
EnumMap mp = new
EnumMap(rank_countries.class);
// values are associated in mp
mp.put(rank_countries.India, 72);
mp.put(rank_countries.United_States, 1083);
mp.put(rank_countries.China, 4632);
mp.put(rank_countries.Japan, 6797);
mp.put(rank_countries.Canada, 1820);
// check if map contains mapping at specified key
boolean ans = mp.containsValue(72);
// prints the result
System.out.println("Map contains 72: " + ans);
}
}
输出:
Map contains 72: true
方案二:
// Java program to demonstrate containsValue() method
import java.util.*;
// An enum of gfg ranking is created
public enum rank_countries {
India,
United_States,
China,
Japan,
Canada,
Russia
};
class Enum_map {
public static void main(String[] args)
{
EnumMap mp = new
EnumMap(rank_countries.class);
// values are associated in mp
mp.put(rank_countries.India, 72);
mp.put(rank_countries.United_States, 1083);
mp.put(rank_countries.China, 4632);
mp.put(rank_countries.Japan, 6797);
mp.put(rank_countries.Canada, 1820);
// check if map contains mapping at specified key
boolean ans = mp.containsValue(2000);
// prints the result
System.out.println("Map contains 2000: " + ans);
}
}
输出:
Map contains 2000: false