Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java map key set

import java.util.HashMap;
import java.util.Map;
 
class Main
{
    public static <K, V> K getKey(Map<K, V> map, V value)
    {
        for (K key: map.keySet())
        {
            if (value.equals(map.get(key))) {
                return key;
            }
        }
        return null;
    }
 
    public static void main(String[] args)
    {
        Map<String, Integer> hashMap = new HashMap();
        hashMap.put("A", 1);
        hashMap.put("B", 2);
        hashMap.put("C", 3);
 
        System.out.println(getKey(hashMap, 2));        // prints `B`
    }
}
Source by www.techiedelight.com #
 
PREVIOUS NEXT
Tagged: #java #map #key #set
ADD COMMENT
Topic
Name
1+6 =