• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

A question About overriding equals and hashcode for an Object

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should help you understand.
 
Campbell Ritchie
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


, thanks david , every project is developed for that only know .
 
Campbell Ritchie
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Kiran V wrote: , thanks david . . . .

His name's Dieter
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic