使用Java在Hashmap中获取匹配的键和值 [英] Get matching key and value in Hashmap using Java
本文介绍了使用Java在Hashmap中获取匹配的键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的地图.
Map<String , String> studentGrades = new HashMap<String, String>();
//Add Key/Value pairs
studentGrades.put("Alvin", "A+");
studentGrades.put("Alan", "A");
studentGrades.put("Becca", "A-");
studentGrades.put("Sheila", "B+");
我正在如下迭代地图.
for(String key: studentGrades.keySet()){
System.out.println(key +" :: "+ studentGrades.get(key));
}
但是,在此地图中,我想检查键"Alvin"是否存在值"A +".我不知道该怎么做.有什么想法吗?
But, in this map, I want to check whether key "Alvin" is present with the value "A+". I couldn't understand how to do it. Any ideas?
推荐答案
您可以使用以下功能:-
you can use this function : -
boolean foo(String key, String value, Map<String , String> sG) {
if (sG != null){
if (sG.containsKey(key)){
if ((sG.get(key)).equals(value)){
return true;
}
}
}
return false;
}
这篇关于使用Java在Hashmap中获取匹配的键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文