list分为浅拷贝和深拷贝,深拷贝就是list1拷贝到list2,我修改list2的内容,不用同步修改list1的内容,浅拷贝则会修改list1的内容。在网上查了有用Collections.copy或者Dto的方式实现,使用后感觉不是很好用,并且感觉不是很好找到直观方便的方式。于是,花了点儿时间在网络上找了一种方式,觉得蛮方便的,分享一下:

调用:
List<Dto> list2= deepCopy(list1);
实现方法:
public static <T> 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;
}

原deepCopy方法地址:
https://www.pianshen.com/article/17271053790/