shallow copy 和 deep copy 的示例

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

Student

 1 package base;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * Created by robin on 2016/5/11.
 7  *
 8  * @author robin
 9  */
10 public class Student implements Cloneable,Serializable{
11 
12     private String stuId;
13 
14     private String name;
15 
16     private Integer age;
17 
18     public Student(String stuId, String name, Integer age) {
19         this.stuId = stuId;
20         this.name = name;
21         this.age = age;
22     }
23 
24     public Student(String stuId, String name) {
25         this.stuId = stuId;
26         this.name = name;
27     }
28 
29     public String getStuId() {
30         return stuId;
31     }
32 
33     public String getName() {
34         return name;
35     }
36 
37     public Integer getAge() {
38         return age;
39     }
40 
41     public void setStuId(String stuId) {
42         this.stuId = stuId;
43     }
44 
45     public void setName(String name) {
46         this.name = name;
47     }
48 
49     public void setAge(Integer age) {
50         this.age = age;
51     }
52     /**
53      * @return 创建并返回此对象的一个副本。
54      * @throws CloneNotSupportedException
55      */
56     public Student clone() throws CloneNotSupportedException {
57         //直接调用父类的clone()方法,返回克隆副本
58         return (Student) super.clone();
59     }
60 
61     @Override
62     public String toString(){
63         StringBuffer sb = new StringBuffer();
64         sb.append("[stuId:");
65         sb.append(this.stuId);
66         sb.append(",name:");
67         sb.append(this.name);
68         sb.append(",age:");
69         sb.append(this.age);
70         sb.append("]");
71         return sb.toString();
72     }
73 
74     /**
75      * JAVA当中所有的类都是继承于Object这个基类的,在Object中的基类中定义了一个equals的方法,这个方法的初始行为是比较对象的内存地址。
76      * 但在一些类库当中这个方法被覆盖掉了,如String,Integer,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了。
77      * 注意:对于复合数据类型之间进行equals比较,在没有覆写equals方法的情况下,他们之间的比较还是基于他们在内存中的存放位置的地址值。
78      * @param student
79      * @return
80      */
81     public boolean equals(Student student){
82         if(this.stuId.equals(student.getStuId()) &&
83                 this.age.equals(student.getAge()) &&
84                 this.name.equals(student.getName())){
85             return true;
86         }
87         return false;
88     }
89 }

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

  1 package base;
  2 
  3 import java.io.*;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 import java.util.Vector;
  7 
  8 /**
  9  * Created by robin on 2016/5/11.
 10  *
 11  * @author robin
 12  */
 13 public class ShallowAndDeepCopy {
 14 
 15     private static List<Student> noumenon = new Vector<Student>();
 16 
 17     private static void initPublicNums(){
 18         for (int i =0;i < 5;i++){
 19             Student newStu = new Student(""+i,"name:"+i,i);
 20             noumenon.add(newStu);
 21         }
 22     }
 23 
 24     public static void main(String args[]) throws CloneNotSupportedException {
 25         initPublicNums();
 26         List<Student> quote = noumenon;
 27         List<Student> shallowCopy = getPublicNumsShallow();
 28         List<Student> deepCopy = getPublicNumsDeep();
 29         List<Student> deepCommonCopy = deepCopy(noumenon);
 30         System.out.println("quote List的内存地址是否相同:"+(noumenon == quote));
 31         System.out.println("shallow copy List的内存地址是否相同:"+(noumenon == shallowCopy));
 32         System.out.println("deep copy List的内存地址是否相同:" + (noumenon == deepCopy));
 33         System.out.println("deepCommon copy List的内存地址是否相同:" + (noumenon == deepCommonCopy));
 34         System.out.println("------------------shallow copy test-----------------------");
 35         for (int i=0;i<shallowCopy.size();i++){
 36             Student shallowStu = shallowCopy.get(i);
 37             Student noumenonStu = noumenon.get(i);
 38             System.out.println("{shallowStu  detail:" + shallowStu.toString()+"}");
 39             System.out.println("{noumenonStu detail:" + noumenonStu.toString()+"}");
 40             System.out.println("{shallowStu equals noumenonStu:" + shallowStu.equals(noumenonStu)+"}");
 41             System.out.println("{shallowCopy("+i+")与noumenon("+i+")内存地址相同:"+(noumenonStu == shallowStu)+"}");
 42             System.out.println();
 43         }
 44         System.out.println("------------------deep copy test-----------------------");
 45         for (int i=0;i<deepCopy.size();i++){
 46             Student deepStu = deepCopy.get(i);
 47             Student noumenonStu = ShallowAndDeepCopy.noumenon.get(i);
 48             System.out.println("{deepStu     detail:" + deepStu.toString() + "}");
 49             System.out.println("{noumenonStu detail:" + noumenonStu.toString() + "}");
 50             System.out.println("{deepStu equals noumenonStu:" + deepStu.equals(noumenonStu) + "}");
 51             System.out.println("{deepCopy("+i+")与noumenon("+i+")内存地址相同:"+(noumenonStu == deepStu)+"}");
 52             System.out.println();
 53         }
 54         System.out.println("------------------deepCommonCopy copy test-----------------------");
 55         for (int i=0;i<deepCommonCopy.size();i++){
 56             Student deepCommonStu = deepCommonCopy.get(i);
 57             Student noumenonStu = ShallowAndDeepCopy.noumenon.get(i);
 58             System.out.println("{deepCommonStu detail:" + deepCommonStu.toString() + "}");
 59             System.out.println("{noumenonStu   detail:" + noumenonStu.toString() + "}");
 60             System.out.println("{deepCommonStu equals noumenonStu:" + deepCommonStu.equals(noumenonStu) + "}");
 61             System.out.println("{deepCommonCopy("+i+")与noumenon("+i+")内存地址相同:"+(noumenonStu == deepCommonStu)+"}");
 62             System.out.println();
 63         }
 64     }
 65 
 66     /**
 67      * shallow copy:list中 元素引用 仍然是相同的
 68      * @return
 69      */
 70     private static List<Student> getPublicNumsShallow(){
 71         List<Student> clone = new ArrayList<Student>(noumenon);
 72         return clone;
 73     }
 74 
 75     /**
 76      * deep copy:list中 引用元素 地址是不同的
 77      * @return
 78      * @throws CloneNotSupportedException
 79      */
 80     private static List<Student> getPublicNumsDeep() throws CloneNotSupportedException {
 81         List<Student> clone = new ArrayList<Student>();
 82         for (int i=0;i< noumenon.size();i++){
 83             clone.add(noumenon.get(i).clone());
 84         }
 85         return clone;
 86     }
 87 
 88     /**
 89      * 深度复制 这种实现的深度复制,是不需要被复制的元素实现clone方法的 但JDK版本要求:1.7及以上
 90      * @param src  这个是同事完成 非本人开发
 91      * @return
 92      */
 93     private static List deepCopy(List src) {
 94         List dest = null;
 95         ByteArrayInputStream byteIn = null;
 96         ObjectInputStream in = null;
 97         try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
 98              ObjectOutputStream out = new ObjectOutputStream(byteOut)) {
 99             out.writeObject(src);
100             byteIn = new ByteArrayInputStream(byteOut.toByteArray());
101             in = new ObjectInputStream(byteIn);
102             dest = (List) in.readObject();
103         } catch (IOException | ClassNotFoundException e) {
104             e.printStackTrace();
105         } finally {
106             try {
107                 if (in != null) {
108                     in.close();
109                 }
110                 if (byteIn != null) {
111                     byteIn.close();
112                 }
113             } catch (IOException e) {
114                 e.printStackTrace();
115             }
116         }
117         return dest;
118     }
119 
120 
121 }

