Navigate through the entries of the map instead:
for (Map.Entry<String, String> entry : scripts.entrySet()) {
if (entry.getValue().equals(name)) {
return entry.getKey();
}
}
return null;
Pergunta
private static HashMap<Script, String> scripts = new HashMap<>();
public Script getScriptByName(String name) {
for (String s : scripts.values()) {
if (s.equals(name)) {
...
}
}
return null;
}
Given this code, how can I get the key of a specific value?
Solução
Navigate through the entries of the map instead:
for (Map.Entry<String, String> entry : scripts.entrySet()) {
if (entry.getValue().equals(name)) {
return entry.getKey();
}
}
return null;