Joh*_*0te 90 java collections map guava
上下文
我需要返回对我用于数据缓存的地图的引用,并且我想确保没有人可以修改它们的引用.
题
我在网上看过很多对UnmodifiableMap和ImmutableMap的引用,但我没有看到任何比较/对比它们的东西.我认为Google/Guava有一个很好的理由创建了他们自己的版本 - 有人可以告诉我它是什么吗?
Mar*_*o13 173
不可修改的地图可能仍会发生变化.它只是可修改地图上的视图,并且通过不可修改的地图可以看到支持地图中的更改.不可修改的映射仅阻止对仅具有对不可修改视图的引用的人的修改:
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".
Run Code Online (Sandbox Code Playgroud)
与此相反,Guava的ImmutableMap真的是不可变的:它是给定地图的真实副本,没有人可以以任何方式修改这个ImmutableMap.
更新:
正如评论中所指出的,也可以使用标准API创建不可变映射
Map<String, String> immutableMap =
Collections.unmodifiableMap(new LinkedHashMap<String, String>(realMap));
Run Code Online (Sandbox Code Playgroud)
这将在给定地图的真实副本上创建一个不可修改的视图,因此可以很好地模拟其特征ImmutableMap
而无需将依赖项添加到Guava.
归档时间: |
|
查看次数: |
111411 次 |
最近记录: |