执行结果:

 1 quote List的内存地址是否相同:true
 2 shallow copy List的内存地址是否相同:false
 3 deep copy List的内存地址是否相同:false
 4 deepCommon copy List的内存地址是否相同:false
 5 ------------------shallow copy test-----------------------
 6 {shallowStu  detail:[stuId:0,name:name:0,age:0]}
 7 {noumenonStu detail:[stuId:0,name:name:0,age:0]}
 8 {shallowStu equals noumenonStu:true}
 9 {shallowCopy(0)与noumenon(0)内存地址相同:true}
10 
11 {shallowStu  detail:[stuId:1,name:name:1,age:1]}
12 {noumenonStu detail:[stuId:1,name:name:1,age:1]}
13 {shallowStu equals noumenonStu:true}
14 {shallowCopy(1)与noumenon(1)内存地址相同:true}
15 
16 {shallowStu  detail:[stuId:2,name:name:2,age:2]}
17 {noumenonStu detail:[stuId:2,name:name:2,age:2]}
18 {shallowStu equals noumenonStu:true}
19 {shallowCopy(2)与noumenon(2)内存地址相同:true}
20 
21 {shallowStu  detail:[stuId:3,name:name:3,age:3]}
22 {noumenonStu detail:[stuId:3,name:name:3,age:3]}
23 {shallowStu equals noumenonStu:true}
24 {shallowCopy(3)与noumenon(3)内存地址相同:true}
25 
26 {shallowStu  detail:[stuId:4,name:name:4,age:4]}
27 {noumenonStu detail:[stuId:4,name:name:4,age:4]}
28 {shallowStu equals noumenonStu:true}
29 {shallowCopy(4)与noumenon(4)内存地址相同:true}
30 
31 ------------------deep copy test-----------------------
32 {deepStu     detail:[stuId:0,name:name:0,age:0]}
33 {noumenonStu detail:[stuId:0,name:name:0,age:0]}
34 {deepStu equals noumenonStu:true}
35 {deepCopy(0)与noumenon(0)内存地址相同:false}
36 
37 {deepStu     detail:[stuId:1,name:name:1,age:1]}
38 {noumenonStu detail:[stuId:1,name:name:1,age:1]}
39 {deepStu equals noumenonStu:true}
40 {deepCopy(1)与noumenon(1)内存地址相同:false}
41 
42 {deepStu     detail:[stuId:2,name:name:2,age:2]}
43 {noumenonStu detail:[stuId:2,name:name:2,age:2]}
44 {deepStu equals noumenonStu:true}
45 {deepCopy(2)与noumenon(2)内存地址相同:false}
46 
47 {deepStu     detail:[stuId:3,name:name:3,age:3]}
48 {noumenonStu detail:[stuId:3,name:name:3,age:3]}
49 {deepStu equals noumenonStu:true}
50 {deepCopy(3)与noumenon(3)内存地址相同:false}
51 
52 {deepStu     detail:[stuId:4,name:name:4,age:4]}
53 {noumenonStu detail:[stuId:4,name:name:4,age:4]}
54 {deepStu equals noumenonStu:true}
55 {deepCopy(4)与noumenon(4)内存地址相同:false}
56 
57 ------------------deepCommonCopy copy test-----------------------
58 {deepCommonStu detail:[stuId:0,name:name:0,age:0]}
59 {noumenonStu   detail:[stuId:0,name:name:0,age:0]}
60 {deepCommonStu equals noumenonStu:true}
61 {deepCommonCopy(0)与noumenon(0)内存地址相同:false}
62 
63 {deepCommonStu detail:[stuId:1,name:name:1,age:1]}
64 {noumenonStu   detail:[stuId:1,name:name:1,age:1]}
65 {deepCommonStu equals noumenonStu:true}
66 {deepCommonCopy(1)与noumenon(1)内存地址相同:false}
67 
68 {deepCommonStu detail:[stuId:2,name:name:2,age:2]}
69 {noumenonStu   detail:[stuId:2,name:name:2,age:2]}
70 {deepCommonStu equals noumenonStu:true}
71 {deepCommonCopy(2)与noumenon(2)内存地址相同:false}
72 
73 {deepCommonStu detail:[stuId:3,name:name:3,age:3]}
74 {noumenonStu   detail:[stuId:3,name:name:3,age:3]}
75 {deepCommonStu equals noumenonStu:true}
76 {deepCommonCopy(3)与noumenon(3)内存地址相同:false}
77 
78 {deepCommonStu detail:[stuId:4,name:name:4,age:4]}
79 {noumenonStu   detail:[stuId:4,name:name:4,age:4]}
80 {deepCommonStu equals noumenonStu:true}
81 {deepCommonCopy(4)与noumenon(4)内存地址相同:false}
原文地址:https://www.cnblogs.com/robinjava77/p/5481874.html