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 Hashmap doubt from K&B 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 "Hashmap doubt from K&B" Watch "Hashmap doubt from K&B" New topic
Author

Hashmap doubt from K&B

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
the doubt is from page number 585 of the book....

in the example Dog d1=new Dog("clover"); and using d1 as a key a string is added to the hashmap.....now the book says that if change the d1.name value to mangolia we wont be able to retrieve the string value corresponding to that key...

but why? isnt changing d1.name also changes the d1.name of the key...i mean if we change d1.name to mangolia then the key present inside the hashmap has a name mangolia as d1 refers to that object key....

we are using the same d1 reference variable to put inside the hashmap and the same d1 to get out of the hashmap

so shouldnt the hashmap and equals of both be the same?

scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
hii
raju
first you will have a look at page 551 exam watch
and now for this case when we change the dogs name to magnolia we get a new hashcode value as hashcode value is the length of dogname
so if you cant match the hashocde than you will get a different bucket to search the dog
and as there is no dog with that name in that bucket so the equals method will fail and return a null
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
but since we have changed the name to mangolia we have changed the name of the string in the object key too....since both are referring to the same object
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
i mean isnt the key object inside the Hashmap and the outside which we are using to access the key are the same...isnt d1 referring to the same object that are inside the map and outside which we are using to get the key?
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
look first we match the hashcode to match buckets and then we use equals to match the right object by its content
so even if the objects are same (inside and outside ) you wont be able to get answer because there hashcodes doesnt match

so this shows that we wont find the object key(even if it is same) if we dont look at right place (the right place is the right bucket which depends on the hashcode )

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
how can we have different objects inside and outside? as in same variable refers to both....so using d1.name should change both the objets but still the objects are the same....???
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521


now why is the output the same for both of them?
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
well at the time of putting the key in the map the hashcode was 6 because its dog("clover")
but when we changed the name to magnolia hashcode changed its now 8
so when we use get method to retrieve the value it matches it with the value we have given at the time of insertion that is 6 but now we have changed the value to 8 so this fails
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
as for the code you have written it is same because the name length in both the cases that is arthur and clover the length of string is same that is the length of clover and arthur is 6 because both have six letters and thats what you have selected as your hashcode
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
hey both the hashcodes are same but so what? isnt the equals method going to be called now? arthur and clover are not the same?
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
yes the equals method output is raju and null as from my compilation
so thats correct
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
am still not clear.....arthur clover have the same hashocdes but diferent equals....and arhtur1 and clover have different hashcodes and equals.....so both the outhout should be null............
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
arthur = clover(both have 6 letters so hashcode runs but equals fail)
arhtur1 = clover(one has 7 letters(arhtur1 )and other has 6 letters (clover) so hashcode fails)
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521


just explain this....and that will be it...
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
thats pretty simple in last 2 lines of code you are
d.name = "arthur"; // #Q2
System.out.println(m.get(new Dog("arthur")));//this doesnt...


you are passing the same name "arthur" in d.name and in argument as well so both the hashcode and equals will give output
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
then what about d.name = "arthur1"; // #Q2
System.out.println(m.get(new Dog("arthur1")));
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
so i am going to sleep now
hope your doubts are cleared as of now
if not leave the questions i will answer them tomorrow
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
hey dont go..just answer the last one please..imean
d.name = "arthur1"; // #Q2
System.out.println(m.get(new Dog("arthur1")));// then why this gives me null
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
thats pretty simple in last 2 lines of code you are
d.name = "arthur"; // #Q2
System.out.println(m.get(new Dog("arthur")));//this doesnt...


you are passing the same name "arthur" in d.name and in argument as well so both the hashcode and equals will give output

well you edited your question while i was answering it

and the reason still lies in hashcode arthur has a hashcode of six as clover but when you use arthur1 the hashcode is 7 so the hashcode check fails so no equals check.


well correct me if i am wrong
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
how can i correct you when am still confused.....what i have understood so far is that when we try to get...the program calculates the hashcode of the object...if found it calcualtes the equals...and if equals then prints the output....when the hashcode isnt found in the hashmap it simply gives null...
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
i havent written correct me to you
i have written it for them who could give a better opinion

and you got the right thing finally, just go through k & B once again and all will be fine
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
ohk...thanks Mahesh.....thanks a ton...goodnight for now
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
this is the answer for future aspirants...

when we put the key and its cooresponding value in the hashmap. the hashcode of the key is calculated and a new bucket is created with value 6 and the object(key) is put into it....so when you use a object to get..the program calculates the hashcode of this object...if the hashcode is found(the bucket is found) then it looks in this bucket if the equal object is found....but if the hashcodes are same but equals give false it will give false

now suppose you change the name to mangolia and use this to get....the hashcode is 8 for which there is not bucket is found....so no equals is calculated and output is null

but if you change the d.name to arthur...the object in the hashmap gets changes to Dog("arthur") and it will still be staying the same bucket.....and when you use new dog("clover") to get...the hashcode will be right..bucket will be found...but both will not be equals..that is false will be returned...and thus output will be null

i owe this explanation to Mahesh and all the ranchers who had already discussed this question before in their own time......
 
GeeCON Prague 2014
 
subject: Hashmap doubt from K&B