• 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

MAP

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have a look at this code




the output for this program i got it as



Dog key
null

null

my doubt--- here i tried to access the value of the key new cat(); but i got null as the answer thus when i tried to figure out the reason for it i found that the equal() and hashcode() methods are not overriden by the Dog class and not Cat class that is why we got null(need your clarification on this),? is it right? also when i tried to access the value using Dog Object key at that time to i got null as the out put !! please clarfyh me whats happening between the codes and equal and hashcode() methods? i know what does equal and hash code means but please explain how it works for this code?
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



working with this code i found that when i change the d2 object as abcdef whose hassh code value matches with hello which was declared earlier! but the equal method doesnt matches!!isnt it? even though equal doesnt matches i can able to retrive the value which is assingned for the d2 object key!! thus it si contradicting the rule? contract between hashcode and equal();!!! so can any one please help me with this!!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you properly indent your code? It would greatly help readablity. Having code tags, of code with no idents is only slightly better than having no code tags.

Henry
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you may fid this to the point!



working with this code i found that when i change the d2 object as abcdef whose hassh code value matches with hello which was declared earlier! but the equal method doesnt matches!!isnt it? even though equal doesnt matches i can able to retrive the value which is assingned for the d2 object key!! thus it si contradicting the rule? contract between hashcode and equal();!!! so can any one please help me with this!!!
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

working with this code i found that when i change the d2 object as abcdef whose hassh code value matches with hello which was declared earlier! but the equal method doesnt matches!!isnt it? even though equal doesnt matches i can able to retrive the value which is assingned for the d2 object key!!



First of all, you got lucky. You are *not* allowed to change the value of a key that is in a map. Doing so can corrupt the map. In this case, it worked because the hashcode didn't change, and you didn't change it to something that would have cause the duplicate rule to the violated.... In most cases, when you change the key, it will corrupt the map, and there is no guarrantee that anything will work as documented.

And why won't equals() return true? The map doesn't clone the keys, it just has a reference to them, so the map is pointing to the same object as what the d2 reference is pointing to.


thus it si contradicting the rule? contract between hashcode and equal();!!! so can any one please help me with this!!!



Which rule are you referring to?

Henry
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i understood your above explanation !! thanks!!

i have one more doubt abt this code




in this code when used the dog object and cat object key to refer to two distinct values i found that while retriving it back using get i mentioned the two object keys but , i got the output as

Dog key
null

So only for dog object i could get the value then what about the cat object key dint it refer to anything!!! why i got null value here?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So only for dog object i could get the value then what about the cat object key dint it refer to anything!!! why i got null value here?



Why do you think it is? ... Hint: The hashmap class uses the hashCode() method to determine the bucket to place the data, and the equals() method to determine equality.

Henry
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but henry,

when i read it in k & b book!! i got that , since the cat class does not overrride the equal nad hash code method , thats why it does not return the value stored with cat object as reference!! i am not clear about this point!! can you get me???
 
reply
    Bookmark Topic Watch Topic
  • New Topic