Java Hashmap: How to get key from value?

前端 未结 30 1855
忘掉有多难
忘掉有多难 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 02:56

    If you build the map in your own code, try putting the key and value in the map together:

    public class KeyValue {
        public Object key;
        public Object value;
        public KeyValue(Object key, Object value) { ... }
    }
    
    map.put(key, new KeyValue(key, value));
    

    Then when you have a value, you also have the key.

提交回复
热议问题