• Post Reply Bookmark Topic Watch Topic
  • New Topic

hashcode  RSS feed

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading some articles about Hashcode, here is my conclusion.

1. hashcode is for loop-up speed. so it mostly used with hashset...
2. hashcode itself doesn't need to be unique, but with equals(), it has to be unique.
3. how hashcode works ? for a container, like hashset, it will calculate hashcode, then put it into an array. if no collision, it will be perfect. if has collision, it will put it into array's array. Searching an object, it will search a hashcode (integer) in that array, so it will be faster.

My understanding is right ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#1 and #3 are right. For #2, hashcode doesn't have to be unique regardless of whether you use equals. Equals has to be able to tell unique objects even if different objects have the same hashcode.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important thing to remember about the relationship between hashCode() and equals() is this:

When two objects are equal (when equals() returns true when you compare the two objects), then the hashCode() of the two objects must also be the same.
 
Baichuan Huang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:The important thing to remember about the relationship between hashCode() and equals() is this:

When two objects are equal (when equals() returns true when you compare the two objects), then the hashCode() of the two objects must also be the same.

yes,i agree with you。

http://isagoksu.com/2009/development/java/what-is-hash-code/
hope this helps。
HashCode is useful in many data structures。
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!