方法一:实现 Cloneable 接口并重写 Object 类中的 clone() 方法
import lombok.Data;
public class Test {
@Data
static class Hello implements Cloneable {
private String name;
public Hello(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public static void main(String[] args) throws CloneNotSupportedException {
Hello hello = new Hello("name");
Hello clone = (Hello) hello.clone();
System.out.println(clone);
}
}
方法二:使用spring的BeanUtils(推荐)
import lombok.Data;
import org.springframework.beans.BeanUtils;
public class Test {
@Data
static class Hello {
private String name;
public Hello() {
}
public Hello(String name) {
this.name = name;
}
}
public static void main(String[] args) {
Hello source = new Hello("name");
Hello target = new Hello();
BeanUtils.copyProperties(source, target);
System.out.println(target);
}
}
方法三:使用fastjson通过对象json序列化和反序列化来完成对象复制(深度克隆)
import com.alibaba.fastjson.JSON;
import lombok.Data;
public class Test {
@Data
static class Hello {
private String name;
public Hello() {
}
public Hello(String name) {
this.name = name;
}
}
public static void main(String[] args) {
Hello source = new Hello("name");
String sourceStr = JSON.toJSONString(source);//序列化为json
Hello target = JSON.parseObject(sourceStr, Hello.class);//反序列化
System.out.println(target);
}
}
方法四:利用序列化实现对象的拷贝(深度克隆)
/**
* 对象深拷贝
*
* @param obj
* @param <T>
* @return
*/
public static <T extends Serializable> T deepClone2(T obj) {
T cloneObj = null;
try {
//写入字节流
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream obs = new ObjectOutputStream(out);
obs.writeObject(obj);
obs.close();
//分配内存,写入原始对象,生成新对象
ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(ios);
//返回生成的新对象
cloneObj = (T) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
}
return cloneObj;
}
/**
* 对象深拷贝 try-with-resources写法
*
* @param obj
* @param <T>
* @return
*/
public static <T extends Serializable> T deepClone(T obj) {
T cloneObj = null;
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream obs = new ObjectOutputStream(out);
ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(ios);
) {
obs.writeObject(obj);
cloneObj = (T) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return cloneObj;
}
/**
* List深拷贝
*
* @param src
* @param <T>
* @return
*/
public static <T> List<T> deepCloneList2(List<T> src) {
List<T> dest = null;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
dest = (List<T>) in.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
}
return dest;
}
/**
* List深拷贝 try-with-resources写法
*
* @param src
* @param <T>
* @return
*/
public static <T> List<T> deepCloneList(List<T> src) {
List<T> dest = null;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
dest = (List<T>) in.readObject();
} catch (Exception e) {
e.printStackTrace();
}return dest;
}