title: Extracting entry from a Java HashMap based on equality of a field in the key type
body: <p>I have a Java HashMap of type <code>&lt;MyType,Double&gt;</code>. The <code>MyType</code> class has two fields <code>foo</code> (of type String)and <code>bar</code>(of type Double). The equals and hashcode methods for <code>MyType</code> use only <code>foo</code>. Now given an object <code>A</code> of type <code>MyType</code> I need to get the matching entry from the Hashmap. What that means is </p>
  
  <pre><code>MyType A = new MyType();
  A.foo = "foo";
  A.bar = 0.0;
  
  MyType B = new MyType();
  B.foo = "foo";
  B.bar = 1.0;
  
  Map&lt;MyType,Double&gt; myMap = new HashMap&lt;MyType,Double&gt;();
  myMap.put(B,5.0)
  </code></pre>
  
  <p>I need to extract the key <code>B</code>(and eventually its bar value) from <code>myMap</code> based on it's equality with <code>A</code> (since their <code>foo</code> values are same) i.e. a function of the form</p>
  
  <pre><code>Double getBar(MyType type,  Map&lt;MyType,Double&gt; map)
  </code></pre>
  
  <p>such that</p>
  
  <pre><code>getBar(A,myMap) returns 1.0 
  </code></pre>
  
  <p>What is the best way to do that? I am not too sure of how this thing is designed in the first place but I am looking for an efficient way of doing this since <code>myMap</code> is expected to be really huge.</p>
  
  <p>UPDATE: A slightly bigger context here is this. I have a set of MyType objects (say S). An external function works on it and creates a HashMap called myMap which calculates and associates a quantity of type Double with each object in the set. It also updates the bar field of each object in the set. What I get back is myMap. Now I need to update each element in my original set S such that each element's bar value is replaced by what the bar value of the corresponding entry in the returned myMap is. So for each A in S I need to read the corresponding B in myMap, get its bar, and then set the bar of A to be same as bar of B.</p>