• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() and hashCode()

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone give a detailed description on equals() and hashcode()with a simple example
[ March 30, 2008: Message edited by: Preethi Enukonda ]
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preethi,

unfortunately the full concept of equals() and hashCode() and how they can be implemented can't be said in a few words.

In short equals() is used to compare to objects for equality. The default implementation just compares the objects for identity which is the same you'd get if you used the == operator. If this isn't satisfying you could override equals. The overridden equals() should meet some rules in order to work as expected.

hashCode() is primarily important if you want to use an object as a key for one of the hash based collection classes. Writing a really good hashing functions isn't trivial so I can't explain here in detail. Anyway you should always override hashCode() if you have overridden equals() for your objects to work correctly.

Unfortunately you will have to read a bit about it. Here's a good article from Brian Goetz which explains most things you'll need to know.

Marco
[ March 30, 2008: Message edited by: Marco Ehrentreich ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic