aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode() and equal() New Doubt : Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode() and equal() New Doubt :" Watch "hashCode() and equal() New Doubt :" New topic
Author

hashCode() and equal() New Doubt :

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
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

Joined: Sep 23, 2000
Posts: 3313
    
    7
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Thanks for clearing the doubt...
Lucas Lee
Ranch Hand

Joined: Oct 02, 2006
Posts: 53
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode() and equal() New Doubt :