Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

hashCode problem

 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest,
I got the point that it is violating the hashcode() and equal() contract.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Enthuware Software Support
Posts: 4468
43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4468
43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic