title: How do I generate an (almost) unique hash ID for objects?
body: <p>How can I get an ID for my objects that makes it easy to distinguish it from others?</p>
  
  <pre><code>class MyClass {
      private String s;
      private MySecondClass c;
      private Collection&lt;someInterface&gt; coll;
      // ..many more
  
      public Result calculate() {
          /* use all field values recursively to calculate the result */
          /* takes considerable amount of time. Implemented */
          return result;
      }
  
      public String hash() {
          /* use all field values recursively to generate a unique identifier */
          // ?????
  }
  </code></pre>
  
  <p><code>calculate()</code> usually takes ~40 seconds to complete. Thus, I do not want to call it multiple times. </p>
  
  <p><code>MyClass</code> objects are quite huge (~60 MB). The <code>Result</code> value of the calculation will only be ~100 KB.</p>
  
  <p>Whenever I am about to run the calculation on an object, my program should look up if that has been done some time earlier already, with the exact same values, recursively. If so, it will look up the result in (e.g.) a <code>HashMap</code> instead. Basically, <code>MyClass</code> objects itself could be used as keys, but the HashMap will include 30-200 elements - I obviously don't want to store all of that in full size. That's why I want to store 30-200 <code>Hash/result</code> values instead.</p>
  
  <p><strong>So, I thought I'd generate a ID (hash) over all values inside my <code>MyClass</code> object. How do I do that?</strong> This way, I can use that very hash to look up the result. <strong>I am aware that a hash code like MD5 will not guarantee 100% uniqueness</strong>, because multiple objects might have the same hash. However, if I store (at maximum) 200 elements via MD5, the chance for a twice used hash will be neglectible, I think. There are <code>16^32=3.4e38</code> different hash codes possible. I'll be happy to hear anybodys comments about it, or see other approaches.</p>
  
  <p>Once the hash is generated, I don't need that object anymore, just its respective <code>result</code> value.</p>
  
  <p>Two seperate objects with the exact same values have to return the same hash code. Much like original hashCode(), just with that I'm trying to maintain uniqueness. The probability for two objects having the same hash code should be absolutely neglectible.</p>
  
  <p>I don't know how to describe the problem in other words anymore. If further clarification is needed, please ask. </p>
  
  <p><strong>So how can I generate my <code>MyClass.hash()</code>?</strong></p>
  
  <p>The problem isn't really about how or where to store the hashes, because I don't even know how I can generate an (almost) unique hash for an entire object, that will always be the same for same values.</p>
  
  <hr>
  
  <p><strong>Clarification:</strong></p>
  
  <p>When talking of size, I mean the serialized size on the hard drive.</p>
  
  <p>I don't think putting the objects in a HashMap would decrease their size. That's whay I want to store some hash String instead. <code>HashMap&lt;hashStringOfMyClassObject, resultValue&gt;</code></p>
  
  <blockquote>
    <p>When you put an object in a HashMap (either as a key or as a value), you don't create a copy of it. So storing 200 large objects in a HashMap consumes little more memory than the 200 objects themselves. </p>
  </blockquote>
  
  <p>I do not store 200 large objects themselves. I only keep 200 different results (as values) which are small, and 200 respective hashCodes of MyClass objects which are also very small. The point of "hashing" the objects is to be able to work with the hash instead of with the object values themselves.</p>