I want to search for a key in a hashmap and find the nearest one to that key!
HashMap map = new HashMap();
If you are inserting values into the hashMap in a sorted order you could use a LinkedHashMap so that the insertion order can be maintained.
Now we can perform a binary search over the keys instead of iterating over all keys (like in some of the other answers).
If iteration over all keys took n comparisons, binary search would take log(n)with base 2 comparisons.
The point to note here would be that this works only if the map is sorted.
A hash map is good as a general purpose map implementation that provides rapid storage and retrieval operations. However, it falls short because of its chaotic and unorderly arrangement of entries.
This causes it to perform poorly in scenarios where there is a lot of iteration as the entire capacity of the underlying array affects traversal other than just the number of entries.
We could say a linked hash map reduces the chaos in the ordering of a hash map without incurring the performance penalty of a tree map.