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
When designing Guava's ImmutableSet
and ImmutableMap
classes, we opted not to cache hash codes. This way, you'll get better performance from hash code caching when and only when you care enough to do the caching yourself. If we cached them ourselves, we'd be costing you extra time and memory even in the case that you care deeply about speed and space!
It's true that HashMap
does this caching, but it was HashMap
's author (Josh Bloch) who strongly suggested we not follow that precedent!
Edit: oh, also, if your hashCode()
is slow, the caching by the collection only addresses half of the problem anyway, as hashCode()
still must be invoked on the object passed in to get()
no matter what.