• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to clone a Hashmap

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • 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: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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: 9000
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since this isn't on the exam, let's move it to 'general'...
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
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
    Bookmark Topic Watch Topic
  • New Topic