java中List深拷贝的简单实例

1.实例中用到的类(一定要实现Serializable接口)

class Person implements Serializable{
    private String Name;
    private List<Double> other;
    private int age;

 。。。。省略get set方法
}

2.克隆List的方法

    public static <T> List<T> deepCopy(List<T> src) {
        List<T> dest=null;
        try{
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);
        out.close();

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        dest = (List<T>) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
        return dest;
    }

3.测试代码即输出结果

List<Double> list=Stream.of(23.6,28.7,21.2).collect(Collectors.toList());
        Person person=new Person();
        person.setName("jerry");
        person.setAge(18);
        person.setOther(list);

        List<Double> list1=Stream.of(45.2,78.6,12.3).collect(Collectors.toList());
        Person p1=new Person();
        p1.setName("tome");
        p1.setAge(20);
        p1.setOther(list1);


        List<Double> list2=Stream.of(99.1,27.7,21.2).collect(Collectors.toList());
        Person p2=new Person();
        p2.setName("Mike");
        p2.setAge(25);
        p2.setOther(list2);


        List<Double> list3=Stream.of(5.6,8.7,1.2).collect(Collectors.toList());
        Person p3=new Person();
        p3.setName("Susan");
        p3.setAge(30);
        p3.setOther(list3);

        List<Person> list_person=Stream.of(person,p1,p2,p3).collect(Collectors.toList());
        List<Person> listclone_p=deepCopy(list_person);
        listclone_p.get(0).setName("Alex");
        listclone_p.get(0).setAge(66);
        listclone_p.get(0).setOther(Arrays.asList(6.6,66.6,666.6));
        list_person.stream().forEach(e->System.out.printf("名字:%s,年龄:%d others1:%.2f \n",e.getName(),e.getAge(),e.getOther().get(0)));
        System.out.println("克隆之后的变化");
        listclone_p.stream().forEach(e->System.out.printf("名字:%s,年龄:%d others1:%.2f \n",e.getName(),e.getAge(),e.getOther().get(0)));
名字:jerry,年龄:18 others1:23.60 
名字:tome,年龄:20 others1:45.20 
名字:Mike,年龄:25 others1:99.10 
名字:Susan,年龄:30 others1:5.60 
克隆之后的变化
名字:Alex,年龄:66 others1:6.60 
名字:tome,年龄:20 others1:45.20 
名字:Mike,年龄:25 others1:99.10 
名字:Susan,年龄:30 others1:5.60 

根据输出结果,可知拷贝的List修改不影响主List.即深拷贝。

Java 深拷贝和浅拷贝是指创建新对象并复制原有对象数据结构的过程,通常用于复杂的数据结构如数组、集合、对象等。它们的区别在于是否完全复制了对象的所有内容,包括嵌套的对象。 **浅拷贝**(Shallow Copy):它只是将原对象的引用复制给新对象。这意味着如果新对象包含对原对象的引用,那么修改新对象会影响到原对象。在 Java ,对于基本类型,浅拷贝就是简单地赋值;对于引用类型(如对象),则是复制指向内存地址的引用。 例如: ```java Object obj = new Object(); Object copy = obj; // 浅拷贝,copy 和 obj 引向同一块内存 List list = new ArrayList(); // 对象 List copyList = list; // 浅拷贝,list 和 copyList 都引用同一个ArrayList实例 ``` 修改 `copyList` 的元素会改变 `list`: ```java copyList.add("test"); System.out.println(list); // 输出 "test" ``` **深拷贝**(Deep Copy):则会对整个对象及其所有嵌套对象进行完整的副本操作,包括递归复制嵌套对象。这样改动一个深拷贝不会影响原始对象。在 Java ,可以手动实现深拷贝,或者使用序列化反序列化的机制,比如 `Cloneable` 接口和 `Object.clone()` 方法(虽然不是所有类都实现了 Cloneable)。 例如: ```java Object obj = new Object(); Object deepCopy = SerializationUtils.deepCopy(obj); // 使用工具类的深拷贝方法 ``` 在这个例子,`deepCopy` 是一个新的对象,它的状态与 `obj` 完全独立。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值