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 - 深拷贝


基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01