其他
什么是 Java 对象深拷贝?面试必问!
点击上方蓝色链接,关注并“设为星标”
介绍
拷贝对象
* 用户
*/
public class User {
private String name;
private Address address;
// constructors, getters and setters
}
/**
* 地址
*/
public class Address {
private String city;
private String country;
// constructors, getters and setters
}
方法一 构造函数
测试用例
public void constructorCopy() {
Address address = new Address("杭州", "中国");
User user = new User("大山", address);
// 调用构造函数时进行深拷贝
User copyUser = new User(user.getName(), new Address(address.getCity(), address.getCountry()));
// 修改源对象的值
user.getAddress().setCity("深圳");
// 检查两个对象的值不同
assertNotSame(user.getAddress().getCity(), copyUser.getAddress().getCity());
}
方法二 重载clone()方法
重写代码
* 地址
*/
public class Address implements Cloneable {
private String city;
private String country;
// constructors, getters and setters
@Override
public Address clone() throws CloneNotSupportedException {
return (Address) super.clone();
}
}
* 用户
*/
public class User implements Cloneable {
private String name;
private Address address;
// constructors, getters and setters
@Override
public User clone() throws CloneNotSupportedException {
User user = (User) super.clone();
user.setAddress(this.address.clone());
return user;
}
}
测试用例
public void cloneCopy() throws CloneNotSupportedException {
Address address = new Address("杭州", "中国");
User user = new User("大山", address);
// 调用clone()方法进行深拷贝
User copyUser = user.clone();
// 修改源对象的值
user.getAddress().setCity("深圳");
// 检查两个对象的值不同
assertNotSame(user.getAddress().getCity(), copyUser.getAddress().getCity());
}
方法三 Apache Commons Lang序列化
重写代码
* 地址
*/
public class Address implements Serializable {
private String city;
private String country;
// constructors, getters and setters
}
* 用户
*/
public class User implements Serializable {
private String name;
private Address address;
// constructors, getters and setters
}
测试用例
public void serializableCopy() {
Address address = new Address("杭州", "中国");
User user = new User("大山", address);
// 使用Apache Commons Lang序列化进行深拷贝
User copyUser = (User) SerializationUtils.clone(user);
// 修改源对象的值
user.getAddress().setCity("深圳");
// 检查两个对象的值不同
assertNotSame(user.getAddress().getCity(), copyUser.getAddress().getCity());
}
方法四 Gson序列化
测试用例
public void gsonCopy() {
Address address = new Address("杭州", "中国");
User user = new User("大山", address);
// 使用Gson序列化进行深拷贝
Gson gson = new Gson();
User copyUser = gson.fromJson(gson.toJson(user), User.class);
// 修改源对象的值
user.getAddress().setCity("深圳");
// 检查两个对象的值不同
assertNotSame(user.getAddress().getCity(), copyUser.getAddress().getCity());
}
方法五 Jackson序列化
重写代码
* 用户
*/
public class User {
private String name;
private Address address;
// constructors, getters and setters
public User() {
}
}
* 地址
*/
public class Address {
private String city;
private String country;
// constructors, getters and setters
public Address() {
}
}
测试用例
public void jacksonCopy() throws IOException {
Address address = new Address("杭州", "中国");
User user = new User("大山", address);
// 使用Jackson序列化进行深拷贝
ObjectMapper objectMapper = new ObjectMapper();
User copyUser = objectMapper.readValue(objectMapper.writeValueAsString(user), User.class);
// 修改源对象的值
user.getAddress().setCity("深圳");
// 检查两个对象的值不同
assertNotSame(user.getAddress().getCity(), copyUser.getAddress().getCity());
}
总结
- END -
关注Java技术栈微信公众号,在后台回复关键字:Java,可以获取一份栈长整理的 Java 最新技术干货。
最近干货分享
点击「阅读原文」加入栈长的战队~