Copy and Copy shallow depth

Deep copy and shallow copy main difference is that there is no copy of the new object to reallocate a memory area in the heap.

Shallow copy: direct assignment, copy only the original object reference address, a memory heap is still common.

Deep copy:  a new object on the heap to re-allocate a block of memory, so the operation of the new object will not affect the original object.

Several methods deep copy

1. Use the Java serialization method #

Want deep copy of an object, commonly used method is serialized as a data stream, the premise of this approach is that the object and the child objects contained in the object to be inherited Serializable interface.

/**
     * 深层拷贝 - 需要类继承序列化接口
     * @param <T>
     * @param obj
     * @return
     * @throws Exception
     */
    public static <T> T copyImplSerializable(T obj) throws Exception {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;

        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;

        Object o = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            bais = new ByteArrayInputStream(baos.toByteArray());
            ois = new ObjectInputStream(bais);

            o = ois.readObject();
            return (T) o;
        } catch (Exception e) {
            throw new Exception("对象中包含没有继承序列化的对象");
        } finally{
            try {
                baos.close();
                oos.close();
                bais.close();
                ois.close();
            } catch (Exception e2) {
                
            }
        }
    }

2. Use Kryo framework sequence #

Kryo is a fast and efficient Java serialization framework is designed to provide fast, efficient and easy to use API. Regardless of the file, database or network data Kryo can be done at any time serialization. Kryo deep copy may also be performed automatically (clone), shallow copy (clone). This is a direct copy of the object to the object, the non-target -> bytes -> copy of the object. This method does not require inheritance Serializable interface.

<dependency> 
<groupId>com.esotericsoftware</groupId> 
<artifactId>kryo</artifactId>
<version>4.x.x</version> 
</dependency>
 public static T copyByKryo(T source){
        Kryo kryo = new Kryo();
        return kryo.copy(source);
    }

3. The method of using a transformed Json

If the object is not inherited Serializable interface, it can first be converted to the JSON an object, then the object into a sequence, similar to the first method. Jackson Json conversion tools can be used or Json-lib, herein Json-lib select only need to add the following dependency maven in which:

<dependency> 
<groupId>net.sf.json-lib</groupId> 
<artifactId>json-lib</artifactId> 
<version>x.x</version> 
<classifier>jdk15</classifier>
</dependency>
 public static <T> T copyByJson(T obj) throws Exception {
        return (T)JSONObject.toBean(JSONObject.fromObject(obj),obj.getClass());
    }

New object 4. Manually

Manual new object attribute values ​​one by calling the set method, more complicated.

Reference:  https://www.cnblogs.com/coderzhw/p/11094284.html#3757894395

 

发布了39 篇原创文章 · 获赞 1 · 访问量 8779

Guess you like

Origin blog.csdn.net/oDengTao/article/details/104024750