Java Hashmap: How to get key from value?

前端 未结 30 1843
忘掉有多难
忘掉有多难 2020-11-22 02:14

If I have the value \"foo\", and a HashMap ftw for which ftw.containsValue(\"foo\") returns true, how can I

30条回答
  •  清酒与你
    2020-11-22 03:09

    /**
     * This method gets the Key for the given Value
     * @param paramName
     * @return
     */
    private String getKeyForValueFromMap(String paramName) {
        String keyForValue = null;
        if(paramName!=null)) {
            Set> entrySet = myMap().entrySet();
            if(entrySet!=null && entrySet.size>0) {
                for(Entry entry : entrySet) {
                    if(entry!=null && paramName.equalsIgnoreCase(entry.getValue())) {
                        keyForValue = entry.getKey();
                    }
                }
            }
        }
        return keyForValue;
    }
    

提交回复
热议问题