Guava 的不可变集合的缺陷?
我不确定我所理解的不可变集合的缺陷是否正确,所以我在这个答案中列出了它们。希望有人在这里纠正我。
a):与 Collections.unmodifyingXXX() 相比,ImmutableXXX.copyOf() 失去了源集合功能。例如,当将 linkedList 放入 ImmutableList.copyOf() 时,ImmutableList 不再链接。与基于树的集合相同。
b):人们认为 Collections.unmodifyingXXX 只是使用源集合的相同引用,因此一旦源集合更改,Collections.unmodifyingXXX 也会更改。但我的解决方案是将源集合包装到传递给 ImmutableXXX.copyOf() 的临时集合中。请参阅下面的代码:
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));
您对不可变集合的理解如何?谢谢!
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): 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): 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));
What's your understanding of Immutable collection? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你提到的一切都不是“缺陷”。
a)
ImmutableList
不再是一个链表,这根本不重要。与基于数组的列表相比,链表的唯一优点是添加和删除元素(主要是删除)。您无法在不可变列表中添加或删除,因此基于数组的方法因其快速随机访问和内存效率而更可取。对于像
TreeSet
这样的东西,有很多要点需要考虑。ImmutableSortedSet
是TreeSet
的不可变等价物,它使用元素的自然排序或Comparator
,就像TreeSet
一样。b) 事实上,您可以在不使用 Guava 的情况下创建一个不可变的
List
,这一事实不会改变任何内容。 Guava 的不可变集合在设计时专门考虑了不可变性,因此它们具有各种优点,包括(但不限于):ImmutableSet
类型的内容时,调用者知道该集合无法对其进行更改。如果它只返回Set
,则情况并非如此。ImmutableSet.copyOf
等实际上不会复制任何内容。Nothing you have mentioned is a "defect".
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.For something like
TreeSet
, there are a number points to consider.ImmutableSet
preserves the iteration order of the elements it's given. So if you have aTreeSet
and useImmutableSet.copyOf
to create an immutable copy, the copied elements will be ordered the same as in the original.ImmutableSortedSet
is the immutable equivalent ofTreeSet
and uses the natural ordering of elements or aComparator
just likeTreeSet
does.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
, the caller knows that set can't change on them. Not so if it just returnsSet
.ImmutableSet.copyOf
etc. don't actually copy anything if the input is already an an immutable instance of the same type.关于这一点,为什么编写正确的多线程编程很困难?因为
很难同步线程对给定资源(在本例中为列表)的访问。
about it, why is writing proper multithreaded programming hard? Because
it is hard to synchronize threads access to a given resource (in this case, the list).
ColinD 和 Amir 直接回答了您的具体问题,但您可能还想查看 GTUG -使用适用于 Java 的 Google Collections Library(第 1 部分,共 2 部分) - Kevin Bourrillion(Guava 的首席开发人员)关于不可变集合的演示,他在其中解释了不可变集合的所有优点。
虽然该演示文稿已有两年历史,并且重点关注“Google Collections”(现在是 Guava 的子部分),但这是一个非常有趣的演示文稿。自演示以来,API 可能发生了一些变化,因为 Google Collections API 当时处于 Beta 阶段,但大多数概念保持不变。
ColinD and Amir answered your specific questions directly, but you might also want to look at GTUG - Using the Google Collections Library for Java (1 of 2) - a presentation about immutable collections by Kevin Bourrillion (Guava's lead developer), where he explains all the advantages of immutable collections.
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 a little since the presentation, because the Google Collections API was in Beta at the time, but most of the concepts stayed the same.