Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode() and equal() New Doubt :

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • 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 ?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for clearing the doubt...
 
Lucas Lee
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic