File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes is it necessary to provide hashCode with equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is it necessary to provide hashCode with equals" Watch "is it necessary to provide hashCode with equals" New topic
Author

is it necessary to provide hashCode with equals

Ram Para
Ranch Hand

Joined: Jul 09, 2008
Posts: 50
Is it necessary to provide hashCode method when I am overriding equals method although I am not going to use the object with any Map implementation.


Ram Parashar
http://doinfinite.com
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Yes it is; that's how the contracts of both equals and hashCode work. When you override one, you must make sure the contract is not violated. You may not run into problems now, but in the future perhaps. And then it is so much harder to find out why errors occur.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Lucas Alberione
Greenhorn

Joined: Aug 15, 2009
Posts: 8
See this post


Lucas
 
jQuery in Action, 2nd edition
 
subject: is it necessary to provide hashCode with equals