I have been reading up on HashMap Collisions and I have found two different definitions and am not sure which is the correct one.
Collision resolution in Java HashMap
The top answer in this is "Collision happens when multiple keys hash to the same bucket."
I see another answer in the same question that is "A collision happens when two different keys happen to have the same hash code, or two keys with different hash codes happen to map into the same bucket in the underlying array."
For #1, multiple keys can hash to the same bucket, but have different hashCodes. Different HashCodes can be % or & to the same bucket, but be different numbers.
For #2, it is saying multiple keys have the same hashCode, and therefore hashing to the same bucket.
Which is the correct definition? Or are there different terms for both these occurences?
Edit: I cannot comment on the answers for clarification unfortunately, as I do not have the points for it. Thanks
CodePudding user response:
A "collision" in the broader context of using hashes is a hash collision.
In the context of a HashMap
, a "collision" means the hash code of two objects resulted in them being stored in the same bucket. Of course, this can result from the two objects returning the same value from hashCode()
, but also (more likely) that their hash codes when used to compute the bucket resulted in the same bucket.