shallow copy 和 deep copy 的示例

时间:2023-03-08 16:42:13

本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5481874.html   (Robin)

Student

 package base;

 import java.io.Serializable;

 /**
* Created by robin on 2016/5/11.
*
* @author robin
*/
public class Student implements Cloneable,Serializable{ private String stuId; private String name; private Integer age; public Student(String stuId, String name, Integer age) {
this.stuId = stuId;
this.name = name;
this.age = age;
} public Student(String stuId, String name) {
this.stuId = stuId;
this.name = name;
} public String getStuId() {
return stuId;
} public String getName() {
return name;
} public Integer getAge() {
return age;
} public void setStuId(String stuId) {
this.stuId = stuId;
} public void setName(String name) {
this.name = name;
} public void setAge(Integer age) {
this.age = age;
}
/**
* @return 创建并返回此对象的一个副本。
* @throws CloneNotSupportedException
*/
public Student clone() throws CloneNotSupportedException {
//直接调用父类的clone()方法,返回克隆副本
return (Student) super.clone();
} @Override
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("[stuId:");
sb.append(this.stuId);
sb.append(",name:");
sb.append(this.name);
sb.append(",age:");
sb.append(this.age);
sb.append("]");
return sb.toString();
} /**
* JAVA当中所有的类都是继承于Object这个基类的,在Object中的基类中定义了一个equals的方法,这个方法的初始行为是比较对象的内存地址。
* 但在一些类库当中这个方法被覆盖掉了,如String,Integer,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了。
* 注意:对于复合数据类型之间进行equals比较,在没有覆写equals方法的情况下,他们之间的比较还是基于他们在内存中的存放位置的地址值。
* @param student
* @return
*/
public boolean equals(Student student){
if(this.stuId.equals(student.getStuId()) &&
this.age.equals(student.getAge()) &&
this.name.equals(student.getName())){
return true;
}
return false;
}
}

深度拷贝和浅层拷贝的区别代码示例

 package base;

 import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector; /**
* Created by robin on 2016/5/11.
*
* @author robin
*/
public class ShallowAndDeepCopy { private static List<Student> noumenon = new Vector<Student>(); private static void initPublicNums(){
for (int i =0;i < 5;i++){
Student newStu = new Student(""+i,"name:"+i,i);
noumenon.add(newStu);
}
} public static void main(String args[]) throws CloneNotSupportedException {
initPublicNums();
List<Student> quote = noumenon;
List<Student> shallowCopy = getPublicNumsShallow();
List<Student> deepCopy = getPublicNumsDeep();
List<Student> deepCommonCopy = deepCopy(noumenon);
System.out.println("quote List的内存地址是否相同:"+(noumenon == quote));
System.out.println("shallow copy List的内存地址是否相同:"+(noumenon == shallowCopy));
System.out.println("deep copy List的内存地址是否相同:" + (noumenon == deepCopy));
System.out.println("deepCommon copy List的内存地址是否相同:" + (noumenon == deepCommonCopy));
System.out.println("------------------shallow copy test-----------------------");
for (int i=0;i<shallowCopy.size();i++){
Student shallowStu = shallowCopy.get(i);
Student noumenonStu = noumenon.get(i);
System.out.println("{shallowStu detail:" + shallowStu.toString()+"}");
System.out.println("{noumenonStu detail:" + noumenonStu.toString()+"}");
System.out.println("{shallowStu equals noumenonStu:" + shallowStu.equals(noumenonStu)+"}");
System.out.println("{shallowCopy("+i+")与noumenon("+i+")内存地址相同:"+(noumenonStu == shallowStu)+"}");
System.out.println();
}
System.out.println("------------------deep copy test-----------------------");
for (int i=0;i<deepCopy.size();i++){
Student deepStu = deepCopy.get(i);
Student noumenonStu = ShallowAndDeepCopy.noumenon.get(i);
System.out.println("{deepStu detail:" + deepStu.toString() + "}");
System.out.println("{noumenonStu detail:" + noumenonStu.toString() + "}");
System.out.println("{deepStu equals noumenonStu:" + deepStu.equals(noumenonStu) + "}");
System.out.println("{deepCopy("+i+")与noumenon("+i+")内存地址相同:"+(noumenonStu == deepStu)+"}");
System.out.println();
}
System.out.println("------------------deepCommonCopy copy test-----------------------");
for (int i=0;i<deepCommonCopy.size();i++){
Student deepCommonStu = deepCommonCopy.get(i);
Student noumenonStu = ShallowAndDeepCopy.noumenon.get(i);
System.out.println("{deepCommonStu detail:" + deepCommonStu.toString() + "}");
System.out.println("{noumenonStu detail:" + noumenonStu.toString() + "}");
System.out.println("{deepCommonStu equals noumenonStu:" + deepCommonStu.equals(noumenonStu) + "}");
System.out.println("{deepCommonCopy("+i+")与noumenon("+i+")内存地址相同:"+(noumenonStu == deepCommonStu)+"}");
System.out.println();
}
} /**
* shallow copy:list中 元素引用 仍然是相同的
* @return
*/
private static List<Student> getPublicNumsShallow(){
List<Student> clone = new ArrayList<Student>(noumenon);
return clone;
} /**
* deep copy:list中 引用元素 地址是不同的
* @return
* @throws CloneNotSupportedException
*/
private static List<Student> getPublicNumsDeep() throws CloneNotSupportedException {
List<Student> clone = new ArrayList<Student>();
for (int i=0;i< noumenon.size();i++){
clone.add(noumenon.get(i).clone());
}
return clone;
} /**
* 深度复制 这种实现的深度复制,是不需要被复制的元素实现clone方法的 但JDK版本要求:1.7及以上
* @param src 这个是同事完成 非本人开发
* @return
*/
private static List deepCopy(List src) {
List dest = null;
ByteArrayInputStream byteIn = null;
ObjectInputStream in = null;
try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut)) {
out.writeObject(src);
byteIn = new ByteArrayInputStream(byteOut.toByteArray());
in = new ObjectInputStream(byteIn);
dest = (List) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (byteIn != null) {
byteIn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return dest;
} }

