PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

[FIXED] What is the correct definition for a HashMap Collision?

 September 20, 2022     hashmap, java     No comments   

Issue

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

  1. The top answer in this is "Collision happens when multiple keys hash to the same bucket."

  2. 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


Solution

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.



Answered By - Bohemian
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing