This example shows how to get the Hashtable key by value in Java. This example also shows how to get a Hashtable key from a value using entrySet and keySet methods.
How to get a Hashtable key from the value in Java?
The Hashtable class in Java maintains the mapping of keys to values, not the other way around. There is no direct way to get the key from value, however, there are a couple of workarounds using which we can get the key as given below.
1. Using the entrySet method
We can get all the entries of the Hashtable object, get an iterator using the iterator
method and then iterate through hashtable entries to find the value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.Hashtable; import java.util.Iterator; import java.util.Map; public class HashtableGetKeyFromValue { public static void main(String[] args) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); hashtable.put(4, "Four"); hashtable.put(5, "Five"); System.out.println( getKeyFromValue(hashtable, "One") ); System.out.println( getKeyFromValue(hashtable, "Three") ); System.out.println( getKeyFromValue(hashtable, "Five") ); System.out.println( getKeyFromValue(hashtable, "Six") ); } private static Integer getKeyFromValue(Hashtable<Integer, String> hashtable, String value){ Integer key = null; if(value == null) return key; //get an iterator for the entries Iterator<Map.Entry<Integer, String>> itr = hashtable.entrySet().iterator(); Map.Entry<Integer, String> entry = null; while( itr.hasNext() ){ entry = itr.next(); //if this entry value is equals to the value if(entry.getValue().equals(value)){ return entry.getKey(); } } return key; } } |
Output
1 2 3 4 |
1 3 5 null |
2. Using the keySet method
Instead of entries, we can also get Set of all keys from the Hashtable using the keySet
method, iterate through it and compare values with the value we are interested in as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.Hashtable; import java.util.Iterator; public class HashtableGetKeyFromValue { public static void main(String[] args) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); hashtable.put(4, "Four"); hashtable.put(5, "Five"); System.out.println( getKeyFromValue(hashtable, "One") ); System.out.println( getKeyFromValue(hashtable, "Three") ); System.out.println( getKeyFromValue(hashtable, "Five") ); System.out.println( getKeyFromValue(hashtable, "Six") ); } private static Integer getKeyFromValue(Hashtable<Integer, String> hashtable, String value){ Integer key = null; if(value == null) return key; //get an iterator for the keys Iterator<Integer> itr = hashtable.keySet().iterator(); Integer currentKey = null; while( itr.hasNext() ){ currentKey = itr.next(); if( hashtable.get(currentKey).equals(value) ){ return currentKey; } } return key; } } |
Output
1 2 3 4 |
1 3 5 null |
What if Hashtable has duplicate values mapped to many keys?
The hashtable allows one value mapped to many keys. In that case, you can return a List of keys instead of a single key as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import java.util.List; public class HashtableGetKeyFromValue { public static void main(String[] args) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "One"); hashtable.put(4, "Four"); hashtable.put(5, "One"); System.out.println( getKeyFromValue(hashtable, "Four") ); System.out.println( getKeyFromValue(hashtable, "One") ); } private static List<Integer> getKeyFromValue(Hashtable<Integer, String> hashtable, String value){ List<Integer> listKeys = new ArrayList<Integer>(); if(value == null) return listKeys; //get an iterator for the keys Iterator<Integer> itr = hashtable.keySet().iterator(); Integer currentKey = null; while( itr.hasNext() ){ currentKey = itr.next(); if( hashtable.get(currentKey).equals(value) ){ //add the current key to the list listKeys.add(currentKey); } } return listKeys; } } |
Output
1 2 |
[4] [5, 3, 1] |
Important Note:
Both of these approaches may not perform well if the hashtable has a large number of entries.
This example is a part of the Java Hashtable Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation