1、基本类型的数据 没有hashcode 直接add
2、包含bean数据类型的list 间相互复制
不管是addall 还是 add
如:
list1.add(list2.get(1));
如果数据类型包含bean, debug会发现数据其实指向是一样的
当list2中数据修改时,list1中的数据同样被修改
此时正确的做法应该是再写一个copy方法
Bean data = new Bean();
data.setName(list2.get(i).getName());
基础数据的复制可以正常set就行,如果是复制的数据类型,则需要继续new 子类
3、把数据写入内存中再读出来,下面时网上的方法,bean对象继承Serializable
package com.example.chanp.testannotation.util;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;/*** Created by sjx on 2019/6/27.*/public class CloneObjectUtils {public static <T> T cloneObject(T obj) {T result = null;ByteArrayOutputStream byteArrayOutputStream = null;ByteArrayInputStream byteArrayInputStream = null;ObjectOutputStream outputStream = null;ObjectInputStream inputStream = null;try {//对象写到内存中byteArrayOutputStream = new ByteArrayOutputStream();outputStream = new ObjectOutputStream(byteArrayOutputStream);outputStream.writeObject(obj);//从内存中再读出来byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());inputStream = new ObjectInputStream(byteArrayInputStream);result = (T) inputStream.readObject();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {try {if (outputStream != null)outputStream.close();if (inputStream != null)inputStream.close();if (byteArrayOutputStream != null)byteArrayOutputStream.close();if (byteArrayInputStream != null)byteArrayInputStream.close();} catch (IOException e) {e.printStackTrace();}}return result;}
}
调用:Bean bean = CloneObjectUtils.cloneObject(list1.get(0));
--------------------->>>>>>>>>>>>>>>>>
2023/08/28 by dpl