When I implement a collection that uses hashes for optimizing access, should I cache the hash values or assume an efficient implementation of hashCode()
?
On the other hand, when I implement a class that overrides hashCode()
, should I assume that the collection (i.e. HashSet
) caches the hash?
This question is only about performance vs. memory overhead. I know that the hash value of an object should not change.
Clarification: A mutable object would of course have to clear the cached value when it is changed, whereas the collection relies on objects not changing. But this is not relevant for my question.