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 - 深拷贝
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01