31

I want to get the key of a HashMap using the value.

hashmap = new HashMap<String, Object>();

haspmap.put("one", 100);
haspmap.put("two", 200);

Which means i want a function that will take the value 100 and will return the string one.

It seems that there are a lot of questions here asking the same thing but they don't work for me.

Maybe because i am new with java.

How to do it?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
kechap
  • 2,077
  • 6
  • 28
  • 50

8 Answers8

67

The put method in HashMap is defined like this:

Object  put(Object key, Object value) 

key is the first parameter, so in your put, "one" is the key. You can't easily look up by value in a HashMap, if you really want to do that, it would be a linear search done by calling entrySet(), like this:

for (Map.Entry<Object, Object> e : hashmap.entrySet()) {
    Object key = e.getKey();
    Object value = e.getValue();
}

However, that's O(n) and kind of defeats the purpose of using a HashMap unless you only need to do it rarely. If you really want to be able to look up by key or value frequently, core Java doesn't have anything for you, but something like BiMap from the Google Collections is what you want.

Zon
  • 18,610
  • 7
  • 91
  • 99
kbyrd
  • 3,321
  • 27
  • 41
  • This will run only while debugging so run time is not a problem. – kechap Nov 13 '11 at 16:47
  • 1
    +1, though I find iterating over the entry set a little more straightforward as it contains the value in the element. Still, without the BiMap the OP needs to maintain the "unique values" invariant outside of the map. – Mark Peters Nov 13 '11 at 16:47
  • @MarkPeters: Thanks. I meant to do that but my fingers didn't cooperate. entrySet is more efficient as you don't need to do lookups for every iteration of the keySet. – kbyrd Nov 13 '11 at 16:55
25

We can get KEY from VALUE. Below is a sample code_

 public class Main {
  public static void main(String[] args) {
    Map map = new HashMap();
    map.put("key_1","one");
    map.put("key_2","two");
    map.put("key_3","three");
    map.put("key_4","four");
System.out.println(getKeyFromValue(map,"four")); } public static Object getKeyFromValue(Map hm, Object value) { for (Object o : hm.keySet()) { if (hm.get(o).equals(value)) { return o; } } return null; } }

I hope this will help everyone.

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
5
  • If you need only that, simply use put(100, "one"). Note that the key is the first argument, and the value is the 2nd.
  • If you need to be able to get by both the key and the value, use BiMap (from guava)
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
4

You have it reversed. The 100 should be the first parameter (it's the key) and the "one" should be the second parameter (it's the value).

Read the javadoc for HashMap and that might help you: HashMap

To get the value, use hashmap.get(100).

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
4

You mixed the keys and the values.

Hashmap <Integer,String> hashmap = new HashMap<Integer, String>();

hashmap.put(100, "one");
hashmap.put(200, "two");

Afterwards a

hashmap.get(100);

will give you "one"

Udo Held
  • 12,314
  • 11
  • 67
  • 93
  • Not sure why you got down voted.... It seemed like a plausible mistake by the OP. @AHungerArtist even said he reversed it in comments below. – jww Jan 18 '14 at 12:18
2

if you what to obtain "ONE" by giving in 100 then

initialize hash map by

hashmap = new HashMap<Object,String>();

haspmap.put(100,"one");

and retrieve value by hashMap.get(100)

hope that helps.

  • Not really cause i made this map like this in order to give the string and return the value. Now i need to to the opposite for debugging. – kechap Nov 13 '11 at 16:45
1
public class Class1 {
private String extref="MY";

public String getExtref() {
    return extref;
}

public String setExtref(String extref) {
    return this.extref = extref;
}

public static void main(String[] args) {

    Class1 obj=new Class1();
    String value=obj.setExtref("AFF");
    int returnedValue=getMethod(value);     
    System.out.println(returnedValue);
}

/**
 * @param value
 * @return
 */
private static int getMethod(String value) {
      HashMap<Integer, String> hashmap1 = new HashMap<Integer, String>();
        hashmap1.put(1,"MY");
        hashmap1.put(2,"AFF");

        if (hashmap1.containsValue(value))
        {
            for (Map.Entry<Integer,String> e : hashmap1.entrySet()) {
                Integer key = e.getKey();
                Object value2 = e.getValue();
                if ((value2.toString()).equalsIgnoreCase(value))
                {
                    return key;
                }
            }
        }   
        return 0;

}
}
this
  • 5,229
  • 1
  • 22
  • 51
0

If you are not bound to use Hashmap, I would advise to use pair< T,T >. The individual elements can be accessed by first and second calls.

Have a look at this http://www.cplusplus.com/reference/utility/pair/

I used it here : http://codeforces.com/contest/507/submission/9531943

pukingminion
  • 117
  • 10