Hi I have a HashMap
and also a function which returns a double value known as answer
. I want to check which value in the Hash
With a HashMap
, you have to go through each entry.
However, if performance is important and you're going to be finding several entries this way, you could create another collection: a list or an array of the entries in the hash map, sorted by value. You could then do a binary search to find the entry with the nearest value more efficiently, and return the key. Of course that doesn't help if you're only going to do this once per map...