private static HashMap<Script, String> scripts = new HashMap<>();
public Script getScriptByName(String name) {
for (String s : scripts.values()) {
if (s.equals(name)) {
...
}
}
return null;
}
给定这段代码,我如何获得特定值的键?
最佳答案
改为浏览 map 的条目:
for (Map.Entry<String, String> entry : scripts.entrySet()) {
if (entry.getValue().equals(name)) {
return entry.getKey();
}
}
return null;
关于java - 如何通过提供值从 HashMap 获取键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23398615/