I want to search for a key in a hashmap and find the nearest one to that key!
HashMap map = new HashMap();
Using a NavigableMap like a TreeMap
long key =
NavigableMap map = new TreeMap();
Long before = map.floorKey(key);
Long after = map.ceilingKey(key);
if (before == null) return after;
if (after == null) return before;
return (key - before < after - key
|| after - key < 0)
&& key - before > 0 ? before : after;