UnmodifiableMap(Java 集合)与 ImmutableMap(Google)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22636575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UnmodifiableMap (Java Collections) vs ImmutableMap (Google)
提问by John Humphreys - w00te
Context
语境
I need to return a reference to a map that I'm using for a data cache, and I'd like to make sure nobody can modify their reference.
我需要返回对我用于数据缓存的地图的引用,并且我想确保没有人可以修改他们的引用。
Question
题
I've seen lots of references to UnmodifiableMap and ImmutableMap online, but I don't see anything comparing/contrasting them. I figure there is a good reason that Google/Guava created their own version - can someone tell me what it is?
我在网上看到了很多对 UnmodifiableMap 和 ImmutableMap 的引用,但我没有看到任何比较/对比它们的内容。我认为 Google/Guava 创建自己的版本是有充分理由的 - 有人可以告诉我它是什么吗?
采纳答案by Marco13
An unmodifiable map may still change. It is only a viewon a modifiable map, and changes in the backing map will be visible through the unmodifiable map. The unmodifiable map only prevents modifications for those who only have the reference to the unmodifiable view:
不可修改的地图可能仍会更改。它只是可修改地图上的一个视图,通过不可修改的地图可以看到后备地图的变化。不可修改的映射仅防止对那些只有不可修改视图的引用的人进行修改:
Map<String, String> realMap = new HashMap<String, String>();
realMap.put("A", "B");
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(realMap);
// This is not possible: It would throw an
// UnsupportedOperationException
//unmodifiableMap.put("C", "D");
// This is still possible:
realMap.put("E", "F");
// The change in the "realMap" is now also visible
// in the "unmodifiableMap". So the unmodifiableMap
// has changed after it has been created.
unmodifiableMap.get("E"); // Will return "F".
In contrast to that, the ImmutableMap of Guava is really immutable: It is a true copyof a given map, and nobody may modify this ImmutableMap in any way.
相比之下,Guava 的 ImmutableMap 真的是不可变的:它是给定地图的真实副本,没有人可以以任何方式修改这个 ImmutableMap。
Update:
更新:
As pointed out in a comment, an immutable map can also be created with the standard API using
正如评论中所指出的,也可以使用标准 API 创建不可变映射,使用
Map<String, String> immutableMap =
Collections.unmodifiableMap(new LinkedHashMap<String, String>(realMap));
This will create an unmodifiable view on a true copy of the given map, and thus nicely emulates the characteristics of the ImmutableMap
without having to add the dependency to Guava.
这将在给定地图的真实副本上创建一个不可修改的视图,从而很好地模拟 的特性,而ImmutableMap
无需向 Guava 添加依赖项。
回答by Eric Stein
The JDK provides
Collections.unmodifiableXXX
methods, but in our opinion, these can be unwieldy and verbose; unpleasant to use everywhere you want to make defensive copies unsafe: the returned collections are only truly immutable if nobody holds a reference to the original collection inefficient: the data structures still have all the overhead of mutable collections, including concurrent modification checks, extra space in hash tables, etc.
JDK 提供了
Collections.unmodifiableXXX
方法,但在我们看来,这些方法既笨拙又冗长;在任何你想让防御性副本不安全的地方使用令人不快:如果没有人持有对原始集合的引用,返回的集合才是真正不可变的 低效:数据结构仍然具有可变集合的所有开销,包括并发修改检查、额外的空间哈希表等
回答by Jakub H
Have a look at ImmutableMap JavaDoc: doc
看看 ImmutableMap JavaDoc: doc
There is information about that there:
有关于那里的信息:
Unlike Collections.unmodifiableMap(java.util.Map), which is a view of a separate map which can still change, an instance of ImmutableMap contains its own data and will never change. ImmutableMap is convenient for public static final maps ("constant maps") and also lets you easily make a "defensive copy" of a map provided to your class by a caller.
与 Collections.unmodifiableMap(java.util.Map) 不同,它是一个仍然可以改变的单独地图的视图,ImmutableMap 的实例包含它自己的数据并且永远不会改变。ImmutableMap 对于公共静态最终映射(“常量映射”)很方便,还可以让您轻松制作由调用者提供给您的类的映射的“防御性副本”。
回答by Harmlezz
ImmutableMap does not accept null
values whereas Collections.unmodifiableMap()
does. In addition it will never change after construction, while UnmodifiableMap
may. From the JavaDoc:
ImmutableMap 不接受null
值,而接受Collections.unmodifiableMap()
。此外,它在建造后永远不会改变,而UnmodifiableMap
可能。来自 JavaDoc:
An immutable, hash-based Map with reliable user-specified iteration order. Does not permit null keys or values.
Unlike Collections.unmodifiableMap(java.util.Map), which is a view of a separate map which can still change, an instance of ImmutableMap contains its own data and will never change. ImmutableMap is convenient for public static final maps ("constant maps") and also lets you easily make a "defensive copy" of a map provided to your class by a caller.
一个不可变的、基于哈希的 Map,具有可靠的用户指定的迭代顺序。不允许空键或值。
与 Collections.unmodifiableMap(java.util.Map) 不同,它是一个仍然可以改变的单独地图的视图,ImmutableMap 的实例包含它自己的数据并且永远不会改变。ImmutableMap 对于公共静态最终映射(“常量映射”)很方便,还可以让您轻松制作由调用者提供给您的类的映射的“防御性副本”。