JavaHashmap:如何从值中获取密钥?如果我有这个价值"foo",以及HashMap<String> ftw对此ftw.containsValue("foo")回报true如何获得相应的密钥?我必须遍历hashmap吗?最好的方法是什么?
3 回答
白衣非少年
TA贡献1155条经验 获得超0个赞
public class NewClass1 {
public static void main(String[] args) {
Map<Integer, String> testMap = new HashMap<Integer, String>();
testMap.put(10, "a");
testMap.put(20, "b");
testMap.put(30, "c");
testMap.put(40, "d");
for (Entry<Integer, String> entry : testMap.entrySet()) {
if (entry.getValue().equals("c")) {
System.out.println(entry.getKey());
}
}
}}一些额外的信息.。可能对你有用
1. Key to value2. Value to key
添加回答
举报
0/150
提交
取消
