天天看点

HashMap判断map集合中是否存在指定的key/value。

public static void main(String[] args) {

// demo01();

Map<String, Integer> map = new HashMap<>();

map.put(“第一个”, 1);

map.put(“第二个”, 2);

map.put(“第三个”, 3);

map.put(“第四个”, 4);

if (map.containsKey(“第二个”)) {//判断map集合中是否存在指定的key

System.out.println(“找到了对应的key值”);

} else {

System.out.println(“搜索的key值不存在”);

}

if (map.containsValue(1)) {//判断map集合中是否存在指定的Value

System.out.println(“找到了对应的value值” + map.get(“第二个”));

} else {

System.out.println(“搜索的value值不存在”);

}

}