Meaningless Drivel is fun!*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 19543
    
  16

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
 
Consider Paul's rocket mass heater.
 
subject: is it necessary to provide hashCode with equals
 
Similar Threads
Overriding and Implementation of hashcode()
hashCode and == ...
Regarding Hashcode and Equals
Set concept in Java of Collection
Overriding and Implementation of hashcode()