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值不存在");
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM