彻底搞懂Java普通类以及集合List浅克隆和深克隆

it2023-02-12  48

一、两种克隆方式的区别

浅克隆: 拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象

深克隆: 不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象

1.1、浅克隆示例

如下图,浅克隆仅仅拷贝了Teacher1中的基本变量,对于引用变量Student则没有拷贝,Teacher1和Teahcer2中的Student用的其实同一个,如果修改Teacher2中的Student的值,Teacher1中Student的值也会跟着改变

代码如下:

Student类

import lombok.Data; @Data public class Student implements Cloneable { private int age; private String name; public Student(int age, String name) { this.age = age; this.name = name; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } }

Teacher类

import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor class Teacher implements Cloneable { private String name; private int age; private Student student; @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Student student = new Student(20,"小明"); Teacher teacher1 = new Teacher("张老师",40,student); //为什么会出现以下结果, Teacher中的clone方法 Teacher teacher2 = (Teacher)teacher1.clone(); teacher2.getStudent().setName("小红"); teacher2.getStudent().setAge(22); System.out.println("teacher1:"+teacher1); System.out.println("teacher2:"+teacher2); } }

输出结果:

teacher1:Teacher(name=张老师, age=40, student=Student(age=22, name=小红)) teacher2:Teacher(name=张老师, age=40, student=Student(age=22, name=小红))

1.2、深克隆示例

如下图,深克隆不仅会拷贝Teacher1中的基本变量,同时也会拷贝引用变量Student,如果修改Teacher2中的Student的值,Teacher1中Student的值并不会跟着改变

代码如下:

Student类

import lombok.Data; @Data public class Student implements Cloneable { private int age; private String name; public Student(int age, String name) { this.age = age; this.name = name; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } }

Teacher类

import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor class Teacher implements Cloneable { private String name; private int age; private Student student; @Override public Object clone() throws CloneNotSupportedException { Teacher teacher = (Teacher)super.clone(); teacher.setStudent((Student)teacher.getStudent().clone()); return teacher; } public static void main(String[] args) throws CloneNotSupportedException { Student student = new Student(20,"小明"); Teacher teacher1 = new Teacher("张老师",40,student); //为什么会出现以下结果, Teacher中的clone方法 Teacher teacher2 = (Teacher)teacher1.clone(); teacher2.getStudent().setName("小红"); teacher2.getStudent().setAge(22); System.out.println("teacher1:"+teacher1); System.out.println("teacher2:"+teacher2); } }

输出结果:

teacher1:Teacher(name=张老师, age=40, student=Student(age=20, name=小明)) teacher2:Teacher(name=张老师, age=40, student=Student(age=22, name=小红))

二、集合中的克隆

以List为例,List是最常用的集合之一,有些时候我们会对一个List集合做各种各样的读写操作,但又不想改变List原来的值,这个时候一个List就不够用了,所以我们要复制出一个甚至多个克隆List来执行这些操作。List的克隆方式有很多,下面列举几种常见的List复制的方式,先申明一个list集合,如下:

List<String> list1 = new ArrayList<>(Arrays.asList("若兰","如雪","弄玉"));

1.1、用构造方法复制

List<String> list2 = new ArrayList<>(list1);

1.2、遍历复制

List<String> list3 = new ArrayList<>(); for (String str:list1) { list3.add(str); }

1.3、Collections.copy复制

List<String> list4 = new ArrayList<>(Arrays.asList(new String[list1.size()])); Collections.copy(list4,list1);

注意: 这里有一个坑,使用Collections.copy方法时一要声明目标集合的大小,并且要等于或者大于源集合的大小。 如果不声明或者小于源集合的元素个数,这样就会报错,报下标界的异常:java.lang.IndexOutOfBoundsException

1.4、System.arraycopy复制

String[] strs = new String[list1.size()]; System.arraycopy(list1.toArray(), 0, strs, 0, list1.size()); List<String> list5 = Arrays.asList(strs);

这个是系统底层的方法,并不推荐使用

1.5、clone复制

List<String> list6 = (List<String>) ((ArrayList<String>) list1).clone();

试试效果,代码如下:

List<String> list1 = new ArrayList<>(Arrays.asList("若兰","如雪","弄玉")); List<String> list2 = new ArrayList<>(list1); List<String> list3 = new ArrayList<>(); for (String str:list1) { list3.add(str); } List<String> list4 = new ArrayList<>(Arrays.asList(new String[list1.size()])); Collections.copy(list4,list1); String[] strs = new String[list1.size()]; System.arraycopy(list1.toArray(), 0, strs, 0, list1.size()); List<String> list5 = Arrays.asList(strs); List<String> list6 = (List<String>) ((ArrayList<String>) list1).clone(); list1.set(0,"琴烟"); list1.set(1,"寒梅"); System.out.println("list1: "+list1.toString()); System.out.println("list2: "+list2.toString()); System.out.println("list3: "+list3.toString()); System.out.println("list4: "+list4.toString()); System.out.println("list5: "+list5.toString()); System.out.println("list6: "+list6.toString());

输出结果如下:

list1: [琴烟, 寒梅, 弄玉] list2: [若兰, 如雪, 弄玉] list3: [若兰, 如雪, 弄玉] list4: [若兰, 如雪, 弄玉] list5: [若兰, 如雪, 弄玉] list6: [若兰, 如雪, 弄玉]

结果表明这几种方法都达到了我们想要的效果,但克隆有深浅之分,这些都是浅克隆,如果我们将String类型换成Student类型的话,结果就不一样了。

代码如下:

List<Student> list1 = new ArrayList<>(); list1.add(new Student(10,"若兰")); list1.add(new Student(20,"如雪")); list1.add(new Student(30,"弄玉")); List<Student> list2 = new ArrayList<>(list1); List<Student> list3 = new ArrayList<>(); for (Student str:list1) { list3.add(str); } List<Student> list4 = new ArrayList<>(Arrays.asList(new Student[list1.size()])); Collections.copy(list4,list1); Student[] strs = new Student[list1.size()]; System.arraycopy(list1.toArray(), 0, strs, 0, list1.size()); List<Student> list5 = Arrays.asList(strs); List<Student> list6 = (List<Student>) ((ArrayList<Student>) list1).clone(); list1.get(0).setName("琴烟"); list1.get(0).setAge(12); list1.get(1).setName("寒梅"); list1.get(1).setAge(22); System.out.println("list1: "+list1.toString()); System.out.println("list2: "+list2.toString()); System.out.println("list3: "+list3.toString()); System.out.println("list4: "+list4.toString()); System.out.println("list5: "+list5.toString()); System.out.println("list6: "+list6.toString());

输出结果如下:

list1: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list2: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list3: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list4: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list5: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list6: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)]

结果显示五种复制方法全都失效,为什么会出现这种情况呢?其实这和集合中元素的存储方式有关,如果元素是基本类型,那存储的就是实实在在的值,如果元素是类类型,那存储的仅是元素地址而已。所以虽然我们对list进行了复制,但其中元素的地址并没有改变,如果对元素进行了修改,那么所有复制而来的list中的元素都会跟着改变。

1.6、集合的深度克隆方法

为了解决上面的问题有人巧用序列化对象让集合中的数据在IO流中来回折腾一圈,结果完美解决,(Student要实现Serializable接口),代码如下:

public static <T> List<T> deepCopyList(List<T> src) { List<T> dest = null; try { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(src); ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); ObjectInputStream in = new ObjectInputStream(byteIn); dest = (List<T>) in.readObject(); } catch (Exception e) { e.printStackTrace(); } return dest; }

