How do you make a deep copy of an object?

前端 未结 19 2311
执念已碎
执念已碎 2020-11-21 23:09

It\'s a bit difficult to implement a deep object copy function. What steps you take to ensure the original object and the cloned one share no reference?

19条回答
  •  佛祖请我去吃肉
    2020-11-22 00:04

    XStream is really useful in such instances. Here is a simple code to do cloning

    private static final XStream XSTREAM = new XStream();
    ...
    
    Object newObject = XSTREAM.fromXML(XSTREAM.toXML(obj));
    

提交回复
热议问题