This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about hashSet

 
Sammy Bill
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i was expecting this code to return 1 as i overrode equals. but it returned 2?
why is that?

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was expecting this code to return 1 as i overrode equals. but it returned 2?
why is that?


The hashing collections require that you to obey the equal() / hashCode() contract. If you override equals, then you should also override hashCode() as well -- as you have the code written, you broke the contract.

Henry
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hashCode method is not overridden. So equals will not get called. So answer will be 2...
 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because you have not overridden hashCode().
 
Sammy Bill
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

Got it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic