实现深度拷贝的两种方法

1.对象实现拷贝方法,对象内部的基本类型不用管,不涉及到地址

对象类型继续实现拷贝方法:

package calmcalmcalm;

import lombok.Data;

import java.util.Arrays;
import java.util.List;

/**
 * @author jinsong.Liang
 * @Description
 * @date 2019/4/28 18:10
 */
public class deepCopy2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        person.setName("小李");
        person.setAge(20);
        GrandParent one = new GrandParent("小李爷爷", 80);
        GrandParent another = new GrandParent("小李奶奶", 77);
        person.setGrandParents(Arrays.asList(one,another));
        Person person2 = (Person) person.clone();
        person2.setName("小松");
        person2.setAge(21);
        person2.setGrandParents(Arrays.asList(new GrandParent("小松爷爷",81),new GrandParent("小松奶奶",77)));
        System.out.println(person+" "+person2);
    }
}

@Data
class Person implements Cloneable{
    private String name;
    private int age;
    private List<GrandParent> grandParents;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

@Data
class GrandParent implements Cloneable{
    private String name;
    private int age;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public GrandParent() {
    }

    public GrandParent(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

2.使用序列化 直接写入流中 再从流中读取 用到了

ByteArrayOutputStream ByteArrayInputStream

ObjectOutputStream ObjectInputStream:

package calmcalmcalm;

import lombok.Data;

import java.io.*;
import java.util.Arrays;
import java.util.List;

/**
 * @author jinsong.Liang
 * @Description 串行化实现深拷贝
 * @date 2019/4/28 17:27
 */

public class deepCopy {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("小李");
        Parent one = new Parent("小李爸", 40);
        Parent another = new Parent("小李妈", 37);
        stu.setParents(Arrays.asList(one, another));
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(out);
            os.writeObject(stu);
            ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
            ObjectInputStream is = new ObjectInputStream(in);
            Student object = (Student) is.readObject();
            object.setName("小松");
            object.setParents(Arrays.asList(new Parent("小松爸", 40), new Parent("小松妈", 36)));
            System.out.println(object + " " + stu);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Data
class Student implements Serializable {
    private static final long serialVersionUID = 1225886679045375224L;
    private String name;
    private List<Parent> parents;
}

@Data
class Parent implements Serializable {
    private static final long serialVersionUID = 3295359707477734908L;
    private String name;
    private int age;

    public Parent() {
    }

    public Parent(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

Idea出现中文乱码:字符编码设置为GBK即可  convert方式而非reload方式

关于编码:

    GBK存储:中文俩字节 英文一个字节

    UTF8  UTF16 UTF32是根据尽可能使用分配空间和直接分配较大的空间而产生的不同编码方式

     UTF8存储 1-6字节  UTF16介于UTF8 和 UTF32之间 2字节或4字节 UTF32是4个字节存储

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Java 中,可以使用以下两种方式实现集合的深拷贝: 1. 使用序列化与反序列化 这种方式需要将集合对象先序列化成字节数组,再通过反序列化生成一个新的对象。由于序列化会将对象的所有属性都进行深拷贝,因此可以实现集合的深拷贝。示例代码如下: ```java public static <T extends Serializable> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(src); ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); ObjectInputStream in = new ObjectInputStream(byteIn); @SuppressWarnings("unchecked") List<T> dest = (List<T>) in.readObject(); return dest; } ``` 2. 遍历拷贝 这种方式需要手动遍历集合中的每个元素,对每个元素进行深拷贝,最后将所有拷贝后的元素添加到一个新的集合中。示例代码如下: ```java public static <T extends Serializable> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException { List<T> dest = new ArrayList<T>(src.size()); for (T element : src) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(element); ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); ObjectInputStream in = new ObjectInputStream(byteIn); @SuppressWarnings("unchecked") T copy = (T) in.readObject(); dest.add(copy); } return dest; } ``` 需要注意的是,这两种方式都要求集合中的元素必须实现 Serializable 接口。如果集合中的元素没有实现 Serializable 接口,则需要手动实现深拷贝方法
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值