Guava 的不可变集合的缺陷?

发布于 2024-10-31 15:03:19 字数 880 浏览 1 评论 0原文

我不确定我所理解的不可变集合的缺陷是否正确,所以我在这个答案中列出了它们。希望有人在这里纠正我。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2024-11-07 15:03:19

你提到的一切都不是“缺陷”。

a) ImmutableList 不再是一个链表,这根本不重要。与基于数组的列表相比,链表的唯一优点是添加和删除元素(主要是删除)。您无法在不可变列表中添加或删除,因此基于数组的方法因其快速随机访问和内存效率而更可取。

对于像 TreeSet 这样的东西,有很多要点需要考虑。

  1. 普通的 ImmutableSet 会保留给定元素的迭代顺序。因此,如果您有一个 TreeSet 并使用 ImmutableSet.copyOf 创建不可变副本,则复制元素的顺序将与原始元素的顺序相同。
  2. ImmutableSortedSetTreeSet 的不可变等价物,它使用元素的自然排序或 Comparator,就像 TreeSet 一样。

b) 事实上,您可以在不使用 Guava 的情况下创建一个不可变的 List,这一事实不会改变任何内容。 Guava 的不可变集合在设计时专门考虑了不可变性,因此它们具有各种优点,包括(但不限于):

  • 事实上,它们的不可变性在类型级别得到保证,正如我在我对你最后一个问题的回答。当您的方法返回 ImmutableSet 类型的内容时,调用者知道该集合无法对其进行更改。如果它只返回Set,则情况并非如此。
  • 内存优化,包括空情况的单例和 1 元素情况的特殊类。
  • 如果输入已经是相同类型的不可变实例,则 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.

  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) 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):

  • 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 singletons for empty cases and special classes for 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.
沧笙踏歌 2024-11-07 15:03:19

为什么我们需要不可变集合

  • 它极大地简化了并发编程。思考
    关于这一点,为什么编写正确的多线程编程很困难?因为
    很难同步线程对给定资源(在本例中为列表)的访问。

why we need immutable collections

  • It dramatically simplifies concurrent programming. Think
    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).
口干舌燥 2024-11-07 15:03:19

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文