aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equals and hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equals and hashcode" Watch "Equals and hashcode" New topic
Author

Equals and hashcode

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
If two objects are considered equal, their hashcodes must also be equal.

Is the reverse true ??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

jose chiramal wrote:Is the reverse true ??


No.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
What you think?

My Answer is NO
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

No Reverse is not true. Please check here for more information on Hashcode
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Jose : Hashcodes are not used for comparing objects. Rather, they are used
to tag objects for quick access. In fact, they are so useful for this that the
hashCode() method is available on all Objects.

A sometimes useful test is to compare two object handles, varA and varB.
If varA == varB is true, then they both point at the same object. From a
user perspective, however, a.equals(b) is the most important equality test.
If you want two strings, str1 = "MAGIC" and str2 = "magic" to be considered
equal, you must override the equals() method to make it so.

Back to hashCode(). Consider a sign outside a voting place telling you where
to vote in the building. Those with a last name beginning with A-F are to join
those in room 1; G-L joins room 2, M-R room 3 and S-Z room 4. Why do
this? To find my wife quickly; for me in room 2. Software uses many more
buckets (rooms) and this greatly speeds finding objects.

To find an object:
1) Calculate its (or its key's) hashcode,
2) Find objects/keys sharing this hashcode,
3) Use the equals() method to find your target.

For my wife search to succeed, she must go to room 2, and I must also go to
room 2. So "H" must always hash to room 2. But many Johnsons will also be
found there. So equal hashcodes say little about object equality.

Jim ... ...


BEE MBA PMP SCJP-6
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Back to hashCode(). Consider a sign outside a voting place telling you where
to vote in the building. Those with a last name beginning with A-F are to join
those in room 1; G-L joins room 2, M-R room 3 and S-Z room 4. Why do
this? To find my wife quickly; for me in room 2. Software uses many more
buckets (rooms) and this greatly speeds finding objects.


WOW! The voting booth example is an excellent analogy of the hashCoding process in practical life. That makes things really clear.

Find the bucket in which the item/object will be present quickly using the hashCode and then use equals() to figure out if the object is actually found in the bucket or not. If nothing matches for that hashCode then we immediately eliminate the possibility that the object might be present, thus avoiding wasting anymore time searching for that object. If not, we have a very few object to search for corresponding to those that are present in that bucket that matches that particular hashCode. Usually good hashCode algorighms will ensure that there is only one object per bucket. If the hashCode is not that good then it will lead to collisions which means there could be multiple objects in that bucket and hence they have to be compared serially one at a time using the equals() method until we either find a match or find none at all. The worst case scenario is all objects having the same hashCode, in which case the search is 100% linear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equals and hashcode