Java Hashmap: How to get key from value?

前端 未结 30 1762
忘掉有多难
忘掉有多难 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:08

    I think this is best solution, original address: Java2s

        import java.util.HashMap;
        import java.util.Map;
    
            public class Main {
    
              public static void main(String[] argv) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("1","one");
                map.put("2","two");
                map.put("3","three");
                map.put("4","four");
    
                System.out.println(getKeyFromValue(map,"three"));
              }
    
    
    // hm is the map you are trying to get value from it
              public static Object getKeyFromValue(Map hm, Object value) {
                for (Object o : hm.keySet()) {
                  if (hm.get(o).equals(value)) {
                    return o;
                  }
                }
                return null;
              }
            }
    

    An easy usage: if you put all data in hasMap and you have item = "Automobile", so you are looking its key in hashMap. that is good solution.

    getKeyFromValue(hashMap, item);
    System.out.println("getKeyFromValue(hashMap, item): "+getKeyFromValue(hashMap, item));
    
    0 讨论(0)
  • 2020-11-22 03:08

    You can use the below:

    public class HashmapKeyExist {
        public static void main(String[] args) {
            HashMap<String, String> hmap = new HashMap<String, String>();
            hmap.put("1", "Bala");
            hmap.put("2", "Test");
    
            Boolean cantain = hmap.containsValue("Bala");
            if(hmap.containsKey("2") && hmap.containsValue("Test"))
            {
                System.out.println("Yes");
            }
            if(cantain == true)
            {
                System.out.println("Yes"); 
            }
    
            Set setkeys = hmap.keySet();
            Iterator it = setkeys.iterator();
    
            while(it.hasNext())
            {
                String key = (String) it.next();
                if (hmap.get(key).equals("Bala"))
                {
                    System.out.println(key);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:09

    It sounds like the best way is for you to iterate over entries using map.entrySet() since map.containsValue() probably does this anyway.

    0 讨论(0)
  • 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<Entry<String,String>> entrySet = myMap().entrySet();
            if(entrySet!=null && entrySet.size>0) {
                for(Entry<String,String> entry : entrySet) {
                    if(entry!=null && paramName.equalsIgnoreCase(entry.getValue())) {
                        keyForValue = entry.getKey();
                    }
                }
            }
        }
        return keyForValue;
    }
    
    0 讨论(0)
  • 2020-11-22 03:11

    Using Java 8:

    ftw.forEach((key, value) -> {
        if (value.equals("foo")) {
            System.out.print(key);
        }
    });
    
    0 讨论(0)
  • 2020-11-22 03:11

    You can get the key using values using following code..

    ArrayList valuesList = new ArrayList();
    Set keySet = initalMap.keySet();
    ArrayList keyList = new ArrayList(keySet);
    
    for(int i = 0 ; i < keyList.size() ; i++ ) {
        valuesList.add(initalMap.get(keyList.get(i)));
    }
    
    Collections.sort(valuesList);
    Map finalMap = new TreeMap();
    for(int i = 0 ; i < valuesList.size() ; i++ ) {
        String value = (String) valuesList.get(i);
    
        for( int j = 0 ; j < keyList.size() ; j++ ) {
            if(initalMap.get(keyList.get(j)).equals(value)) {
                finalMap.put(keyList.get(j),value);
            }   
        }
    }
    System.out.println("fianl map ---------------------->  " + finalMap);
    
    0 讨论(0)
提交回复
热议问题