Java - How to find a value from a hashmap that is the closest to a particular number?

后端 未结 4 1667
北荒
北荒 2021-01-14 07:44

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

4条回答
  •  萌比男神i
    2021-01-14 07:50

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

提交回复
热议问题