集合 @SneakyThrows public <T> List<T> deepCopy(List<T> src) { // 将序列化到字节流 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; } 对象 @SneakyThrows public <T> T deepCopy(T src) { // 将对象本身序列化到字节流 ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(src); // 再将字节流通过反序列化方式得到对象副本 ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray())); @SuppressWarnings("unchecked") T dest = (T) objectInputStream.readObject(); return dest; }