• 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

Doubt in Collection

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

I have doubt in the following code:


The output comes as:
null
Dog key
null

I am not able to understand why the line 6 is giving o/p as null.
My understanding for above code is:

This thing I understood that when d1.name is set to "magnolia"in line 1, then line 2 prints null beacuse the d1 object saved earlier in the map has name as "clover" . So the hashcode will be 8 but earlier it was 6.

But when line 5 changes d1.name as "arthur", but line 6 is printing the
dog object whose name is "clover" which was saved into the map, so the hashcode() will be 6 and equals should also satisfy, then why it is not returning "Dog key" instead of null.
I think I am not getting the logic of line 1 - line 6. Can anyone clarify my doubt?

Regards,
Lata
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, Let me explain you..Think hash map is a Guardian with two guards..!!
When you put an "Dog Key" in Map..you tell the first Guard(HashMap)..Allow any Object to enter whose Hash value is 6...!!
you tell second Guard(equals) that give a copy of "Dog key" to any DOG whose name is "clover"...!! I hoep till now everything will be clear to you..!!

After that you ask the second guard..I dont like "clover"..so allow everyone whose name is "magnolia"
Now First you send DOG "magnolia"..It goes to first guard..but as its Hash value is 8..so guard doesnt allow the DOG..!! Because it require 6 for authentication..!!

After that you ask the second guard..I dont like "magnolia"..so allow everyone whose name is "clover"..

Now you send DOG "clover"..it has value 6..so first guard allows it..again it has clover name so second guard also..so it bring with "DOG KEY"..in its mouth..!!

After that you ask the second guard..I dont like "clover"..so allow everyone whose name is "arthur"..!!
Now you send DOG "clover"..it has value 6..so first guard allows it..but second Guard wants "arthur"..so it doesn't allow it..hence result is null
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lata Bagga:
... But when line 5 changes d1.name as "arthur", but line 6 is printing the dog object whose name is "clover" which was saved into the map, so the hashcode() will be 6 and equals should also satisfy...


Take a closer look at "equals should also satisfy."
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic