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

collections doubt

 
gobburi saikrishna
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers

iam confused about equals() and hashCode() method, which has to be over ridden when comparision is made among to collection objects.
if two objects are equal their hshcodes must be equal, is reverse true.
please help me,

Thanks in Advance
[ February 09, 2008: Message edited by: gobburi saikrishna ]
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

if two objects are equal their hashcodes must be equal, is reverse true.


The reverse need not be true.

Consider this:
the hashcode returns the length of a string-field like

and equals returns


Here "Amy" and "May" will have the same hashcode (ie 3) but they are not "equal"


Thanks,
Vishwa
 
naga sandeep
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

What vishwa told is right.... two objects are said to be equal if their hashcodes are equal and the reverse is not true....

You can find this in Kathy Sierra Book also..at chapter 7 page 534 in that table..
 
naga sandeep
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

What vishwa told is right.... two objects are said to be equal if their hashcodes are equal and the reverse is not true....

You can find this in Kathy Sierra Book also..at chapter 7 page 534 in that table..
 
Thiago Furuchima
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

just remember that while using "hash collections", both hashCode and equals are used to help fiding the right "thing".

hashCode is used to put things into the "box", while equals is used to search in the "box".

If two objects have different hashCode, they will be in different "boxes", and will never be found no matter their equals method look like. So "if two objects are equal their hashcodes must be equal" or you cannot find them once you lost their original reference variable.

If two objects have equals hashCode, they will be in the same box, and the equals method is still able to find them, because they are in the same "box". So "if two objects are equal their hashcodes don't necessary needs to be equal".

Hope helped.

tks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by naga sandeep:
...two objects are said to be equal if their hashcodes are equal...

Not quite.

If two objects are equal, then their hashcodes must be equal. But two unequal objects can certainly have equal hashcodes. In other words, we can not say that two objects are equal if their hashcodes are equal.

In the table on p. 534 of K&B, look at the row for "x.equals(y) == false." These objects not equal, but in the third column it says, "No hashCode() requirements." So one possibility is that these unequal objects might have the same hashCode.

For details on the hashCode contract, see the API documentation of the hashCode method in java.lang.Object.
[ February 10, 2008: Message edited by: marc weber ]
 
Gayathri Sugavanam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends

I have a doubt...
Why do we have so...many collection classes even though most of them are quite similar. why such a wide range..?

thanks in advance for any replies
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because even though they are similar, they are different. that's the whole idea...

i can write my code using one of the collection classes. OTHER people then use a common API to interact with my code.

if 6 months later i realize that while i had been using a "Foo" class, perhaps "Bar" would work better. I can change MY code, but since it uses the same API, all those OTHER coders don't have to change a thing.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say Visitor design pattern

http://www.oodesign.com/visitor-pattern.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic