• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to clone a Hashmap

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All

I have a requirement to have a backup copy of orginal HashMap so that when some deletion,addition is done on original HashMap I can use a backup Map to track it

My Map structure is like this

Map
|
key-----value (nested Map)
|
key-----value (nested Map)
|
key-----value (List)


I am not able to have a backup copy as whenever something happens to original copy backup copy is also getting affected with same operation

If anyone can please help as I am really stuck


 
author
Posts: 23919
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

Gaurav Chikara wrote:I am not able to have a backup copy as whenever something happens to original copy backup copy is also getting affected with same operation

If anyone can please help as I am really stuck



Well, it depends on what you mean by "something happens". If you mean that the items get deleted or added, then it should work -- meaning that your clone will have the old structure.

If you mean changes to the key or value, then it won't work -- as the clone is only a shallow copy. You will need to copy it (via a loop, and possibly recursively), if you want a deep copy.

Henry
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since this isn't on the exam, let's move it to 'general'...
 
permaculture is largely about replacing oil with people. And one tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic