• 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

hashCode() and equal() New Doubt :

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we will talk aboutthe following :

HashSet,LinkedHashSet
HashMap,LinkedHashMap.

A. Is it true that if we don't provide our hashCode() then default Object class hashCode() will add each (our) object in a sepearte bucket. Is this true?

If yes, then

1. In HashSet and LinkedHashSet even duplicate(meaninfully equal) objects will be added in seperate bucket and as no two object are in the same bucket, then no two objects can be equal.

2. in K&B at Page-563, it's written that :

Remember that when you use a class that implements Map, any classes that you
use as a part of the keys for that map must override the hashCode() and equals()
methods. (Well, you only have to override them if you're interested in retrieving
stuff from your Map. Seriously, it's legal to use a class that doesn't override equals()
and hashCode() as a key in a Map; your code will compile and run, you just won't
find your stuff.)



Please let me know why is this so ?
 
Enthuware Software Support
Posts: 4797
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandeep Vaid:

A. Is it true that if we don't provide our hashCode() then default Object class hashCode() will add each (our) object in a sepearte bucket. Is this true?



That is true because Object class's hashCode (and equals) is based on the memory address of the Object which is unique for every object. So no two objects (i.e. objects of classes that don't implement equals and hashCode method) can be considered equal.

Originally posted by Sandeep Vaid:

If yes, then

1. In HashSet and LinkedHashSet even duplicate(meaninfully equal) objects will be added in seperate bucket and as no two object are in the same bucket, then no two objects can be equal.



Right. Because of the above reason. Do not forget that we are talking about classes that do not implement hashCode and equals methods. If you think two objects of a class can be meaningfully equal (even though they are two different physical objects), then you ought to implement equals and hashCode in that class.

Most of the times, objects such as Strings and Integers are used as keys in Maps. They implement (override) hashCode and equals methods properly. So it is never a problem to use them in Maps.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clearing the doubt...
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Paul.
In addtion,
If you don't provide your own implemetion of hashCode() and equals() methods,your classes will inherit these two methods from Object class as default implemetation which base on memory address.
 
reply
    Bookmark Topic Watch Topic
  • New Topic