UnmodifiableMap(Java Collections)vs ImmutableMap(Google) [英] UnmodifiableMap (Java Collections) vs ImmutableMap (Google)

查看:514
本文介绍了UnmodifiableMap(Java Collections)vs ImmutableMap(Google)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文



我需要返回对我用于数据缓存的地图的引用,



看到大量的UnmodifiableMap和ImmutableMap在线,但我没有看到任何比较/对比他们。我认为有一个很好的理由,Google / Guava创建了自己的版本 - 有人可以告诉我它是什么?

解决方案

不可修改的地图可能仍然改变。它只是可修改地图上的查看,并且后备地图中的更改将通过不可修改的地图可见。不可修改的映射仅防止对那些仅具有对不可修改视图的引用的修改:

  Map< String,String> realMap = new HashMap< String,String>(); 
realMap.put(A,B);

Map< String,String> unmodifiableMap = Collections.unmodifiableMap(realMap);

//这是不可能的:它会抛出一个
// UnsupportedOperationException
//unmodifiableMap.put(\"C,D);

//这仍然是可能的:
realMap.put(E,F);

//在unmodifiableMap中,realMap的变化现在也是可见的
//。所以unmodifiableMap
//在创建后发生了变化。
unmodifiableMap.get(E); //将返回F。

与此相反,Guava的ImmutableMap真的是不可变的::是给定地图的真实 ,任何人都不能以任何方式修改此ImmutableMap。



更新



正如 comment ,也可以使用标准API创建不可变的地图,使用

  Map< String,String> immutableMap = 
Collections.unmodifiableMap(new LinkedHashMap< String,String>(realMap));

这将在给定地图的真实副本上创建一个不可修改的视图, ImmutableMap ,而无需向Guava添加依赖项。


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?

解决方案

An unmodifiable map may still change. It is only a view on 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 copy of a given map, and nobody may modify this ImmutableMap in any way.

Update:

As pointed out in a comment, an immutable map can also be created with the standard API using

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.

这篇关于UnmodifiableMap(Java Collections)vs ImmutableMap(Google)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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