执行结果:

 quote List的内存地址是否相同:true
shallow copy List的内存地址是否相同:false
deep copy List的内存地址是否相同:false
deepCommon copy List的内存地址是否相同:false
------------------shallow copy test-----------------------
{shallowStu detail:[stuId:0,name:name:0,age:0]}
{noumenonStu detail:[stuId:0,name:name:0,age:0]}
{shallowStu equals noumenonStu:true}
{shallowCopy(0)与noumenon(0)内存地址相同:true} {shallowStu detail:[stuId:1,name:name:1,age:1]}
{noumenonStu detail:[stuId:1,name:name:1,age:1]}
{shallowStu equals noumenonStu:true}
{shallowCopy(1)与noumenon(1)内存地址相同:true} {shallowStu detail:[stuId:2,name:name:2,age:2]}
{noumenonStu detail:[stuId:2,name:name:2,age:2]}
{shallowStu equals noumenonStu:true}
{shallowCopy(2)与noumenon(2)内存地址相同:true} {shallowStu detail:[stuId:3,name:name:3,age:3]}
{noumenonStu detail:[stuId:3,name:name:3,age:3]}
{shallowStu equals noumenonStu:true}
{shallowCopy(3)与noumenon(3)内存地址相同:true} {shallowStu detail:[stuId:4,name:name:4,age:4]}
{noumenonStu detail:[stuId:4,name:name:4,age:4]}
{shallowStu equals noumenonStu:true}
{shallowCopy(4)与noumenon(4)内存地址相同:true} ------------------deep copy test-----------------------
{deepStu detail:[stuId:0,name:name:0,age:0]}
{noumenonStu detail:[stuId:0,name:name:0,age:0]}
{deepStu equals noumenonStu:true}
{deepCopy(0)与noumenon(0)内存地址相同:false} {deepStu detail:[stuId:1,name:name:1,age:1]}
{noumenonStu detail:[stuId:1,name:name:1,age:1]}
{deepStu equals noumenonStu:true}
{deepCopy(1)与noumenon(1)内存地址相同:false} {deepStu detail:[stuId:2,name:name:2,age:2]}
{noumenonStu detail:[stuId:2,name:name:2,age:2]}
{deepStu equals noumenonStu:true}
{deepCopy(2)与noumenon(2)内存地址相同:false} {deepStu detail:[stuId:3,name:name:3,age:3]}
{noumenonStu detail:[stuId:3,name:name:3,age:3]}
{deepStu equals noumenonStu:true}
{deepCopy(3)与noumenon(3)内存地址相同:false} {deepStu detail:[stuId:4,name:name:4,age:4]}
{noumenonStu detail:[stuId:4,name:name:4,age:4]}
{deepStu equals noumenonStu:true}
{deepCopy(4)与noumenon(4)内存地址相同:false} ------------------deepCommonCopy copy test-----------------------
{deepCommonStu detail:[stuId:0,name:name:0,age:0]}
{noumenonStu detail:[stuId:0,name:name:0,age:0]}
{deepCommonStu equals noumenonStu:true}
{deepCommonCopy(0)与noumenon(0)内存地址相同:false} {deepCommonStu detail:[stuId:1,name:name:1,age:1]}
{noumenonStu detail:[stuId:1,name:name:1,age:1]}
{deepCommonStu equals noumenonStu:true}
{deepCommonCopy(1)与noumenon(1)内存地址相同:false} {deepCommonStu detail:[stuId:2,name:name:2,age:2]}
{noumenonStu detail:[stuId:2,name:name:2,age:2]}
{deepCommonStu equals noumenonStu:true}
{deepCommonCopy(2)与noumenon(2)内存地址相同:false} {deepCommonStu detail:[stuId:3,name:name:3,age:3]}
{noumenonStu detail:[stuId:3,name:name:3,age:3]}
{deepCommonStu equals noumenonStu:true}
{deepCommonCopy(3)与noumenon(3)内存地址相同:false} {deepCommonStu detail:[stuId:4,name:name:4,age:4]}
{noumenonStu detail:[stuId:4,name:name:4,age:4]}
{deepCommonStu equals noumenonStu:true}
{deepCommonCopy(4)与noumenon(4)内存地址相同:false}