title: HashMap get value of object that is "equal" but different hash?
body: <p>I've got a <code>HashMap&lt;Point, T&gt;</code> data structure which holds several points that are mapped to other instances of the class <code>T</code>. This map is given some default values when my class is instantiated like this:</p>
  
  <pre><code>T t1 = new T();
  T t2 = new T();
  Point p1 = new Point(0, 1);
  Point p2 = new Point(0, 2);
  
  HashMap&lt;Point, T&gt; map = new HashMap&lt;Point, T&gt;();
  static {
      map.put(p1, t1);
      map.put(p2, t2);
  }
  </code></pre>
  
  <p>In my code, I will be receiving events that contain an <code>x</code> and an <code>y</code> value. When I receive one of these events I'm trying to create a new <code>Point</code> object with the <code>x</code> and <code>y</code> that is passed and then retrive the value from the map like this:</p>
  
  <pre><code>Point p = new Point(event.getX(), event.getY); // Assume (x, y) = (0, 1) (p1)
  if(p.equals(p1)    
      T t = map.get(p);
  </code></pre>
  
  <p>Although <code>p</code> is equal to <code>p1</code> in this case (with (x, y) = (0, 1) I am getting a <code>null</code> value back from the <code>map</code>. I assume that is because the <code>hashCode()</code> method in <code>Point (Point2D)</code> uses something else than <code>equals</code> to calculate an unique hash, which it should to prevent collisions.</p>
  
  <p>My question is: <em>How can I retrive the value from the <code>map</code> using the new instance <code>p</code>? Is there another data structure that would fit the use case?</em></p>
  
  <p>I guess I could use <code>toString()</code> or some other mapping like <code>HashMap&lt;String, T&gt;</code> or perhaps I would extend the <code>Point</code> class and <code>Override</code> the <code>hashCode()</code> method to suit my purposes. These ways feel "hacky" though, if there is a cleaner way I'd love to hear it.</p>