1、代码
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值不存在”);
}
}
HashMap判断map集合中是否存在指定的key/value
最新推荐文章于 2024-02-29 13:44:37 发布
本文通过一个简单的Java示例展示了如何使用HashMap进行增删改查的基本操作,并具体讲解了如何检查Map中是否存在特定的Key和Value。
476

被折叠的 条评论
为什么被折叠?



