• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hashmap doubt from K&B

 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


now why is the output the same for both of them?
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes the equals method output is raju and null as from my compilation
so thats correct
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


just explain this....and that will be it...
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then what about d.name = "arthur1"; // #Q2
System.out.println(m.get(new Dog("arthur1")));
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohk...thanks Mahesh.....thanks a ton...goodnight for now
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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......
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic