How can I get an identifier for my objects, which makes it easy to distinguish it from others?
class MyClass { private String s; private MySecondClass c; private Collection<someInterface> coll;
calculate() usually takes ~ 40 seconds. Thus, I do not want to call him several times.
MyClass objects are pretty huge (~ 60 MB). The value of the Result calculation will be only ~ 100KB.
Whenever I am going to perform a calculation on an object, my program should look, if it has been done some time earlier, with exactly the same values, recursively. If so, it will look for the result (for example) of a HashMap . In principle, MyClass objects themselves can be used as keys, but the HashMap will include 30-200 elements - I obviously do not want to store all this in full size. Therefore, I want to keep the values of 30-200 Hash/result .
So, I thought that I would create an identifier (hash) for all the values inside my MyClass object. How to do it? That way, I can use this hash to find the result. I know that a hash code such as MD5 does not guarantee 100% uniqueness , because several objects can have the same hash. However, if I save (maximum) 200 elements through MD5, the likelihood of using a twice-used hash will, as it seems to me, be negligible. There are 16^32=3.4e38 different hash codes. I will be glad to hear from him anybodys comments or see other approaches.
After creating the hash, I no longer need this object, just its Result value.
Two separate objects with the same values must return the same hash code. Like the original hashCode (), it's just that I try to maintain uniqueness. The probability for two objects having the same hash code should be absolutely negligible.
I do not know how to describe the problem in other words. If further clarification is required, please ask.
So how can I generate my MyClass.hash() ?
The problem is not how and where to store the hashes, because I don’t even know how I can generate a (almost) unique hash for the whole object, which will always be the same for the same values.
Clarification:
Speaking of size, I mean the serialized size on the hard drive.
I do not think that placing objects in a HashMap will reduce their size. That I want to save some hash string instead. HashMap<hashStringOfMyClassObject, resultValue>
When you put an object in a HashMap (either as a key or as a value), you are not creating a copy of it. Thus, storing 200 large objects in HashMap consumes a bit more memory than 200 objects.
I do not store 200 large objects. I save only 200 different results (as values) that are small, and 200 corresponding hash codes of MyClass objects, which are also very small. The hash point of objects should work with a hash, not with the values of the object itself.