title: What is the most efficient way to implement a findOrCreate pool in Scala?
body: <p>I would like to create a pool of objects of some type, let's say <code>Person</code>, implemented as follows:</p>
  
  <pre><code>class Person(val name: String, val email: String) { 
    val data = expensiveComputation(name, email)
  }
  </code></pre>
  
  <p>and have a lookup method that looks like the following:</p>
  
  <pre><code>def findOrCreate(name: String, email: String): Person
  </code></pre>
  
  <p>This method should have the property that it always returns the same object for a given set of parameters to <code>findOrCreate</code>, creating it if it doesn't exist. Furthermore, I would like the objects to have cheap equality testing and hashing.</p>
  
  <p>When these objects are initialised, some expensive computation is required, so I really want to avoid instantiating an object twice. In addition, the domain (actually sequence processing in bioinformatics) would make me instantiate each object 10-100 times if I didn't have a lookup facility, so memory costs are another compelling reason.</p>
  
  <p>The best I have thought of is to create a mutable <code>HashMap</code> (to avoid GC overhead, since I will be creating a large number of objects) which maps the parameters to Person instances, and check for existence in the map on each call. In addition I would override equals with calls to <code>eq</code>, and leave <code>hashCode</code> as the default <code>AnyRef.hashCode</code> implementation. </p>
  
  <p>Is there a better way of implementing this pattern? </p>