网站首页 > java教程 正文
浅拷贝是指在对一个对象进行拷贝时,只拷贝对象本身和其中的基本数据类型,而不拷贝对象内部的引用类型。因此,在浅拷贝的对象中,引用类型的变量指向的依旧是原始对象中的引用。
深拷贝是指在对一个对象进行拷贝时,不仅拷贝对象本身和其中的基本数据类型,同时也拷贝对象内部的引用类型。因此,在深拷贝的对象中,引用类型的变量指向的是全新的对象。
cloning
克隆库是一个小型的开源(Apache许可)Java库,用于深度克隆对象。对象不必实现可克隆接口。实际上,这个库可以克隆任何Java对象。如果您不想修改缓存对象,或者每当您想创建对象的深度副本时,它可以在缓存实现中使用。
重要提示:Java类的深度克隆可能意味着数千个对象被克隆!另外,克隆文件和流可能会导致JVM崩溃。强烈建议在开发期间启用将克隆类转储到stdout,以便查看克隆的内容。
<dependency>
<groupId>io.github.kostaskougios</groupId>
<artifactId>cloning</artifactId>
<version>1.10.3</version>
</dependency>
Github: https://github.com/kostaskougios/cloning
cloning 测试代码
cloning 可以深度克隆任何 java 对象, 简单对象,复杂对象和集合对象,集合中的每个元素也都会被深度克隆。简单易用,功能强大。
publicclass CloningExp {
public static void main(String[] args) {
Cloner cloner = new Cloner();
// clone 简单对象
Bean1 bean1 = new Bean1();
bean1.setF1("XA");
bean1.setF2(478354);
System.out.println("bean1 -> " + bean1);
Bean1 clonedBean1 = cloner.deepClone(bean1);
System.out.println("clonedBean1 -> " + clonedBean1);
System.out.println("bean1 == clonedBean1 -> " + (bean1 == clonedBean1));
// clone 复杂对象
Bean2 bean2 = new Bean2();
bean2.setF1("VVV");
bean2.setF2(bean1);
System.out.println("bean2 -> " + bean2);
Bean2 clonedBean2 = cloner.deepClone(bean2);
System.out.println("clonedBean2 -> " + clonedBean2);
System.out.println("bean2 == clonedBean2 -> " + (bean2 == clonedBean2));
System.out.println("bean2.bean1 == clonedBean2.bean1 -> " + (bean2.getF2() == clonedBean2.getF2()));
// clone 集合
List<Bean1> bean1List = new ArrayList<>();
bean1List.add(new Bean1("Bean1-1" , 1));
bean1List.add(new Bean1("Bean1-2" , 1));
bean1List.add(new Bean1("Bean1-3" , 1));
System.out.println("bean1List -> " + bean1List);
List<Bean1> clonedBean1List = cloner.deepClone(bean1List);
System.out.println("clonedBean1List -> " + clonedBean1List);
System.out.println("bean1List == clonedBean1List -> " + (bean1List == clonedBean1List));
Map<Bean1 , Bean2> map = new HashMap<>();
Bean1 mapBean1 = new Bean1("Bean1-1", 1);
Bean2 mapBean2 = new Bean2("Bean2-1", mapBean1);
map.put(mapBean1 , mapBean2);
System.out.println("map -> " + map);
Map<Bean1, Bean2> clonedMap = cloner.deepClone(map);
System.out.println("clonedMap -> " + clonedMap);
System.out.println("map == clonedMap -> " + (map == clonedMap));
Cloner clonedCloner = cloner.deepClone(cloner);
System.out.println("cloner == clonedCloner -> " + (cloner == clonedCloner));
Map<String , Object> sourceMap = new HashMap<>();
sourceMap.put("1" , new Bean1("CAC" , 555));
sourceMap.put("2" , new Bean1("AXC" , 5589));
sourceMap.put("3" , new Bean1("bbv" , 76));
sourceMap.put("4" , "dasfsdgsdg");
sourceMap.put("5" , new Object());
sourceMap.put("6" , new ArrayList<>());
sourceMap.put("7" , 99999999999L);
sourceMap.put("8" , new HashMap<>());
long startTimeMillis = System.currentTimeMillis();
for (int i = 0 ;i < 500000; i++) {
cloner.deepClone(sourceMap);
}
System.out.println("time cost -> " + (System.currentTimeMillis() - startTimeMillis) + " ms");
}
@Data
@AllArgsConstructor
@NoArgsConstructor
staticclass Bean1 {
private String f1;
private Integer f2;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
staticclass Bean2 {
private String f1;
private Bean1 f2;
}
}
bean1 -> CloningExp.Bean1(f1=XA, f2=478354)
clonedBean1 -> CloningExp.Bean1(f1=XA, f2=478354)
bean1 == clonedBean1 -> false
bean2 -> CloningExp.Bean2(f1=VVV, f2=CloningExp.Bean1(f1=XA, f2=478354))
clonedBean2 -> CloningExp.Bean2(f1=VVV, f2=CloningExp.Bean1(f1=XA, f2=478354))
bean2 == clonedBean2 -> false
bean2.bean1 == clonedBean2.bean1 -> false
bean1List -> [CloningExp.Bean1(f1=Bean1-1, f2=1), CloningExp.Bean1(f1=Bean1-2, f2=1), CloningExp.Bean1(f1=Bean1-3, f2=1)]
clonedBean1List -> [CloningExp.Bean1(f1=Bean1-1, f2=1), CloningExp.Bean1(f1=Bean1-2, f2=1), CloningExp.Bean1(f1=Bean1-3, f2=1)]
bean1List == clonedBean1List -> false
map -> {CloningExp.Bean1(f1=Bean1-1, f2=1)=CloningExp.Bean2(f1=Bean2-1, f2=CloningExp.Bean1(f1=Bean1-1, f2=1))}
clonedMap -> {CloningExp.Bean1(f1=Bean1-1, f2=1)=CloningExp.Bean2(f1=Bean2-1, f2=CloningExp.Bean1(f1=Bean1-1, f2=1))}
map == clonedMap -> false
cloner == clonedCloner -> false
time cost -> 447 ms
cloning 性能
Map<String , Object> sourceMap = new HashMap<>();
sourceMap.put("1" , new Bean1("CAC" , 555));
sourceMap.put("2" , new Bean1("AXC" , 5589));
sourceMap.put("3" , new Bean1("bbv" , 76));
sourceMap.put("4" , "dasfsdgsdg");
sourceMap.put("5" , new Object());
sourceMap.put("6" , new ArrayList<>());
sourceMap.put("7" , 99999999999L);
sourceMap.put("8" , new HashMap<>());
对 sourceMap 克隆 50万次耗时 447 ms
Apache Commons Lang3
Apache Commons Lang3 中有一个 SerializationUtils 类用来对目标对象进行深度克隆,它内部使用的是 JDK 内置的对对象的序列化和反序列化,所以使用 SerializationUtils 有个前提是对象必须实现了 Serializable 接口。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>
SerializationUtils 测试代码
publicclass SerializationUtilsExp {
public static void main(String[] args) {
// clone 简单对象
Bean1 bean1 = new Bean1();
bean1.setF1("XA");
bean1.setF2(478354);
System.out.println("bean1 -> " + bean1);
Bean1 clonedBean1 = SerializationUtils.clone(bean1);
System.out.println("clonedBean1 -> " + clonedBean1);
System.out.println("bean1 == clonedBean1 -> " + (bean1 == clonedBean1));
// clone 复杂对象
Bean2 bean2 = new Bean2();
bean2.setF1("VVV");
bean2.setF2(bean1);
System.out.println("bean2 -> " + bean2);
Bean2 clonedBean2 = SerializationUtils.clone(bean2);
System.out.println("clonedBean2 -> " + clonedBean2);
System.out.println("bean2 == clonedBean2 -> " + (bean2 == clonedBean2));
System.out.println("bean2.bean1 == clonedBean2.bean1 -> " + (bean2.getF2() == clonedBean2.getF2()));
// clone 集合
ArrayList<Bean1> bean1List = new ArrayList<>();
bean1List.add(new Bean1("Bean1-1" , 1));
bean1List.add(new Bean1("Bean1-2" , 1));
bean1List.add(new Bean1("Bean1-3" , 1));
System.out.println("bean1List -> " + bean1List);
List<Bean1> clonedBean1List = SerializationUtils.clone(bean1List);
System.out.println("clonedBean1List -> " + clonedBean1List);
System.out.println("bean1List == clonedBean1List -> " + (bean1List == clonedBean1List));
HashMap<Bean1, Bean2> map = new HashMap<>();
Bean1 mapBean1 = new Bean1("Bean1-1", 1);
Bean2 mapBean2 = new Bean2("Bean2-1", mapBean1);
map.put(mapBean1 , mapBean2);
System.out.println("map -> " + map);
Map<Bean1, Bean2> clonedMap = SerializationUtils.clone(map);
System.out.println("clonedMap -> " + clonedMap);
System.out.println("map == clonedMap -> " + (map == clonedMap));
HashMap<String , Object> sourceMap = new HashMap<>();
sourceMap.put("1" , new Bean1("CAC" , 555));
sourceMap.put("2" , new Bean1("AXC" , 5589));
sourceMap.put("3" , new Bean1("bbv" , 76));
sourceMap.put("4" , "dasfsdgsdg");
sourceMap.put("5" , "AAAAAAAAAAAAAA");
sourceMap.put("6" , new ArrayList<>());
sourceMap.put("7" , 99999999999L);
sourceMap.put("8" , new HashMap<>());
long startTimeMillis = System.currentTimeMillis();
for (int i = 0 ;i < 500000; i++) {
SerializationUtils.clone(sourceMap);
}
System.out.println("time cost -> " + (System.currentTimeMillis() - startTimeMillis) + " ms");
}
@Data
@AllArgsConstructor
@NoArgsConstructor
staticclass Bean1 implements Serializable {
private String f1;
private Integer f2;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
staticclass Bean2 implements Serializable {
private String f1;
private Bean1 f2;
}
}
bean1 -> SerializationUtilsExp.Bean1(f1=XA, f2=478354)
clonedBean1 -> SerializationUtilsExp.Bean1(f1=XA, f2=478354)
bean1 == clonedBean1 -> false
bean2 -> SerializationUtilsExp.Bean2(f1=VVV, f2=SerializationUtilsExp.Bean1(f1=XA, f2=478354))
clonedBean2 -> SerializationUtilsExp.Bean2(f1=VVV, f2=SerializationUtilsExp.Bean1(f1=XA, f2=478354))
bean2 == clonedBean2 -> false
bean2.bean1 == clonedBean2.bean1 -> false
bean1List -> [SerializationUtilsExp.Bean1(f1=Bean1-1, f2=1), SerializationUtilsExp.Bean1(f1=Bean1-2, f2=1), SerializationUtilsExp.Bean1(f1=Bean1-3, f2=1)]
clonedBean1List -> [SerializationUtilsExp.Bean1(f1=Bean1-1, f2=1), SerializationUtilsExp.Bean1(f1=Bean1-2, f2=1), SerializationUtilsExp.Bean1(f1=Bean1-3, f2=1)]
bean1List == clonedBean1List -> false
map -> {SerializationUtilsExp.Bean1(f1=Bean1-1, f2=1)=SerializationUtilsExp.Bean2(f1=Bean2-1, f2=SerializationUtilsExp.Bean1(f1=Bean1-1, f2=1))}
clonedMap -> {SerializationUtilsExp.Bean1(f1=Bean1-1, f2=1)=SerializationUtilsExp.Bean2(f1=Bean2-1, f2=SerializationUtilsExp.Bean1(f1=Bean1-1, f2=1))}
map == clonedMap -> false
time cost -> 26061 ms
SerializationUtils 性能
HashMap<String , Object> sourceMap = new HashMap<>();
sourceMap.put("1" , new Bean1("CAC" , 555));
sourceMap.put("2" , new Bean1("AXC" , 5589));
sourceMap.put("3" , new Bean1("bbv" , 76));
sourceMap.put("4" , "dasfsdgsdg");
sourceMap.put("5" , "AAAAAAAAAAAAAA");
sourceMap.put("6" , new ArrayList<>());
sourceMap.put("7" , 99999999999L);
sourceMap.put("8" , new HashMap<>());
对 sourceMap 克隆 50万次耗时 25716 ms
其他一些深拷贝的方式
- new 对象 (太累了且不优雅)
- JSON 序列化
- 使用一些对象序列化库,例如:Protocol Buffers , Kryo , ProtoStuff , Hessian ;
猜你喜欢
- 2024-10-19 Python 中赋值、浅拷贝、深拷贝的区别是什么?
- 2024-10-19 Java 对象拷贝原理剖析(java 对象拷贝原理剖析)
- 2024-10-19 认识Object类和深浅拷贝!(阐述object.assign的用法,深拷贝与浅拷贝的区别?)
- 2024-10-19 三面“有赞”Java岗斩获offer:Spring+JVM+并发锁+分布式+算法
- 2024-10-19 谈谈 Java 开发中的对象拷贝(java对象拷贝工具类)
- 2024-10-19 深入浅出Java中的clone克隆方法,写得也太棒了
- 2024-10-19 深拷贝和浅拷贝之list、dataframe
- 2024-10-19 对象拷贝java 浅谈(java对象的拷贝)
- 2024-10-19 Java克隆对象需要知道的事(java克隆的作用)
- 2024-10-19 Java的Object十二个常用方法,你用过几个?
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)