Java HashMap - deep copy(Java HashMap - 深拷贝)
问题描述
我只是想找出如何制作 HashMap 的深层副本的最佳解决方案.此地图中没有实现 Cloneable 的对象.我想找到比序列化和反序列化更好的解决方案.
I am just trying to find out the best solution how to make a deep copy of HashMap. There are no objects in this map which implement Cloneable. I would like to find better solution than serialization and deserialization.
推荐答案
看看 Deep Cloning,在 Google Code 上你可以找到一个库.您可以在 https://github.com/kostaskougios/cloning 上阅读它.
Take a look at Deep Cloning, on Google Code you can find a library. You can read it on https://github.com/kostaskougios/cloning.
它的工作原理很简单.这可以克隆任何对象,并且对象不必实现任何接口,例如可序列化.
How it works is easy. This can clone any object, and the object doesnt have to implement any interfaces, like serializable.
Cloner cloner = new Cloner();
MyClass clone = cloner.deepClone(o);
// clone is a deep-clone of o
但请注意:这可能会克隆数千个对象(如果克隆的对象具有那么多引用).此外,复制文件或流可能会使 JVM 崩溃.
Be aware though: this may clone thousands of objects (if the cloned object has that many references). Also, copying Files or Streams may crash the JVM.
但是,您可以忽略某些类实例,例如流等.值得检查这个库及其来源.
You can, however, ignore certain instances of classes, like streams et cetera. It's worth checking this library and its source out.
这篇关于Java HashMap - 深拷贝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java HashMap - 深拷贝
基础教程推荐
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- JPA惰性列表上的流 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Maven:无效的目标版本:10 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
