0

Hashmap key in java

How to get the key of known value using HashMap in java?

27th Dec 2020, 6:53 PM
HBhZ_C
HBhZ_C - avatar
3 Answers
0
HBhZ_C yes. That is literally what I answered. Visit the link I mentioned in my previous answer. The top answer gives some solutions. I recommend you read that answer, then check out each collection/method mentioned in the answer so that you know what they do. And then attempt again to find a solution to your problem.
27th Dec 2020, 7:27 PM
XXX
XXX - avatar
+ 1
I don't know why you would want to do that, because there can be multiple keys mapping to the same value. Anyways, here is a useful answer I found https://stackoverflow.com/questions/1383797/java-hashmap-how-to-get-key-from-value As said in the top answer of the linked question, you will need to maintain 1:1 relationship, that is, 1 key mapping to 1 value, because if multiple values map to the same value, then which key should the program return? An ideal solution could be making a function to add a key value pair, and in that function you can check if the key already exists in the hash map, and accordingly give suitable errors.
27th Dec 2020, 7:09 PM
XXX
XXX - avatar
+ 1
It was a method in java HashMap to get the value given its key but the inverse is it possible to give the value for unknown key and get its key XXX .
27th Dec 2020, 7:21 PM
HBhZ_C
HBhZ_C - avatar