java流在创建不可变列表时是否会创建一些临时列表?

由于不能将任何元素添加到不可变列表中,我认为 java 流首先将元素收集到一个列表中,然后使用第一个列表中的元素创建一个新的不可变列表。因此,列表有两个实例,第一个实例可用于垃圾收集。

所以,我的问题是

  1. 如上所述,流是否实际上创建了两个列表对象?

  2. 如果不是,流如何创建不可变列表?


慕森王
浏览 87回答 2
2回答

料青山看我应如是

考虑以下示例:List<String> people&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= getPeople().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(collectingAndThen(toList(), Collections::unmodifiableList));对于这个例子,我使用的是Collections::unmodifiableList方法,所以让我们检查源代码:/**&nbsp;* Returns an unmodifiable view of the specified list.&nbsp; This method allows&nbsp;* modules to provide users with "read-only" access to internal&nbsp;* lists.&nbsp; Query operations on the returned list "read through" to the&nbsp;* specified list, and attempts to modify the returned list, whether&nbsp;* direct or via its iterator, result in an&nbsp;* <tt>UnsupportedOperationException</tt>.<p>&nbsp;*&nbsp;* The returned list will be serializable if the specified list&nbsp;* is serializable. Similarly, the returned list will implement&nbsp;* {@link RandomAccess} if the specified list does.&nbsp;*&nbsp;* @param&nbsp; list the list for which an unmodifiable view is to be returned.&nbsp;* @return an unmodifiable view of the specified list.&nbsp;*/public static <T> List<T> unmodifiableList(List<? extends T> list) {&nbsp; &nbsp; return (list instanceof RandomAccess ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new UnmodifiableRandomAccessList<>(list) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new UnmodifiableList<>(list));}正如@Pshemo 在评论中提到的那样,它UnmodifiableList可以作为列表的包装器,您还可以在源代码中检查该类包含一个列表:&nbsp;static class UnmodifiableList<E> extends UnmodifiableCollection<E>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;implements List<E> {&nbsp; &nbsp; &nbsp;private static final long serialVersionUID = -283967356065247728L;&nbsp; &nbsp; &nbsp;final List<? extends E> list; // Here is the wrapped list&nbsp; &nbsp; &nbsp;UnmodifiableList(List<? extends E> list) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super(list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.list = list;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; ...}可以在此处找到用于提取这些代码的源代码。所以回答你的问题:流使用方法等Collections::unmodifiableList方法创建不可变列表内部流不会在不同的列表中添加任何内容,因为它ImmutableList只是作为包装器工作Collection您还可以查看文档和来源,以了解这些不可变相关方法和对象的工作原理。

牛魔王的故事

任何实现都会以某种方式将元素累积到具有某种程度的可变性的结构中,然后返回一个无法修改的列表。如何完成的细节取决于实现,但这里有几种可能性:元素被累积到一个ArrayList中,然后被复制到一个不可变列表中。元素被累积到一个ArrayList中,并返回一个防止修改的包装器(例如Collections.unmodifiableList。)由于没有其他对象引用原始ArrayList的 ,因此结果是不可变的。这些元素被累积到一些技术上不是列表的结构中,例如原始数组,并且该数组被复制或包装在不可变的列表对象中。选择这些实现中的哪一个取决于Collector您调用的特定对象,例如Collectors.toList()或ImmutableList.toImmutableList()。该实现的细节取决于该库的作者,他们可以使用任何这些策略。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java