egimaben
egimaben

Reputation: 833

Exact cause of hash code collisions in HashMap

I know how hashmaps work and how they resolve collisions. But many answers related to collisions on SO point to two reasons:

My assumption is that reason 2 above is also correct, as am sure of reason 1.

Only 1 question:

NB: This question came closest to what I want to understand. However, the accepted answer seems to discard reason 2 altogether, so it still leaves me wondering.

Upvotes: 0

Views: 111

Answers (1)

Kayaman
Kayaman

Reputation: 73558

Your reason 2 is flawed. A bucket may be reused even if there are plenty of empty buckets if adding two (unequal) objects that end up in the same bucket (hashcode % buckets.length). There's no "getting full" element here, except that as buckets get filled there's obviously a greater chance of ending up in a non-empty bucket.

Upvotes: 2

Related Questions