title: Java - How to find a value from a hashmap that is the closest to a particular number?
body: <p>Hi I have a <code>HashMap&lt;String, Double&gt;</code> and also a function which returns a double value known as <code>answer</code>. I want to check which value in the HashMap is the closest to the answer and then grab that value's key and print it.</p>
  
  <pre><code>HashMap&lt;String, Double&gt; output = new HashMap&lt;String, Double&gt;();
  
  
  contents
  ("A", 0)
  ("B", 0.25)
  ("C", 0.5)
  ("D", 0.75)
  ("E", 1)
  </code></pre>
  
  <p>Suppose the answer to one of my functions was 0.42, how can I check which value it is closest to and then grab the key to that value. I cant switch around the key and value of the HashMap (as a previous function assigns the values equally to each letter), otherwise it would be better to go through each key and get the value.</p>