简体   繁体   English

如何从java HashMap获取不可变集合?

[英]How to get a immutable collection from java HashMap?

I need to get a collection from the java HashMap without the changes in the map reflecting in the collection later . 我需要从java HashMap中获取一个集合,而不会在以后的集合中反映地图中的更改。 I wanted to use Collection.toArray() to achieve this , but its not working . 我想使用Collection.toArray()来实现这一点,但它不起作用。 The resultant Object[] is also changing (javadocs say that The returned array will be "safe" in that no references to it are maintained by this collection ) . 结果Object []也在改变(javadocs说返回的数组将是“安全的”,因为这个集合没有维护对它的引用)。 Any simple way to achieve this? 有任何简单的方法来实现这一目

Its not possible to do this with a single API call, you need to utilize deep cloning. 使用单个API调用无法实现此功能,您需要利用深度克隆。 Clones won't be changed when you make changes to the original. 更改原始文件时,不会更改克隆。 This topic has been discussed on SO before, see How to clone ArrayList and also clone its contents? 此主题之前已在SO上讨论过,请参阅如何克隆ArrayList并克隆其内容? and Deep clone utility recomendation . 深度克隆实用程序推荐

您可以创建不可修改的地图

The following is still valid and describes the issue observed (see the bit on what sort of copy is performed). 以下内容仍然有效并描述了观察到的问题(请参阅执行何种副本的位)。 I do not, however, provide any answer on how to perform a deep-copy. 但是,我不提供有关如何执行深层复制的任何答案。

Instead, my suggestion , is to design the objects to be immutable , which avoids this issue entirely. 相反, 我的建议是将对象设计为不可变的 ,这完全避免了这个问题。 In my experience this works really well for most trivial objects (that is, objects which are not "containers") and can simplify code and reasoning about it. 根据我的经验,这对于大多数琐碎的对象(即不是“容器”的对象)非常有效,并且可以简化代码和推理。


From the Javadoc for HashMap.values : 从Javadoc for HashMap.values

Returns a Collection view of the values contained in this map. 返回此映射中包含的值的Collection视图。 The collection is backed by the map , so changes to the map are reflected in the collection , and vice-versa... 该集合由地图支持 ,因此对地图的 更改将反映在集合中 ,反之亦然......

Perhaps it would be beneficial to create a copy of it? 也许创建它的副本会有好处吗?

HashMap<K,V> map = ....;
List<V> values = new ArrayList<V>(map.values());

That, in essence, performs a shallow-copy that is "now separate". 实质上,这是执行“现在分开”的浅拷贝 However, being a shallow copy , no clone/copy of the contained objects is performed . 但是, 作为浅拷贝 ,不执行所包含对象的克隆/拷贝 (See βнɛƨн Ǥʋяʋиɢ's answer if deep-copy semantics are desired: the question itself seems a little vague on the matter.) (如果需要深拷贝语义,请参阅答案:问题本身在这个问题上看起来有点模糊。)

Happy coding 快乐的编码

Create a shallow copy of the original map whith HashMap.clone() , then retrieve the values colleation from that. 使用HashMap.clone HashMap.clone()创建原始映射的浅表副本,然后从中检索值收集。 This will create new references to the objects in the original map at the time of the clone() call; 这将在clone()调用时创建对原始映射中对象的新引用; obviously changes inside the contained objects themselves will still be reflected in the copied collection. 显然,包含的对象内部的更改仍将反映在复制的集合中。 If you want this behaviour, your only way is to hard copy the map objects in your desired collection. 如果您想要这种行为,唯一的方法是在所需的集合中硬拷贝地图对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM