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?

有帮助吗?

解决方案

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;      
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top