vue项目接口域名动态的获取方法
295
2022-11-08
java中深复制知识点详解
在正式开始深复制的讲解之前,我们先来理解一下概念。假设一个物品需要批量生产,但是这个物品还配有赠品,生产的时候需要把赠品也列在计划内。所谓深复制的原理就是这样,我们不能只复制属性,包括引用之类的附带也需要被复制。下面就为大家带来深复制的两种不同方法。
1.序列化实现
如下为谷歌Gson序列化HashMap,实现深度复制的例子:
public class CopyDeepMapTest {
public static void main(String[] args) {
HashMap
userMap.put(1, new User("jay", 26));
userMap.put(2, new User("fany", 25));
//Shallow clone
Gson gson = new Gson();
String jsonString = gsonhttp://.toJson(userMap);
Type type = new TypeToken
HashMap
//Same as userMap
System.out.println(clonedMap);
System.out.println("\nChanges DO NOT reflect in other map \n");
//Change a value is clonedMap
clonedMap.get(1).setName("test");
//Verify content of both maps
System.out.println(userMap);
System.out.println(clonedMap);
}
}
运行结果:
{1=User{name='jay', age=26}, 2=User{name='fany', age=25}}
Changes DO NOT reflect in other map
{1=User{name='jay', age=26}, 2=User{name='fany', age=25}}
{1=User{name='test', age=26}, 2=User{name='fany', age=25}}
从运行结果看出,对cloneMap修改,userMap没有被改变,所以是深度复制。
2.list深复制
两个list数据相同但是地址值不同,A和B是独立的两个list,A改变了B不会改变,B改变了A也不会改变
深复制的工具类方法:
public static
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&http://lt;T> dest = (List
return dest;
}
关于深拷贝的一个扩展实例:
public class YuelyLog implements Serializable,Cloneable {
private Attachment attachment;
private String name;
private String date;
@Override
protected YuelyLog clone() throws CloneNotSupportedException {
return (YuelyLog)super.clone();
}
public Attachment getAttachment() {
return attachment;
}
public void setAttachment(Attachment attachment) {
this.attachment = attachment;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
/**
* 使用序列化技术实现深拷贝
* @return
*/
public YuelyLog deepClone() throws IOException,Clashttp://sNotFoundException{
//将对象写入流中
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(this);
//从流中取出
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
return (YuelyLog)objectInputStream.readObject();
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~