Java: recommended solution for deep cloning/copying an instance

后端 未结 9 2257
半阙折子戏
半阙折子戏 2020-11-22 01:28

I\'m wondering if there is a recommended way of doing deep clone/copy of instance in java.

I have 3 solutions in mind, but I can have miss some, and I\'d like to ha

相关标签:
9条回答
  • 2020-11-22 01:58

    For complicated objects and when performance is not significant i use gson to serialize the object to json text, then deserialize the text to get new object.

    gson which based on reflection will works in most cases, except that transient fields will not be copied and objects with circular reference with cause StackOverflowError.

    public static <ObjectType> ObjectType Copy(ObjectType AnObject, Class<ObjectType> ClassInfo)
    {
        Gson gson = new GsonBuilder().create();
        String text = gson.toJson(AnObject);
        ObjectType newObject = gson.fromJson(text, ClassInfo);
        return newObject;
    }
    public static void main(String[] args)
    {
        MyObject anObject ...
        MyObject copyObject = Copy(o, MyObject.class);
    
    }
    
    0 讨论(0)
  • 2020-11-22 01:59

    I'd suggest to override Object.clone(), call super.clone() first and than call ref = ref.clone() on all references that you want to have deep copied. It's more or less Do it yourself approach but needs a bit less coding.

    0 讨论(0)
  • 2020-11-22 02:00

    Joshua Bloch's book has a whole chapter entitled "Item 10: Override Clone Judiciously" in which he goes into why overriding clone for the most part is a bad idea because the Java spec for it creates many problems.

    He provides a few alternatives:

    • Use a factory pattern in place of a constructor:

           public static Yum newInstance(Yum yum);
      
    • Use a copy constructor:

           public Yum(Yum yum);
      

    All of the collection classes in Java support the copy constructor (e.g. new ArrayList(l);)

    0 讨论(0)
  • 2020-11-22 02:00

    Use XStream toXML/fromXML in memory. Extremely fast and has been around for a long time and is going strong. Objects don't need to be Serializable and you don't have use reflection (although XStream does). XStream can discern variables that point to the same object and not accidentally make two full copies of the instance. A lot of details like that have been hammered out over the years. I've used it for a number of years and it is a go to. It's about as easy to use as you can imagine.

    new XStream().toXML(myObj)
    

    or

    new XStream().fromXML(myXML)
    

    To clone,

    new XStream().fromXML(new XStream().toXML(myObj))
    

    More succinctly:

    XStream x = new XStream();
    Object myClone = x.fromXML(x.toXML(myObj));
    
    0 讨论(0)
  • 2020-11-22 02:01

    Depends.

    For speed, use DIY. For bulletproof, use reflection.

    BTW, serialization is not the same as refl, as some objects may provide overridden serialization methods (readObject/writeObject) and they can be buggy

    0 讨论(0)
  • 2020-11-22 02:02

    For deep cloning implement Serializable on every class you want to clone like this

    public static class Obj implements Serializable {
        public int a, b;
        public Obj(int a, int b) {
            this.a = a;
            this.b = b;
        }
    }
    

    And then use this function:

    public static Object deepClone(Object object) {
        try {
            ByteArrayOutputStream baOs = new ByteArrayOutputStream();
            ObjectOutputStream oOs = new ObjectOutputStream(baOs);
            oOs.writeObject(object);
            ByteArrayInputStream baIs = new ByteArrayInputStream(baOs.toByteArray());
            ObjectInputStream oIs = new ObjectInputStream(baIs);
            return oIs.readObject();
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    

    like this: Obj newObject = (Obj)deepClone(oldObject);

    0 讨论(0)
提交回复
热议问题