• 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

Coping a HashMap

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit confused about how to copy a HashMap. From what I read, I should be doing this:

However, that does not seem to really make a new copy. When I change 'ramps' it also makes the same change in 'savedRamps'. It appears it is copying just the reference rather than the class. I also found something about 'HashMap.clone()' but that does not seem to be in any of my libraries. Is that a 3rd party library? How do I make an independent copy of a HashMap other than a brute force 'for' loop? TIA.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After more reading, I think I understand at least part of the problem. I need a deep copy/clone in order preserve the 'RampRecords' themselves. Am I on the right track?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What changes are you doing to the ramps map?

If you are just changing some data in one of the RampRecord objects, then both maps are referring to the same ones.
If you are adding or removing an entry, then they should not be reflected.

See:

which gives:
map1 =  {two=2, one=1}
map2 =  {one=1}
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I indeed need to copy the RampRecords as well because they may change in the original. I have reached the conclusion that I need to write my own method to properly deep copy the HashMap.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This HashMap constructor says it creates a new Map with the same mappings. The clone() method creates a shallow clone (and its return type is Object). You cannot expect a Map or Collection to have a method which creates a deep clone because you cannot predict whether the “K”s and “V”s are mutable or not, and if mutable, how they should be copied. Dave Tolls is right about the difference between changing the Map and changing the state of one of its “V”s.
 
Wanna see my flashlight? How about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic