I want to find out the key for given value from HashMap, currently I have to go through all keys and check its value in map, is there a faster way?
Asked
Active
Viewed 3,904 times
3
-
1An issue with this is that a value can be associated with more than one key. When this is the case, you're only getting the first key you happen to find for that value. If a single value should be associated with at most one key (and it probably ought to be, if you're wanting to find the inverse mapping), you should use BiMap (mentioned below) which enforces this and allows you to get the value -> key mapping easily. – ColinD May 14 '10 at 02:51
6 Answers
6
No, there is not a faster way (without introducing other data structures). If you need to do this often, reconsider your design. Maybe you want another HashMap
whose keys are the values of the other HashMap
?

Justin Ardini
- 9,768
- 2
- 39
- 46
1
Here are two related posts on stackoverflow:
Some solution hadn't been mentioned yet BidiMap:
The source code is from here:
package com.discursive.jccook.collections.bidi;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.DualHashBidiMap;
public class BidiMapExample {
private BidiMap countryCodes = new DualHashBidiMap( );
public static void main(String[] args) {
BidiMapExample example = new BidiMapExample( );
example.start( );
}
private void start( ) {
populateCountryCodes( );
String countryName = (String) countryCodes.get( "tr" );
System.out.println( "Country Name for code 'tr': " + countryName );
String countryCode =
(String) countryCodes.inverseBidiMap( ).get("Uruguay");
System.out.println( "Country Code for name 'Uruguay': " + countryCode );
countryCode = (String) countryCodes.getKey("Ukraine");
System.out.println( "Country Code for name 'Ukraine': " + countryCode );
}
private void populateCountryCodes( ) {
countryCodes.put("to","Tonga");
countryCodes.put("tr","Turkey");
countryCodes.put("tv","Tuvalu");
countryCodes.put("tz","Tanzania");
countryCodes.put("ua","Ukraine");
countryCodes.put("ug","Uganda");
countryCodes.put("uk","United Kingdom");
countryCodes.put("um","USA Minor Outlying Islands");
countryCodes.put("us","United States");
countryCodes.put("uy","Uruguay");
}
}
-1
If you look at HashMap.get(key) method you will see a use of
Entry entry = getEntry(key);
Why is getEntry(key) method private?? It returns key and value for desired search. I will use this method by brute force, but it is ugly solution...
Implementation of method in HashMap follows
/**
* Returns the entry associated with the specified key in the
* HashMap. Returns null if the HashMap contains no mapping
* for the key.
*/
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
-
OP is looking for a **reverse lookup** solution to get the key by value. There's no key as input. – kukido Jan 08 '14 at 23:32
-2
Use a better datastructure, like a TreeMap, as that will be much more efficient to search.

Mitch Dempsey
- 38,725
- 6
- 68
- 74
-
How is a `TreeMap` more efficient to search? You'd still need to walk through all values if you want to find the key for a given value. – Jesper May 14 '10 at 08:20
-
@Jesper - Yes, but because a treemap is sorted, the number of nodes you must traverse is generally (not always) much less than in a hashmap – Mitch Dempsey May 14 '10 at 19:10
-
2a `TreeMap` is sorted by the *keys*, not the values, so if you want to find the key that matches a value, it doesn't help you that it's sorted by keys. You'll have to look at all the values. – Jesper May 14 '10 at 21:53