wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode problem" Watch "hashCode problem" New topic
Author

hashCode problem

Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
From enthuware.

Which of the following statements are correct regarding the above class?
Select 1 correct option.
a) This is an invalid implementation of hashCode() method with respect to the given equals() method.
b) Only one of the Info objects will be stored in the HashMap.
c) Both the objects will be stored in the HashMap.
d) An exception will be thrown at run time at line .

I was expecting c) to be correct,as the keys are meaningfully unequal.But the answer says a) as a correct answer?
s1 will return same hashCode for both the keys.SO that is not invalid.
Can anybody please explain?


~Sanjeev Singh<br />SCJP 1.5
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

If s1, s2 and s3 are "aa", "bb", and "cc" in one object, and "a" "abb", and "cc" in another, this equals() method would return "true", but the hashCode() method would return different values -- and that's a violation of hashCode()'s contract: it must return equal values for any two objects for which equals() returns true.


[Jess in Action][AskingGoodQuestions]
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Thanks Ernest,
I got the point that it is violating the hashcode() and equal() contract.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Hi,

1. why do we need to substitute different values and do workout since for this class class is particularly specified by the programmer with the values


Info i1 = new Info("aaa", "aaa", "aaa");
Info i2 = new Info("aaa", "bbb", "ccc");


why don't we just substitute the above i1 and i2 values only for equlas() and hashcode() findings and come to the conclusion that "Thia is an valid implementation of hashCode() method with respect to the given equals() method. ?

2. why c option is not correct.Please Help.
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
As Ernest Friedman-Hill already stated the implementation of "equals(...)" and "hashcode(...)" are not appropriate. So, answer a) is correct.
But of cause answer c) is correct, too.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Answer C is correct.

If the HashMap was a HashSet then Answer C would be incorrect, because a HashSet cares about uniqueness which is determined by the equals method.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3201
    
    2
Hi Sanjeev,
Can you please quote the questionid of the question? I remember discussing this sometime back and option c was deemed to cause unnecessary diversion from the main point. This was later modified.


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

Joined: Jan 05, 2006
Posts: 477
Thanks for the reply Anton and Wilhelm.
While I apply the values given in the question, I get answer (c) as the correct answer for the question.

I also understand Ernest's explanation as to how we might get answer (a) to be the correct answer for this question, if we apply the values as follows: (s1, s2 and s3 are "aa", "bb", and "cc" in one object, and "a" "abb", and "cc" in another)

What I don't understand is that why do we need to apply different values to this question to arrive at the answer (a) while the question has already given the specific values to apply in the program? If I just applied the values given in the question, I would only get the correct answer as (c).
Please explain.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Originally posted by Paul Anil:
Hi Sanjeev,
Can you please quote the questionid of the question? I remember discussing this sometime back and option c was deemed to cause unnecessary diversion from the main point. This was later modified.


Well, it is more than just a diversion. Option C is a valid answer. The statement that "Both the objects will be stored in the HashMap" is true. And having an equals() and hashcode() methods that doesn't fulfill the contract, doesn't change that.

Henry


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

Joined: Jan 10, 2007
Posts: 122
Originally posted by Shiva Mohan:
What I don't understand is that why do we need to apply different values to this question to arrive at the answer (a) while the question has already given the specific values to apply in the program? If I just applied the values given in the question, I would only get the correct answer as (c).
Please explain.

Well, if the implementation of hashcode() and equals() is fulfilling the contract or is not fulfilling the contract has nothing to do with special objects of the class. The contract must be fulfilled for "ALL" possible objects of the class.
So it is ok to search for 2 possible objects of the class where the contract is NOT fulfilled to prove that the implementation is NOT correct.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Good points Anton and Henry. Thanks.

so for this problem the following any one of the option is valid.
a This is an invalid implementation of hashCode() method with respect to the given equals() method.
b.if there is an option telling this is an invalid implementation of the equlas() method.

both are correct.Is my understanding correct?

Hi Paul,
The question Id of this question is 1043963785218. Please verify and answer here.

[ February 11, 2007: Message edited by: Shiva Mohan ]

[ February 11, 2007: Message edited by: Shiva Mohan ]
[ February 11, 2007: Message edited by: Shiva Mohan ]
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3201
    
    2
This question was in the old question bank and was updated in the new (current) version with a new question id ( com.enthuware.ets.scjp.v5.2.711 ). Option c was changed to:

Both the objects will be stored in the HashMap however retrieving them using the Info objects as keys will not be possible.


This is marked incorrect with the following explanation for this option:

In this particular case, since the keys (i.e. the two Info objects) are unequal as per their equals() method, and their hashCodes are also different, the HashMap will work properly. Both the objects will be stored and retrieved. However, it will fail when Info objects that break the rule of equals() and hashCode() are put in the map.


HTH,
Paul.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode problem
 
Similar Threads
equals, hashCode Q
Should we expect such questions?
HashMap Question
question on hascode()
please explain the output?