I want to have a map in Java so I thought HashMap
. But now, I want to overcome the overhead introduced by hashing. Basically there are objects that hold a key for the map for a very short time - but many of them, and polling/replacing a value often. So I thought the overhead from hashing the key every time get()
or set()
is being called could be significant and also overcome. So I thought of, with the key, saving the hash of the key - and reducing overhead.
Is it possible in Java to get()
and set()
a value with the key and a precomputed hash for the key - and of course getting that key from the map for consistency to avoid the overhead of the map having to hash the key?
An object can cache its hash code, so that when it's used repeatedly as a key, hashing overhead can be eliminated. For example, java.lang.String
does this. The Integer
class effectively does the same. Your custom key class can follow this pattern.