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
If your values are unique, you can use a TreeMap, which implements NavigableMap, which has the nice ceilingKey and floorKey methods:
NavigableMap map = new TreeMap<>();
map.put(0d, "A");
map.put(0.25, "B");
map.put(0.5, "C");
map.put(0.75, "D");
map.put(1d, "E");
double value = 0.42;
double above = map.ceilingKey(value);
double below = map.floorKey(value);
System.out.println(value - below > above - value ? above : below); //prints 0.5
Note: both methods can return null if value is less (resp. greater) than the smallest / largest key.