判断Map中是否包含某个key需要使用到map中的containsKey()方法;
判断Map中是否包含某个value值需要使用到map中的containsValue()方法。
使用方法如下:
public static void main(String[] args) {
Map map = new HashMap();
map.put("name", "小老虎");
map.put("age", "20");
String key = "birthday";
String value = "50";
boolean containsKey = map.containsKey(key);
if(containsKey) {
// 条件为真
System.out.println("Map中包含名为:"+key+"的key");
}else {
// 条件为假
System.out.println("Map中不包含名为:"+key+"的key");
}
boolean containsValue = map.containsValue(value);
if(containsValue) {
// 条件为真
System.out.println("Map中包含:"+value+"的值");
}else {
// 条件为假
System.out.println("Map中不包含:"+value+"的值");
}
}