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

时间:2022-05-20 14:53:32

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)不同,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. 如果我有Dimension对象的ImmutableList(例如)那么我不能更改其中的任何Dimension对象?
  3. 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)?
  4. 如果我有Dimension对象的Collections.unmodifiableList(列表),那么我不仅可以添加或删除任何对象,但我可以更改它们(例如调用setDimension(width,height)方法)?

5 个解决方案

#1


52  

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.

不,不变性仅适用于Collection中对象的数量和引用,并不解决您在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获得的是通过使用ImmutableList保证引用的对象,它们的顺序和列表大小不能从任何源更改。使用Collections.unmodifiableList,如果其他内容具有对基础列表的引用,即使您引用了不可修改的列表,该代码也可以修改列表。

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

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

#2


15  

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

使用Collections.unmodifiableList在List周围创建一个包装器。如果基础列表发生变化,那么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.

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

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

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

#3


6  

ImmutableList is similar to Collections.unmodifiableList( new ArrayList( list ) ) . Note that the newly created ArrayList is not assigned to a field or variable.

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

#4


3  

  1. No, the contained individual objects can still be modified. A collection is really only storing references to the contained objects, not a full copy of every object.
  2. 不,仍然可以修改包含的单个对象。集合实际上只存储对包含对象的引用,而不是每个对象的完整副本。
  3. 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.
  4. 您可以通过修改名为Collections.unmodifiableList(list)的父集合来修改列表。但是,您可以使用setDimension来更改存储的列表元素。

#5


0  

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

您可能还想看看这个问题(Collections.unmodifiableSet()和Guava的ImmutableSet之间有什么区别)。

#1


52  

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.

不,不变性仅适用于Collection中对象的数量和引用,并不解决您在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获得的是通过使用ImmutableList保证引用的对象,它们的顺序和列表大小不能从任何源更改。使用Collections.unmodifiableList,如果其他内容具有对基础列表的引用,即使您引用了不可修改的列表,该代码也可以修改列表。

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

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

#2


15  

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

使用Collections.unmodifiableList在List周围创建一个包装器。如果基础列表发生变化,那么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.

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

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

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

#3


6  

ImmutableList is similar to Collections.unmodifiableList( new ArrayList( list ) ) . Note that the newly created ArrayList is not assigned to a field or variable.

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

#4


3  

  1. No, the contained individual objects can still be modified. A collection is really only storing references to the contained objects, not a full copy of every object.
  2. 不,仍然可以修改包含的单个对象。集合实际上只存储对包含对象的引用,而不是每个对象的完整副本。
  3. 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.
  4. 您可以通过修改名为Collections.unmodifiableList(list)的父集合来修改列表。但是,您可以使用setDimension来更改存储的列表元素。

#5


0  

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

您可能还想看看这个问题(Collections.unmodifiableSet()和Guava的ImmutableSet之间有什么区别)。