• Post Reply Bookmark Topic Watch Topic
  • New Topic

How two different String producing same hashcode/How hashCode Calculated in Java  RSS feed

 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code snippet produces the same value of hashcode. Correct me if i am wrong, it seems with some calculation(in Hexadecimal), both of these string going to same bucket and equals method differentiating them and returning false.
I would like to understand How java calculates the hashcode. Any idea ???




It Produces the following output.
false
FB 2236
Ea 2236
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's how hash code for String is calculated:val is a char[] that is a private member of String class. That array holds the "value" of a String.

It is not possible to assign an unique hash code to each possible value of String. There are 2^32 different hashcodes.
Number of possible Strings is much much larger...

And that is how a hash code for Object is calculated: This is native method so the implementation depends on JVM.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surinder Mehra wrote:I would like to understand How java calculates the hashcode. Any idea ???

Simple answer: No.

However, there is a general pattern for reasonably decent hashes that uses a loop involving addition and multiplying by a prime number; but there are many, many variants.

However, NONE of them guarantees that hashes will always be different for different values (and hashCode() doesn't require it), they simply ensure that that there's a good probability that they will be.

Winston
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not possible to assign an unique hash code to each possible value of String. There are 2^32 different hashcodes.

Pawel Pawlowicz and Winston Gutkowski,,
Thanks for quick reply, i have one more doubt, After 2^32 hash-codes, Isn't it that further coming String will hash to one of these buckets, kind of making the search algorithm inefficient. As in this case, where two different string hashes to same bucket. Does equals method always works right to pick-out the right string. <Please ignore if you feel this is silly question>

Thanks !!!
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surinder Mehra wrote:Does equals method always works right to pick-out the right string.

equals() method will always tell you (or your code ) whether two strings are identical or not. That's its purpose.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surinder Mehra wrote:Please ignore if you feel this is silly question...

Questions are rarely stupid. Like they say on 'Jeopardy': it's only easy if you know the answer.

But you're concentrating on the wrong thing. The question has (or should have, with a decent hash) very little to do with the limit of an int, but much more to do with how many Strings you plan to hash. Assuming that your algorithm is reasonably good, the question is: how many different Strings (and don't forget that Sets and Maps don't allow duplicates) can I hash before collisions start to become an issue?

And the answer to that is: quite a lot. So if you only have a few hundred, or even several thousand, Strings you need to store, you can pretty much forget about it.

Winston
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston !!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!