Get key from a HashMap using the value

157,800

Solution 1

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.

Solution 2

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.

Solution 3

  • 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)

Solution 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).

Solution 5

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"

Share:
157,800
kechap
Author by

kechap

Updated on October 26, 2020

Comments

  • kechap
    kechap over 3 years

    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?

  • kechap
    kechap over 12 years
    Ops i wrote it reversed. I will fix it.
  • kechap
    kechap over 12 years
    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
    kechap over 12 years
    This will run only while debugging so run time is not a problem.
  • Mark Peters
    Mark Peters over 12 years
    +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.
  • kbyrd
    kbyrd over 12 years
    @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.
  • kechap
    kechap over 12 years
    It works like a charm. Thanks.
  • jww
    jww over 10 years
    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.
  • Ritesh Sinha
    Ritesh Sinha almost 9 years
    This is better than the accepted answer.
  • thonnor
    thonnor over 8 years
    Brilliant little method - worked a treat for what I needed - I'm going to make good use of this example in the years to come I reckon.
  • user207421
    user207421 over 8 years
    The question is about Java, not C++.
  • user207421
    user207421 over 8 years
    Mere code is not an answer. You have to explain.