Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

HashMap : How multiple values/objects has associate with one key

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I am not understands that how multiple values or objects can associate with one key in HashMap.

I have tried below example,



Output :

Map : {emp1=Employee [id=1, name=Anand, city=Pune]}
Map : {emp1=Employee [id=1, name=KKC, city=Pune]}

Can you please explain me the output and how hascode and equals has worked here, as if I comment the overridden hascode and equals methods still the output is the same so confused in hashcode equals contract.

Thank you in advance.
 
Saloon Keeper
Posts: 7616
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes you think these two objects have the same key? Note that 'id' is not the key the way it has been written.
 
Saloon Keeper
Posts: 23006
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I read this correctly?" It looks like you only put one entry into the Map, but pulled and printed it twice.
 
Anand Surya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:What makes you think these two objects have the same key? Note that 'id' is not the key the way it has been written.



Hi Carey,

Thanks for your reply. I am not sure but as I have putted with String "emp1" as a key in map.put("emp1", emp1);
And after getting the object from the key "emp1" and I have just changed the Name property of object Employee.
I guess it has to replace the existing objects name from "Anand" To "KKC" but seems like it has been added into the map.

And if key is not the same then how it has inserted second value into the map as I have changed the name of existing object of the map.

Thanks a lot, please correct if I am wrong anywhere
 
Anand Surya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Do I read this correctly?" It looks like you only put one entry into the Map, but pulled and printed it twice.



Hi Tim,

Yes you have read it correctly, when I have tried this I am also bit confused with HashMap working.
I don't know how it has been inserted the value into the hashmap when I have called setName property.

Thanks
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct in that you have changed the Name of the existing object in the map.  You have not added a second object in the map; when you print out the Map, there is still only one object in there.  What happens if you put in another object with the key "emp2"?  Try that and see what happens when you print out the map; you should see one object associated with emp1 and one object associated with emp2.

Perhaps you are confused by the fact that when you get the object from the map, it does not create a copy of the object.  You have a reference to the object locally (through the emp1 variable) and one in the map.  Since both references refer to the same object, any change to the object is visible by all the references, so when you print out the map it is printing out the changed value.
 
Anand Surya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joel thanks for the reply.

As you suggested I have tried with below code


And getting this output

Map : {emp1=Employee [id=1, name=Anand, city=Pune]}
Map : {emp2=Employee [id=2, name=AKS, city=KOP], emp1=Employee [id=1, name=KKC, city=Pune]}

And as you said "Since both references refer to the same object, any change to the object is visible by all the references, so when you print out the map it is printing out the changed value."
Then why it has stored into the same when I have only get the object and setname to "KKC". Sorry but still I am not getting could you please explain more.

Thanks a lot

 
Anand Surya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel McNary wrote:. . . Perhaps you are confused by the fact . . . .



Now I understood, I have just iterate the map and I got it. Thanks once again.
 
Marshal
Posts: 71682
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't routinely quote the whole of the preceding post, because that adds nothing to the discussion.
 
Popeye has his spinach. I have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic