List深度克隆

/**
 * @Description: List深度克隆
 * @Param: source 源集合
 * @Return: java.util.List
 * @Author: CWR
 * @Date: 2019/12/26 16:17
 */
public static  List deepCopy(List source) throws IOException, ClassNotFoundException {
	ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
	ObjectOutputStream out = new ObjectOutputStream(byteOut);
	out.writeObject(source);
	ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
	ObjectInputStream in = new ObjectInputStream(byteIn);
	@SuppressWarnings("unchecked")
	List result = (List) in.readObject();
	return result;
}

克隆的对象必须要实现Serializable接口,不然会报错。

你可能感兴趣的:(Java)