Using serialization and deserialization to perform deep replication on Java objects

Source: Internet
Author: User

Source http://lhg803.iteye.com/blog/407270

The Java. Lang. Object and cloneable interfaces in JDK APIs must be modified according to the specific class to implement in-depth clone.Code.
Serialization and deserialization can be used for deep replication independently of a specific class.

When using this method for deep copy, all objects must be implements serializable. Otherwise, a java. Io. notserializableexception exception is reported.

Instance:

Student

 Package Cup1;

Import Java. Io. serializable;

Public Class Student Implements Serializable {
Private String name;
Private String Tel;
Private Int Age;


Public String getname (){
Return Name;
}
Public Void Setname (string name ){
This . Name = Name;
}
Public String gettel (){
Return Tel;
}
Public Void Settel (string Tel ){
This . Tel = Tel;
}
Public Int Getage (){
Return Age;
}
Public Void Setage ( Int Age ){
This . Age = age;
}

}

Student class, including a list of student objects

 Package Cup1;

Import Java. Io. serializable;
Import Java. util. List;

Public Class Teacher Implements Serializable {
Private String name;
Private Int Age;
Private List <student> stulist;

Public String getname (){
Return Name;
}
Public Void Setname (string name ){
This . Name = Name;
}
Public Int Getage (){
Return Age;
}
Public Void Setage ( Int Age ){
This . Age = age;
}
Public List <student> getstulist (){
Return Stulist;
}
Public Void Setstulist (list <student> stulist ){
This . Stulist = stulist;
}

}

Test class, including the main method and the deepclone object deep clone method

 Package Cup1;

Import Java. Io. bytearrayinputstream;
Import Java. Io. bytearrayoutputstream;
Import Java. Io. ioexception;
Import Java. Io. objectinputstream;
Import Java. Io. objectoutputstream;
Import Java. util. arraylist;
Import Java. util. List;

Public Class Test {

Public Static Void Main (string [] ARGs ){
Student S1 = New Student ();
S1.setname ("Wang ");
S1.setage (25 );
S1.settel ("110 ");
Student S2 = New Student ();
S2.setname ("Li ");
S2.setage (35 );
Ss2.settel ("119 ");

Teacher T = New Teacher ();
T. setname ("Zhang ");
T. setage (50 );
List <student> stulist = New Arraylist <student> ();
Stulist. Add (S1 );
Stulist. Add (S2 );
T. setstulist (stulist );
System. Out. println ("------- original -------");
System. Out. println ("T:" + t );
System. Out. println ("S1:" + S1 );
System. Out. println ("S2:" + S2 );
System. Out. println ("------- after clone -------");
Teacher t2 = (teacher) deepclone (t );
System. Out. println ("T2:" + T2 );
System. Out. println ("T:" + t );

Student S3 = t2.getstulist (). Get (0 );
Student S4 = t2.getstulist (). Get (1 );
System. Out. println ("S3:" + S3 );
System. Out. println ("S1:" + S1 );
System. Out. println ("S4:" + S4 );
System. Out. println ("S2:" + S2 );

System. Out. println ("------- after T2 change -------");
T2.setage (55 );
T2.setname ("Zhao ");

System. Out. println ("T2 name:" + t2.getname () + "T2 age" + t2.getage ());
System. Out. println ("t name:" + T. getname () + "t age" + T. getage ());

}

Public Static Object deepclone (Object SRC ){
Object DST = Null ;

Try {
Bytearrayoutputstream out = New Bytearrayoutputstream ();
Objectoutputstream oo = New Objectoutputstream (out );
OO. writeobject (SRC );

Bytearrayinputstream in = New Bytearrayinputstream (
Out. tobytearray ());
Objectinputstream OI = New Objectinputstream (in );
DST = Oi. readobject ();
} Catch (Ioexception e ){
E. printstacktrace ();
}Catch (Classnotfoundexception e ){
E. printstacktrace ();
}

Return DST;
}
}

Output result

 
------- Original -------
T: cup1.teacher @ 1fb8ee3
S1: cup1.student @ 61de33
S2: cup1.student @ 14318bb
------- After clone -------
T2: cup1.teacher @ 12ac982
T: cup1.teacher @ 1fb8ee3
S3: cup1.student @ 1389e4
S1: cup1.student @ 61de33
S4: cup1.student @ c20e24
S2: cup1.student @ 14318bb
------- After T2 change -------
T2 name: Zhao T2 age55
T name: Zhang t age50

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.