• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashCode  RSS feed

 
keshav pradeep ramanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
The hashcode generated are unique code for every object.
But how do the collisions occur in HashMaps even if the hashcodes generated are unique?
Please correct me if i am wrong
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hashcodes are not unique.
Hashcode is an 32-bit integer, meaning there are 2^32 = 4,294,967,296 different hashcodes.
When you put objects with coliding hashcodes (and not not equal by means of equals method) into hashmap they are stored in the same bucket on a list.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keshav pradeep ramanath wrote: . . . The hashcode generated are unique code for every object. . . .
Whoever told you that is mistaken. You can have two different objects with the same hash code. There was a recent discussion about a related question: this thread. This is an example where two different objects have the same hash code.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not just that two objects can have the same hashcode() (and therefore go into the same bucket), but also that two objects with different hashcodes() can go into the same bucket due to a super complicated series of formulas that convert hashcodes to indexes. Some friendly advice: don't try and figure those formulas out .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!