Quantcast
Channel: Java Hashmap: How to get key from value? - Stack Overflow
Viewing all articles
Browse latest Browse all 41

Answer by Balasubramanian Ganapathi for Java Hashmap: How to get key from value?

$
0
0

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);            }        }    }}

Viewing all articles
Browse latest Browse all 41

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>