Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question About overriding equals and hashcode for an Object

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should help you understand.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie and Rob Prime for always as helping hand
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, if you're going to publish your classes and let other people use them... override equals() and hashCode().
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


, thanks david , every project is developed for that only know .
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote: , thanks david . . . .
His name's Dieter
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic