File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equals and HashCode

 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was reading the K&B book, when i came to this



this code i made, says the contrary



thanks !
[ October 10, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
 
Varun Chopra
Ranch Hand
Posts: 211
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your book must also say whenever you override equals(), you have to override hashcode() as well.
 
Thomas Thevis
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code i made, says the contrary

Well, you can always create code breaking so called general contracts. The book specifies the conditions which have to be fulfilled to satisfy the general contract for java.lang.Object.hashCode() (have a look at the javadocs there).
It is necessary to satisfy the general contract to ensure that other classes relying on that contract work properly. For example, hash based collections like java.util.HashSet may not work properly if you break that contract. Do you know why?

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