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 HashMap 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 the 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 a key from the value in Hashtable in Java.
We can still do this, but it highly depends on the 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 a key from the value in Hashtable in Java.
Java - Key from Value in Map
There 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 them; but just because you need to find the key from the value in Hashtable, adding a 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()){
if(value.equals(entry.getValue())){
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()){
if(value.equals(entry.getValue())){
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()){
if(intValue.equals(entry.getValue())){
strKey = entry.getKey();
break; //breaking because its one to one map
}
}
System.out.println("key from value in hash table key: "+ strKey +" value: " + intValue);
}
}
Output
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 (http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/BiMap.html ) from google collections you can do it in just one line :
bidirMap.inverse().get(value)
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.
- How to sort the map by keys in Java 8? (example)
- Top 5 Courses to Learn Java 8 Programming (courses)
- How to join String in Java 8 (example)
- How to use forEach() method in Java 8 (example)
- Java 8 map + filter + stream example (tutorial)
- How to use Stream class in Java 8 (tutorial)
- How to use filter() method in Java 8 (tutorial)
- 5 Free Java 8 and Java 9 courses for Programmers (courses)
- 20 Examples of Date and Time in Java 8 (tutorial)
- How to convert List to Map in Java 8 (solution)
- How to format/parse the date with LocalDateTime in Java 8? (tutorial)
- How to use peek() method in Java 8 (example)
- Top 5 websites to learn Java Coding for FREE (websites)
- 10 examples of Options in Java 8? (example)
Thanks for reading this Java tutorial so far.
5 comments :
iterate throgh the map using MAP.Entry obtained for Map.entrySet() and find out the corresponding key for the value.
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?
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());
}
Hi everyone.
I used a map in jsp like
#@java.util.LinkedHashMap@{'1':'Jan','2':'Feb','3':'Mar','4':'Apr','5':'May','6':'June','7':'July','8':'Aug','9':'Sept','10':'Oct','11':'Nov','12':'Dec'}
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)?
How we can get value through using key in hashmap or hashtable?
Post a Comment