Get key from a HashMap using the value [duplicate]

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?

3

8 Answers

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.

4

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.

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

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

1

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"

1

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 byhashMap.get(100)

hope that helps.

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;
}
}
1

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

I used it here :

1

You Might Also Like