permaculture playing cards*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 19656
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is it necessary to provide hashCode with equals
 
Similar Threads
hashCode and == ...
Set concept in Java of Collection
Regarding Hashcode and Equals
Overriding and Implementation of hashcode()
Overriding and Implementation of hashcode()