This article describes the Java deep replication function and usage. Share it for your reference, as follows:
Written before:
What is deep replication? In Java, when creating an object, we usually have a reference pointing to the object. When we change the value (properties) of the object by referring to the variable, the reference remains unchanged, and it changes the memory in the memory, that is, the object pointed to. Generally speaking, when we assign this reference to another reference variable or pass it as a parameter, we only pass it, that is, we point the reference to "copy" a copy to another reference variable, and then the reference variable also points to the same object, and no new object is created in memory. In some cases, we need to "really copy" the object, create a copy of the known object, not just "copy" the reference, whether it is a backup or other operations.
So, how to achieve it?
Let’s talk about the idea first: first serialize the object into the stream, then deserialize it, and read it out from the stream.
The following code:
package com.yo.java;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.List;/** * Java implements deep copy* @author Yo * */public class DeepCopy implements Serializable{ int i; /** * @param args * @throws IOException * @throws ClassNotFoundException */ public static void main(String[] args) throws ClassNotFoundException, IOException { demo1(); demo2(); } /** * Deep copy, the actual parameter class must implement the Serializable interface* @param o * @return * @throws IOException * @throws ClassNotFoundException */ public static Object deepCopy(Object o) throws IOException, ClassNotFoundException {// //Serialize first and write to the stream ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(o); //Then deserialize and read from the stream, that is, copying ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray()); ObjectInputStream oi = new ObjectInputStream(bi); return oi.readObject(); } /** * Reference passing and deep copy* @throws ClassNotFoundException * @throws IOException */ public static void demo1() throws ClassNotFoundException, IOException { System.out.println("===================================================================================================================================================================================================================================================================================================================================================================================================================================== 1;//Initialize the value of i in dc1 DeepCopy dc2 = dc1; dc1.i = 2;//Change the value of i in dc1 System.out.println("dc1 : " + dc1.i); System.out.println("dc2(reference pass) : " + dc2.i); System.out.println("================================================================================================================================================================================================================================================================================================================================================================================================================== (DeepCopy)deepCopy(dc3); dc3.i = 2;//Change the value of i in dc3 System.out.println("dc3 : " + dc3.i); System.out.println("dc4(deep copy) : " + dc4.i); } /** * Value copy and deep copy of the set* @throws ClassNotFoundException * @throws IOException */ public static void demo2() throws ClassNotFoundException, IOException { System.out.println("================================================================= System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. System. Out of System. System. System. System. Out of System. System. System. System. Out of System. System. System. System. Out of System. System. System. System. Out of System. System. System. System. System. Out of System. System. System. System. System. System. Out of System. System. System. System. System. System. Out of System. System. System. System. System. System. System. System. Out of System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System src) The result is the same //Change the value of the element in list1 for(DeepCopy d1 : list1) { //Change the value of i in dc1 d1.i = 2; } //Transip list for(DeepCopy d1 : list1) { System.out.println("list1 : " + d1.i); } //Transip list2 for(DeepCopy d2 : list2) { System.out.println("list2(Copy) : " + d2.i); } System.out.println("============================================================================================================================================================================================================================================================================================================================================================ new DeepCopy(); dc3.i = 1;//Initialize the value of i in dc3 List<DeepCopy> list3 = new ArrayList<DeepCopy>(); list3.add(dc3); List<DeepCopy> list4 = (List<DeepCopy>) deepCopy(list3); for(DeepCopy d: list3) { //Change the value of i in dc3 di = 2; } for(DeepCopy d3: list3) { System.out.println("list3: " + d3.i); } for(DeepCopy d4: list4) { System.out.println("list4(deep copy): " + d4.i); } }}The above operation results are as follows (actually measured):
========================================================================================================================================================================================================================================================================================================================================================================================================================================================================= : 2list4 (deep copy): 1
It can be seen that when just referring to pass or creating a new value based on the value of the object, it can only be called "shallow copy". When the properties of the original object change, the properties of the new object created according to the above method will also change accordingly; and if deep copy is used, it is true that a new object is copied. The new object has no relationship with the original object. The change of the properties of the original object will not affect the new object, just like the meaning of copy.
As mentioned above, if there is any inappropriateness, if you can point it out, thank you very much
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.