You could do this by extending HashMap
(read the source code of HashMap
to see what needs to be modified); basically you'd override put
and +=
to not call findEntry
, and you'd override addEntry
(from HashTable
) to simply compute the hash code and drop the entry in place. Then it wouldn't handle collsions at all.
But this isn't a wise thing to do since the HashEntry
structure is specifically designed to handle collisions--the next
pointer becomes entirely superfluous at that point. So if you are doing this for performance reasons, it's a bad choice; you have overhead because you wrap everything in an Entry
. If you want no collision checking, you're better off just storing the (key,value) tuples in a flat array, or using separate key and value arrays.
Keep in mind that you will now suffer from collisions in hash value, not just in key. And, normally, HashMap
starts small and then expands, so you will initially destructively collide things which would have survived had it not started small. You could override initialSize
also if you knew how much you would add so that you'd never need to resize.
But, basically, if you want to write a special-purpose high-speed unsafe hash map, you're better off writing it from scratch or using some other library. If you modify the generic library version, you'll get all the unsafety without all of the speed. If it's worth fiddling with, it's worth redoing entirely. (For example, you should implement filters and such that map f: (Key,Value) => Boolean
instead of mapping the (K,V)
tuple--that way you don't have to wrap and unwrap tuples.)