不可变的番石榴的缺陷? [英] Defects of Immutable collections of Guava?

查看:192
本文介绍了不可变的番石榴的缺陷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我理解的不变集合的缺陷是正确的,所以我列出他们在这个答案。希望有人在这里更正。

I am not sure the defects of Immutable collections I understand is correct, so I list them in this answer. Hope someone corrects me here.

a):与Collections.unmodifiableXXX()相比,ImmutableXXX.copyOf()丢失了源集合功能。例如,当一个linkedList被放入ImmutableList.copyOf()时,ImmutableList不再被链接。与基于树的集合相同。

a): Comparing to Collections.unmodifiableXXX(), ImmutableXXX.copyOf() loses the source collection feature. For example, when a linkedList is put into ImmutableList.copyOf(), the ImmutableList is not linked anymore. Same as Tree based collection.

b):人们认为Collections.unmodifiableXXX只使用源集合的相同引用,所以一旦源集合被更改,Collections.unmodifiableXXX也是改变。但我的解决方案是将源集合包装到临时集合,并传递给ImmutableXXX.copyOf()。请参阅下面的代码:

b): People think Collections.unmodifiableXXX just uses same reference of source collection, so once the source collection is changed, Collections.unmodifiableXXX is also changed. But my solution is to wrap a source collection into a temp collection which is passed to ImmutableXXX.copyOf(). See code below:

List<String> l = new ArrayList<String>();
List<String>  unmodifiableList = Collections.unmodifiableList(l);
ImmutableList<String> immutableList= ImmutableList.copyOf(l);
l.add("a");//unmodifiableList is also added "a", immutableList not.

/*My solution as follows:
So unmodifiableList2 is also immutable as ImmutableList.copyOf(l) does*/
List<String> unmodifiableList2= Collections.unmodifiableList(new ArrayList(l));

您对Immutable集合的理解是什么?谢谢!

What's your understanding of Immutable collection? Thanks!

推荐答案

没有任何内容是缺陷。

a)根本不重要的是, ImmutableList 不再是链接列表。与基于数组的列表相比,链接列表的唯一优点包括添加和删除元素(主要删除)。您不能添加到不可变列表中或从不可变列表中删除,因此基于数组的更适合快速随机访问以及内存效率。

a) It doesn't matter at all that an ImmutableList is no longer a linked list. The only advantages of a linked list over an array-based list involve adding and removing elements (removing primarily). You can't add to or remove from an immutable list, so array-based is preferable for its fast random access as well as memory efficiency.

对于<$


  1. 正常 ImmutableSet 保留它给出的元素的迭代顺序。因此,如果你有一个 TreeSet 并使用 ImmutableSet.copyOf 创建一个不可变的副本, c> c> c> c> 并像 TreeSet 一样使用元素的自然排序或 Comparator

  1. A normal ImmutableSet preserves the iteration order of the elements it's given. So if you have a TreeSet and use ImmutableSet.copyOf to create an immutable copy, the copied elements will be ordered the same as in the original.
  2. ImmutableSortedSet is the immutable equivalent of TreeSet and uses the natural ordering of elements or a Comparator just like TreeSet does.

b)您可以创建一个列表使用Guava不会改变任何东西。 Guava的不可变集合专门设计了具有不变性的,并且它们具有各种优点,因为包括(但不限于):

b) The fact that you can create a List that happens to be immutable without using Guava doesn't change anything. Guava's immutable collections are designed specifically with immutability in mind and they have various advantages because of that, including (but not limited to):


  • 事实上,他们的不变性在类型级别是保证的,正如我在回答你最后一个问题时提到的。当你的方法返回类型 ImmutableSet 的东西时,调用者知道该集合不能改变它们。

  • 内存优化,包括针对空元素和单元素情况的特殊类。
  • 如果只是返回 Set 如果输入已经是相同类型的不可变实例,
  • ImmutableSet.copyOf 等实际上不会复制任何内容。

  • 方法/构建器,以便轻松创建不可变集合。

  • The fact that their immutability is guaranteed at the type level, as I mentioned in my answer to your last question. When your method returns something of type ImmutableSet, the caller knows that set can't change on them. Not so if it just returns Set.
  • Memory optimizations, including special classes for empty and 1-element cases.
  • ImmutableSet.copyOf etc. don't actually copy anything if the input is already an an immutable instance of the same type.
  • Methods/builders to make it easy to create immutable collections.

这篇关于不可变的番石榴的缺陷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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