File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes collections doubt 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 "collections doubt" Watch "collections doubt" New topic
Author

collections doubt

gobburi saikrishna
Ranch Hand

Joined: Jan 21, 2008
Posts: 52
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

Joined: Jun 04, 2007
Posts: 331
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


Blog
naga sandeep
Greenhorn

Joined: Apr 03, 2007
Posts: 13
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..


......it's my lie...f ....I am gonna live 4r ever
naga sandeep
Greenhorn

Joined: Apr 03, 2007
Posts: 13
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

Joined: Jan 30, 2008
Posts: 11
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Gayathri Sugavanam
Greenhorn

Joined: Apr 03, 2008
Posts: 4
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

Joined: Oct 02, 2003
Posts: 11230
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
I would say Visitor design pattern

http://www.oodesign.com/visitor-pattern.html


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
 
Don't get me started about those stupid light bulbs.
 
subject: collections doubt