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 - 深拷贝
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01