Guava 的 Collections.unmodifyingSet() 和 ImmutableSet 有什么区别?
ImmutableSet
的 JavaDoc 说:
与
Collections.unmodifyingSet
(它是仍然可以更改的单独集合的视图)不同,此类的实例包含其自己的私有数据并且永远不会更改。此类对于公共静态最终集(“常量集”)很方便,并且还可以让您轻松地为调用者提供给您的类的集创建“防御性副本”。
但是 ImmutableSet
仍然存储元素的引用,我无法弄清楚与 Collections.unmodifyingSet()
的区别。示例:
StringBuffer s=new StringBuffer("a");
ImmutableSet<StringBuffer> set= ImmutableSet.of(s);
s.append("b");//s is "ab", s is still changed here!
谁能解释一下吗?
JavaDoc of ImmutableSet
says:
Unlike
Collections.unmodifiableSet
, which is a view of a separate collection that can still change, an instance of this class contains its own private data and will never change. This class is convenient for public static final sets ("constant sets") and also lets you easily make a "defensive copy" of a set provided to your class by a caller.
But the ImmutableSet
still stores reference of elements, I couldn't figure out the difference to Collections.unmodifiableSet()
. Sample:
StringBuffer s=new StringBuffer("a");
ImmutableSet<StringBuffer> set= ImmutableSet.of(s);
s.append("b");//s is "ab", s is still changed here!
Could anyone explain it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
考虑一下:
换句话说,
ImmutableSet
是不可变的,无论它是根据可能发生变化的任何集合构建的 - 因为它创建了一个副本。Collections.unmodifyingSet
防止返回的集合被直接更改,但它仍然是可能更改的支持集的视图。请注意,如果您开始更改任何集合引用的对象的内容,那么无论如何,所有的赌注都会被取消。不要那样做。事实上,首先使用可变元素类型创建集合并不是一个好主意。 (同上,使用可变键类型进行映射。)
Consider this:
In other words,
ImmutableSet
is immutable despite whatever collection it's built from potentially changing - because it creates a copy.Collections.unmodifiableSet
prevents the returned collection from being directly changed, but it's still a view on a potentially-changing backing set.Note that if you start changing the contents of the objects referred to by any set, all bets are off anyway. Don't do that. Indeed, it's rarely a good idea to create a set using a mutable element type in the first place. (Ditto maps using a mutable key type.)
除了 Jon 提到的行为差异之外,
ImmutableSet
和Collections.unmodifyingSet
创建的Set
之间的一个重要区别是ImmutableSet
code> 是一个类型。您可以通过在整个代码中使用ImmutableSet
而不是Set
来传递一个集合,并清楚地表明该集合是不可变的。使用Collections.unmodifyingSet
,返回的类型只是Set
...所以很明显,集合在创建时是不可修改的,除非您在任何地方添加Javadoc传递Set
说“这个集合是不可修改的”。Besides the behavioral difference that Jon mentions, an important difference between
ImmutableSet
and theSet
created byCollections.unmodifiableSet
is thatImmutableSet
is a type. You can pass one around and have it remain clear that the set is immutable by usingImmutableSet
rather thanSet
throughout the code. WithCollections.unmodifiableSet
, the returned type is justSet
... so it's only clear that the set is unmodifiable at the point where it is created unless you add Javadoc everywhere you pass thatSet
saying "this set is unmodifiable".Kevin Bourrillion(Guava 首席开发人员)在本演示文稿。虽然该演示文稿已有两年历史,并且重点关注“Google Collections”(现在是 Guava 的子部分),但这是一个非常有趣的演示文稿。 API 可能有所改变(Google Collections API 当时处于 Beta 阶段),但 Google Collections / Guava 背后的概念仍然有效。
您可能还对这个其他问题 (Google 的 ImmutableList 和 Collections.unmodifyingList() 之间有什么区别)。
Kevin Bourrillion (Guava lead developer) compares immutable / unmodifiable collections in this presentation. While the presentation is two years old, and focuses on "Google Collections" (which is now a subpart of Guava), this is a very interesting presentation. The API may have changed here and there (the Google Collections API was in Beta at the time), but the concepts behind Google Collections / Guava are still valid.
You might also be interested in this other SO question ( What is the difference between google's ImmutableList and Collections.unmodifiableList() ).
其他答案中未说明的两者之间的区别是
ImmutableSet
不允许null
值,如 Javadoc(同样的限制适用于所有 Guava 不可变集合中的值。)
例如:
所有这些都在运行时失败。相比之下:
这很好。
A difference between the two not stated in other answers is that
ImmutableSet
does not permitnull
values, as described in the Javadoc(The same restriction applies to values in all Guava immutable collections.)
For example:
All of these fail at runtime. In contrast:
This is fine.