Java HashMap - 深拷贝 [英] Java HashMap - deep copy

查看:27
本文介绍了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 - 深拷贝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