测试代码如下:

List<Student> list1 = new ArrayList<>(); list1.add(new Student(10,"若兰")); list1.add(new Student(20,"如雪")); list1.add(new Student(30,"弄玉")); List<Student> list2 = new ArrayList<>(list1); List<Student> list3 = new ArrayList<>(); for (Student str:list1) { list3.add(str); } List<Student> list4 = new ArrayList<>(Arrays.asList(new Student[list1.size()])); Collections.copy(list4,list1); Student[] strs = new Student[list1.size()]; System.arraycopy(list1.toArray(), 0, strs, 0, list1.size()); List<Student> list5 = Arrays.asList(strs); List<Student> list6 = (List<Student>) ((ArrayList<Student>) list1).clone(); List<Student> list7 = deepCopyList(list1); list1.get(0).setName("琴烟"); list1.get(0).setAge(12); list1.get(1).setName("寒梅"); list1.get(1).setAge(22); System.out.println("list1: "+list1.toString()); System.out.println("list2: "+list2.toString()); System.out.println("list3: "+list3.toString()); System.out.println("list4: "+list4.toString()); System.out.println("list5: "+list5.toString()); System.out.println("list6: "+list6.toString()); System.out.println("list7: "+list7.toString());

输出结果:

list1: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list2: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list3: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list4: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list5: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list6: [Student(age=12, name=琴烟), Student(age=22, name=寒梅), Student(age=30, name=弄玉)] list7: [Student(age=10, name=若兰), Student(age=20, name=如雪), Student(age=30, name=弄玉)]

前面五种全部失败,最后一种成功!

最新回复(0)