google的ImmutableList和Collections.unmodifyingList()有什么区别?
来自 ImmutableList javadocs:
不像 Collections.unmodifierList(java.util.List), 这是一个单独的视图 仍然可以改变的集合, ImmutableList 的实例包含其 拥有私人数据并且永远不会 改变。 ImmutableList 很方便 对于公共静态最终列表 (“常量列表”)并且还可以让您 轻松制作一个“防御性副本” 由以下人员向您的班级提供的列表 来电者。
这是否意味着:
- 如果我有 Dimension 对象的 ImmutableList(例如),那么我无法更改其中的任何 Dimension 对象?
- 如果我有 Dimension 对象的 Collections.unmodifyingList (列表),那么我不仅可以添加或删除任何对象,而且可以更改它们(例如调用 setDimension(width, height) 方法)?
From ImmutableList javadocs:
Unlike
Collections.unmodifiableList(java.util.List),
which is a view of a separate
collection that can still change, an
instance of ImmutableList contains its
own private data and will never
change. ImmutableList is convenient
for public static final lists
("constant lists") and also lets you
easily make a "defensive copy" of a
list provided to your class by a
caller.
Does it mean that:
- if I have ImmutableList of Dimension objects (for example) then I can't change any Dimension object in it?
- and if I have Collections.unmodifiableList (list) of Dimension objects then I can't only add or delete any object but I can change them (for example call setDimension(width, height) method)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,不变性仅适用于
Collection
中对象的数量和引用,并不解决放入Collection
中的对象的可变性。与标准 JDK
Collections.unmodifyingList
相比,Immutable 列表的优势在于,通过使用ImmutableList
,您可以保证引用的对象、它们的顺序和列表的大小不能从任何位置更改。来源。使用Collections.unmodifyingList
,如果其他内容引用了基础列表,则即使您引用了不可修改的列表,该代码也可以修改该列表。但是,如果您想要真正的不变性,则必须用不可变对象填充列表。
No, the immutability is only applied to the amount and references of the objects in the
Collection
, and does not address the mutability of objects you put in theCollection
.What Immutable list gains over the standard JDK
Collections.unmodifiableList
is that by usingImmutableList
you are guaranteed that the objects referenced, their order and the size of the list cannot change from any source. WithCollections.unmodifiableList
if something else has a reference to the underlying list, that code can modify the list even though you have a reference to an unmodifiable list.If, however, you want true immutability, you have to fill the list with immutable objects.
使用
Collections.unmodifyingList
创建一个围绕您的列表的包装器。如果底层列表发生变化,您的 unmodifyingList 的视图也会发生变化。正如文档所说,谷歌的代码创建了一个副本。这是一个更昂贵的计算并消耗更多内存,但如果有人更改原始列表,它不会影响 ImmutableList。
这些都不会阻止您更改列表中的对象、或其字段、或字段的字段等。
Using
Collections.unmodifiableList
creates a wrapper around your List. if the underlying list changes, so does your unmodifiableList's view.As the documentation says, Google's code creates a copy. It's a more expensive computation and consumes more memory, but if someone alters the original list, it cant affect the ImmutableList.
Neither of these will prevent you from changing an object in a list, or it's fields, or fields of fields, etc.
ImmutableList
类似于Collections.unmodifyingList( new ArrayList( list ) )
。请注意,新创建的ArrayList
未分配给字段或变量。ImmutableList
is similar toCollections.unmodifiableList( new ArrayList( list ) )
. Note that the newly createdArrayList
is not assigned to a field or variable.