How to get Key From Value in Hashtable, HashMap or Map in Java

It's not easy to get key from the value in Hashtable or HashMap, as compared to getting value from key because HashMap or Hashtable doesn't enforce one to one mapping between key and value inside Map in Java. in fact, Map allows the same value to be mapped to multiple keys inside HashMap, Hashtable, or any other Map implementation. What you have in your kitty is Hashtable.containsValue(String value) or Hashtable.containsKey(String key) to check whether key or value exists in Hashtable or not, but sometimes we want to retrieve a value from Map corresponding to any key and there is no API method to do in Map.

We can still do this, but it highly depends on data in your Map because Hashtable and HashMap both allow duplicate values mapped to the different keys. In this Java tutorial, we will see an example of how to get key from the value in Hashtable in Java.

And, If you are new to Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.

Java - Key from Value in Map

How to get key from value in Java HashMap example tutorialThere are essentially two ways to find a key from values in Map, one is without using any third-party library, and the other is using third party libraries like Google collection or commons-collections which provide bi-directional Maps

Though most of the time projects already use these utility libraries, so it's better to use them if you already using it; but just because you need to find the key from the value in Hashtable, adding new dependency doesn't make sense, especially if you can still do this by writing a function and iterating over Map in Java. 

In the next section, we will code an example of retrieving the value from the key in a hashtable as well as in hashmap.

Key from Value Hashtable HashMap Example

import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

 * Java program to get key from value in Java HashMap, Hashtable and Map.
 * @author java67
public class KeyFromValueExample {

public static void main(String args[]) {
        //how to get key from value in hashtable in Java
        Hashtable table = new Hashtable();
        table.put("Sony", "Bravia");
        table.put("Samsung", "Galaxy");
        table.put("Nokia", "Lumia");
        System.out.println("does hash table has Lumia as value : " + table.containsValue("Lumia"));
        System.out.println("does hash table Lumia as key : " + table.containsKey("Lumia"));
        //finding key corresponding to value in hashtable - one to one mapping
        String key= null;
        String value="Lumia";
        for(Map.Entry entry: table.entrySet()){
                key = entry.getKey();
                break; //breaking because its one to one map
        System.out.println("got key from value in hashtable key:  "+ key +" value: " + value);
        //finding key corresponding to value in hashtable - one to many mapping
        table.put("HTC", "Lumia");
        Set keys = new HashSet();
        for(Map.Entry entry: table.entrySet()){
                keys.add(entry.getKey()); //no break, looping entire hashtable
        System.out.println("keys : " + keys +" corresponding to value in hash table:  "+ value);
        //how to get from value in HashMap example - similar to Hashtable example
        HashMap map = new HashMap();
        map.put("one", 1);
        map.put("two", 2);
        //find key from value in HashMap Java - one to one mapping
        Integer intValue=2;
        String strKey = null;
        for(Map.Entry entry: map.entrySet()){
                strKey = entry.getKey();
                break; //breaking because its one to one map
        System.out.println("key from value in hash table key:  "+ strKey +" value: " + intValue);

does hash table has Lumia as value : true
does hash table has Lumia as key : false
got the key from value in hashtable key:  Nokia value: Lumia
keys : [Nokia, HTC] corresponding to value in hash table:  Lumia
key from value in hash table key:  two value: 2

If you are using bi-directional Map e.g. BiMap ( ) from google collections you can do it in just one line :


That's all on how to get key from value in Hashtable and HashMap in Java. Though using google collection for this task is much clear and concise, it’s not always the best option. Using JDK to find the key from value in HashMap is better, if you know your map is one to one or one to many. Let me know, if you come across any other way of finding the key from value in Hashtable or HashMap in Java.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java
Algorithms and Data Structures - Part 1 and 2
Data Structures in Java 9 by Heinz Kabutz


Anonymous said...

iterate throgh the map using MAP.Entry obtained for Map.entrySet() and find out the corresponding key for the value.

Jody said...

When I tried to use "Map.Entry entry: table.entrySet()", it prompted that "Type mismatch: cannot convert from element type Object to Map.Entry". Even if I copied the codes, it has no change. I don't know where has wrong?

satish said...

int arr[] = { 2, 4, 5, 8, 5, 6, 5, 9, 3, 3, 3 };
Map map = new HashMap();
for (int i : arr) {
Integer count = map.get(i);
map.put(i, count != null ? count + 1 : 0);
for (Map.Entry data : map.entrySet()) {
System.out.println("data : " + data.getKey() + " ,occurance :"
+ data.getValue());

Unknown said...

Hi everyone.
I used a map in jsp like

now in java script
i got a value like 'May' from a chart. Now I need the key of that value.
how can i get the key of that value from the above map(which is used in jsp)?

Devendera Singh Tadiyal said...

How we can get value through using key in hashmap or hashtable?

Post a Comment