• 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

Deep copy of complicated object

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

Assume that you have an object A.

It contains many objects.

Some of these objects are ArrayLists of another objects

Soem of those objects from the ArrayLists are also ArrayLists.

Etc.


In short: you have an objects containing objects which may be ArrayLists that contains object which may be ArrayLists etc.


How would you make a deep copy B of such object A?

A deep copy = no common references between the contains of the object A and the contains of the object B which is a copy of object A.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One trick you can use is to serialize the object tree, then reload it to create the copy.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
One trick you can use is to serialize the object tree, then reload it to create the copy.



I think you will find more details in this article. Beware of serializing anything with passwords in, or singletons.
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.

Isn't it that we can use this solution only if all objects in the object tree are serializable?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ismael Upright:
Thanks for your replies.

Isn't it that we can use this solution only if all objects in the object tree are serializable?



Yep.

There are other remaining alternatives, of course, which all boil down to explicitly writing code to walk through the tree of objects and copy everything. You can do this either by overriding clone() -- and thereby making use of clone() to do some of the work -- or by ignoring clone(), and writing your own code.
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's also possible to use reflection to systematically copy data from all fields of an object - even if the object is not serializable. I think there are several tools that do this already - the one I know best is XStream. You can use this to convert almost any object to a simple String representation (using XML, JSON, or other formats), then read that String and convert it to a new instance. While there are some classes it can't convert, it works pretty easily for most of them.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
walk through the tree of objects

Also bear in mind that it's possible you don't have a tree of objects. In general you have a directed graph of objects. It's possible that two objects in this complicated thing both contain references to a third object. In that case you might well want the copies of those two objects to contain references to a single copy of the third object.

Serialization takes care of this issue.
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic