为什么 Stream#toList 的默认实现看起来过于复杂/次优?
Why does Stream#toList's default implementation seem overcomplicated / suboptimal?
查看 Stream#toList
的实施,我只是注意到它看起来过于复杂和次优。
就像上面的 javadoc 中提到的那样,这个 default
实现没有被大多数 Stream
实现使用,但是,我认为它可能不是这样。
来源
/**
* Accumulates the elements of this stream into a {@code List}. The elements in
* the list will be in this stream's encounter order, if one exists. The returned List
* is unmodifiable; calls to any mutator method will always cause
* {@code UnsupportedOperationException} to be thrown. There are no
* guarantees on the implementation type or serializability of the returned List.
*
* <p>The returned instance may be <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>.
* Callers should make no assumptions about the identity of the returned instances.
* Identity-sensitive operations on these instances (reference equality ({@code ==}),
* identity hash code, and synchronization) are unreliable and should be avoided.
*
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
*
* @apiNote If more control over the returned object is required, use
* {@link Collectors#toCollection(Supplier)}.
*
* @implSpec The implementation in this interface returns a List produced as if by the following:
* <pre>{@code
* Collections.unmodifiableList(new ArrayList<>(Arrays.asList(this.toArray())))
* }</pre>
*
* @implNote Most instances of Stream will override this method and provide an implementation
* that is highly optimized compared to the implementation in this interface.
*
* @return a List containing the stream elements
*
* @since 16
*/
@SuppressWarnings("unchecked")
default List<T> toList() {
return (List<T>) Collections.unmodifiableList(new ArrayList<>(Arrays.asList(this.toArray())));
}
我认为什么会更好
return (List<T>) Collections.unmodifiableList(Arrays.asList(this.toArray()));
甚至
return Arrays.asList(this.toArray()));
IntelliJ 的提议
return (List<T>) List.of(this.toArray());
在 JDK 来源中实施是否有充分的理由?
toArray
方法可以实现为 return 一个数组,该数组随后会发生变异,这将有效地使 returned 列表不是不可变的。这就是为什么通过创建新 ArrayList
完成显式复制的原因。
它本质上是一个防御副本。
这也在 review of this API 期间进行了讨论,其中 Stuart Marks 写道:
As written it's true that the default implementation does perform apparently redundant copies, but we can't be assured that toArray() actually returns a freshly created array. Thus, we wrap it using Arrays.asList and then copy it using the ArrayList constructor. This is unfortunate but necessary to avoid situations where someone could hold a reference to the internal array of a List, allowing modification of a List that's supposed to be unmodifiable.
查看 Stream#toList
的实施,我只是注意到它看起来过于复杂和次优。
就像上面的 javadoc 中提到的那样,这个 default
实现没有被大多数 Stream
实现使用,但是,我认为它可能不是这样。
来源
/**
* Accumulates the elements of this stream into a {@code List}. The elements in
* the list will be in this stream's encounter order, if one exists. The returned List
* is unmodifiable; calls to any mutator method will always cause
* {@code UnsupportedOperationException} to be thrown. There are no
* guarantees on the implementation type or serializability of the returned List.
*
* <p>The returned instance may be <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>.
* Callers should make no assumptions about the identity of the returned instances.
* Identity-sensitive operations on these instances (reference equality ({@code ==}),
* identity hash code, and synchronization) are unreliable and should be avoided.
*
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
*
* @apiNote If more control over the returned object is required, use
* {@link Collectors#toCollection(Supplier)}.
*
* @implSpec The implementation in this interface returns a List produced as if by the following:
* <pre>{@code
* Collections.unmodifiableList(new ArrayList<>(Arrays.asList(this.toArray())))
* }</pre>
*
* @implNote Most instances of Stream will override this method and provide an implementation
* that is highly optimized compared to the implementation in this interface.
*
* @return a List containing the stream elements
*
* @since 16
*/
@SuppressWarnings("unchecked")
default List<T> toList() {
return (List<T>) Collections.unmodifiableList(new ArrayList<>(Arrays.asList(this.toArray())));
}
我认为什么会更好
return (List<T>) Collections.unmodifiableList(Arrays.asList(this.toArray()));
甚至
return Arrays.asList(this.toArray()));
IntelliJ 的提议
return (List<T>) List.of(this.toArray());
在 JDK 来源中实施是否有充分的理由?
toArray
方法可以实现为 return 一个数组,该数组随后会发生变异,这将有效地使 returned 列表不是不可变的。这就是为什么通过创建新 ArrayList
完成显式复制的原因。
它本质上是一个防御副本。
这也在 review of this API 期间进行了讨论,其中 Stuart Marks 写道:
As written it's true that the default implementation does perform apparently redundant copies, but we can't be assured that toArray() actually returns a freshly created array. Thus, we wrap it using Arrays.asList and then copy it using the ArrayList constructor. This is unfortunate but necessary to avoid situations where someone could hold a reference to the internal array of a List, allowing modification of a List that's supposed to be unmodifiable.