Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between hashCode and equals method

 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Can you please tell me what is the basic difference between hashCode and equal method.

Thanks
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to understand why we need hashCode method and why we need
equals method and how they are interlinked.
Try to understand how equals and hashcode method are used with
maps.
Then you will be able to understand difference and similarities between
two methods.
 
Bod Toki
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's an easy way to sort out the difference

SCENARIO:
...we want to sort out a couple of balls- so it's easy to find them later. some red, some blue, some green, etc...

20 balls in all (labeled 1 - 20, each ball has a unique number)
5 greens (1 - 5)
5 reds (6 - 10)
5 blues (11 - 15)
5 yellows (16 - 20)


...what we do is pick a ball from our pile- if it's red, it goes in our red bucket, blues in blue bucket, etc...

...if the two of us have that agreement (ie. me sorting the balls into their relative colored buckets and you coming for a ball later), then you can come later on (after i have left) and find it an easy task to find that red ball (say marked number 9) that you are looking for.

...so here's how it relates to java...

HASHCODE
the colored buckets- quick way for you to know where the ball you are looking for is.

EQUALS
that unique number (remember 1 - 20) that identifies the exact ball you are looking for.

CONCLUSSION
if the methods are implemented properly (yes -some wack programmers don't implement them properly - be sure you will see this on the exam) then if we are looking for the no. 9 ball (from our earlier chart), the ball must be red and it must be in the red bucket.

a no. 6 - 10 ball is red
but

a red ball is not necessarily a no. 9 ball-> figure that out :roll:
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The hashcode is a unique number to identify the object. Mostly we use hashcode to identify a particular object. Let stake the following situation...


We are classifying the objects one some attribute and all objects which are mathcing on one attribute will be placed in one queue and like that ....

First all objects with same hascode are put in single line and to differentiate with in single line we use the equlas.

So for any given two objects,

if hashcode is same then it is not neccessary that two objects are equal.
but
if they are equal then their hashcode must be same.

Try some hands on work ....

More inputs are welcome....
 
Cosima Laube
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bod,

thanks a lot for taking the effort of writing down this scenario. Now I have a picture in mind about the difference between hashCode and equals. :-)

With regards,
Cosima
[ September 02, 2006: Message edited by: Cosima Laube ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic