File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes A question About overriding equals and hashcode for an Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "A question About overriding equals and hashcode for an Object" Watch "A question About overriding equals and hashcode for an Object" New topic
Author

A question About overriding equals and hashcode for an Object

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hello ,
when going through tutorials in web i found this line :

"The Collection classes that implement interface Set or Map
expect the object or key object respectively to override equals "

I need some clarification on the above statement .
Please let me know if the above is true ,because as far as i am concerned i could easily add Objects to a HashSet
without my Object overriding equals or hashcode .

This is my sample Program :




Please clarify .

Thanks


Save India From Corruption - Anna Hazare.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Please always tell us where you found such quotes.

Of course you need not override equals() and hashCode(). As long as you don't mind your collection classes working completely incorrectly . . .

If you want them to work properly, however, you will find that quote is absolutely correct.
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Overriding equals & hashCode are important when it comes to determining the "identity" of an instance.

You can search the forum & find quite a few topics on the same.

You can also go through SCJP study guide or Head First Java for elaborate details.


Regards,
Avishkar Nikale
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi Cambpell ,

Of course you need not override equals() and hashCode(). As long as you don't mind your collection classes working completely incorrectly . . .


This was really funny , could you please add some light on this or provide me a link related to collection classes working of Incorrectly .

Thank you .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

This should help you understand.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
There should be explanations about collections working incorrectly in some of these links: Odersky, Spoon and Venners (courtesy of Garrett Rowe), Angelika Langer and Joshua Bloch's Effective Java™ (you may be able to find a "sample chapter" from the 1st edition on the internet, which describes equals()).
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks Campbell Ritchie and Rob Prime for always as helping hand
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Hi , After going through tutorials you mentioned , i got some idea when to overide equals and HashCode of an Object class .

I assume that , when we are using any Hashing type of Collection class and storing Custom objects under it then we need to override them , if not the searching is not done correctly .

Correct me , if i am wrong .

Thanks in advance .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You should override equals() and hashCode() on every class you intend for serious use, as opposed to trivial training classes.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

on every class you intend for serious use,


Can you please let me know what is the word "for serious use" you mean exactly .

Thanks .
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

Basically, if you're going to publish your classes and let other people use them... override equals() and hashCode().


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



, thanks david , every project is developed for that only know .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Ravi Kiran V wrote: , thanks david . . . .
His name's Dieter
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10202
    
166

Ravi Kiran V wrote:

every project is developed for that only know .


Not really. I have many projects (or rather just some collection of code) which I use as quick examples for trying out various things. i.e. they are not meant for "serious" use.

[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question About overriding equals and hashcode for an Object