• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode() usage

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, This is from Whizlabs..Could you please suggest usage
 
Stephan van Hulst
Bartender
Pie
Posts: 5592
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop posting topics and read the contract.

Look at the last two bullets.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Stop posting topics and read the contract.

Look at the last two bullets.
Steve, I read the contract. Maybe I am not clear .
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hashcode of two objects can be same value, and to further confirm it, you can use the equals() method. But, for two objects, if the hashcode are different, then you can say, both are unequal. This scenario in used in HashMap, etc
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan stop posting replies that are not relevant to the question. Thank you.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dude it's simple.

If a1.hashcode() != a2.hashcode(), then they can never be equal and hence it confirms inequality.

However, if a1.hashcode()== a2. hashcode(), it doesn't confirm that a1 equals a2, only after (a1equalsa2)=true, you know that they are equal.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be polite here, and, Rohit, you are mixing == and equals() method. The original post question asked with related to usage of hashCode() and equals() methods. If you follw the contract, then only you can expect the intended behavior.

In HashMaps, it checks the hashCode() of two objects, if both are same, then it go further to check whether the objects are meaning fully equal. If the hashCode() aren't same, it assumes, the objects shouldn't be meaningfully equal.

But, if don't follow the contract, you can't expect the above behavior.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit Ramachandran wrote:Stephan stop posting replies that are not relevant to the question. Thank you.

Stephen posted a link to the hashCode-equals contract which I think is on topic. Also your tone is against the Be Nice rule at javaranch. Stephan your reply could also have been more nice in reminding Harikrishna to read the contract...
 
Stephan van Hulst
Bartender
Pie
Posts: 5592
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize, and I apologize to Harikrishna.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran, I edited my code, each time I used = i meant equals but yeah my bad.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rohit, I am clear with your reply. Back to my original question, object inequality means, are you referring something like this, !(o instanceof Question01) from the following code ?
Rohit Ramachandran wrote:Dude it's simple.

If a1.hashcode() != a2.hashcode(), then they can never be equal and hence it confirms inequality.

However, if a1.hashcode()== a2. hashcode(), it doesn't confirm that a1 equals a2, only after (a1equalsa2)=true, you know that they are equal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic