Java google 的 ImmutableList 和 Collections.unmodifiableList() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2185789/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:31:08  来源:igfitidea点击:

What is the difference between google's ImmutableList and Collections.unmodifiableList ()?

javacollectionscomparisonguava

提问by Roman

From ImmutableList javadocs:

来自 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.

与 Collections.unmodifiableList(java.util.List) 不同,后者是一个仍然可以更改的单独集合的视图,ImmutableList 的实例包含自己的私有数据并且永远不会更改。ImmutableList 对于公共静态最终列表(“常量列表”)很方便,还可以让您轻松制作由调用者提供给您的类的列表的“防御性副本”。

Does it mean that:

是否意味着:

  1. if I have ImmutableList of Dimension objects (for example) then I can't change any Dimension object in it?
  2. 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)?
  1. 如果我有 Dimension 对象的 ImmutableList(例如),那么我不能更改其中的任何 Dimension 对象?
  2. 如果我有 Dimension 对象的 Collections.unmodifiableList(列表),那么我不仅可以添加或删除任何对象,还可以更改它们(例如调用 setDimension(width, height) 方法)?

采纳答案by Yishai

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 the Collection.

不,不变性仅适用于集合中对象的数量和引用,并没有解决您放入集合中的对象的可变性。

What Immutable list gains over the standard JDK Collections.unmodifiableList is that by using ImmutableList you are guaranteed that the objects referenced, their order and the size of the list cannot change from any source. With Collections.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.

与标准 JDK Collections.unmodifiableList 相比,Immutable 列表的优势在于,通过使用 ImmutableList,您可以保证引用的对象、它们的顺序和列表的大小不会因任何来源而改变。使用 Collections.unmodifiableList 如果其他东西引用了底层列表,即使您引用了不可修改的列表,该代码也可以修改列表。

If, however, you want true immutability, you have to fill the list with immutable objects.

但是,如果您想要真正的不变性,则必须用不可变对象填充列表。

回答by z5h

Using Collections.unmodifiableListcreates a wrapper around your List. if the underlying list changes, so does your unmodifiableList's view.

使用Collections.unmodifiableList在您的列表周围创建一个包装器。如果基础列表发生变化,您的 unmodifiableList 视图也会发生变化。

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.

正如文档所说,谷歌的代码会创建一个副本。这是一个更昂贵的计算并消耗更多内存,但如果有人更改原始列表,它不会影响 ImmutableList。

Neither of these will prevent you from changing an object in a list, or it's fields, or fields of fields, etc.

这些都不会阻止您更改列表中的对象,或者它的字段,或字段的字段等。

回答by BobMcGee

  1. No, the contained individual objects can still be modified. A collection is really only storing referencesto the contained objects, not a full copy of every object.
  2. You can modify the list by modifying the parent Collection you called Collections.unmodifiableList(list) on. But yes, you CAN use setDimension to change a stored list element.
  1. 不,包含的单个对象仍然可以修改。集合实际上只存储对所包含对象的引用,而不是每个对象的完整副本。
  2. 您可以通过修改您在其上调用 Collections.unmodifiableList(list) 的父 Collection 来修改列表。但是是的,您可以使用 setDimension 来更改存储的列表元素。

回答by KitsuneYMG

ImmutableListis similar to Collections.unmodifiableList( new ArrayList( list ) ). Note that the newly created ArrayListis notassigned to a field or variable.

ImmutableList类似于Collections.unmodifiableList( new ArrayList( list ) ). 请注意,新创建ArrayList分配给字段或变量。

回答by Bartek

You might also want to take a look at this question(What's the difference between Collections.unmodifiableSet()and ImmutableSetof Guava).

您可能还想看看这个问题(GuavaCollections.unmodifiableSet()ImmutableSetGuava之间有什么区别)。