title: Reusing cached instances
body: <p>I have an application that uses a data structure <code>Point</code>. Let's say that overall there are 50 distinct instances of <code>Point</code> (meaning <code>p1.equals(p2)</code> == false). However during calculation loads of new instances are created, that are actually the same as already instantiated objects.</p>
  
  <p>As these instances are stored this has a heavy impact on memory consumption:
  50 distinct <code>Point</code>s are represented by 500'000 instances of <code>Point</code>. In the data structure there is nothing that would prevent the reuse of already present instances. For that reason I created a cache:</p>
  
  <pre><code>HashMap&lt;Point, Point&gt; pointCache = new HashMap&lt;&gt;();
  </code></pre>
  
  <p>So I can check if the point is present and add it if it is not. This kind of cache however seems like a bit of overkill, as the key and the value are essentially the same.</p>
  
  <p>Furthermore I already have a map present:</p>
  
  <pre><code>HashMap&lt;Point, Boolean&gt; flag = new HashMap&lt;&gt;();
  </code></pre>
  
  <p>What I am curious about is: Is there a map like data structure that I could use for <code>flag</code> that would allow the retrieval of the key? If not is there any other data structure that I could use for the cache that would be more like a set and would allow easy checking and retrieval?</p>
  
  <p><strong>EDIT:</strong> For completeness, the <code>Point</code> class I am using is <code>javafx.geometry.Point2D</code> and therefore nothing that I can change.</p>