So for example we have very poor hash for all custom objects = 2(in bits =..10)
. Somewhere in related posts is said : "HashMap uses power of two size because you can easily pick the bucket by hashCode & MASK where MASK = 000....111
where amount of 1s == current power of 2 used for size. "
So for map length = 2 we have ..10 & 01 = 0
- index for bucket when size is 2.
For size = 4 we will have: ..010 & 11 = 10(= 2dex)
- index for size 4.
For size = 8 we will have: ..010 & 111 = 10(= 2dex)
- again for size 8.
So in this simple situation we will have 2 different buckets for same object key.(In general map method hash(int hashCode) does the same - it can produce different bucket indexes for same object hash - depending on the map size - to handle collisions on lower bits). When u perform get() on the map - does it go through all this different buckets suitable for the same key - or not? Or how does is track all needed buckets for object hash?
Why hash method in HashMap