RFR: 8180352: Add Stream.toList() method [v4]
Paul Sandoz
psandoz at openjdk.java.net
Wed Nov 25 19:23:59 UTC 2020
On Tue, 24 Nov 2020 07:10:14 GMT, Stuart Marks <smarks at openjdk.org> wrote:
>> This change introduces a new terminal operation on Stream. This looks like a convenience method for Stream.collect(Collectors.toList()) or Stream.collect(Collectors.toUnmodifiableList()), but it's not. Having this method directly on Stream enables it to do what can't easily by done by a Collector. In particular, it allows the stream to deposit results directly into a destination array (even in parallel) and have this array be wrapped in an unmodifiable List without copying.
>>
>> In the past we've kept most things from the Collections Framework as implementations of Collector, not directly on Stream, whereas only fundamental things (like toArray) appear directly on Stream. This is true of most Collections, but it does seem that List is special. It can be a thin wrapper around an array; it can handle generics better than arrays; and unlike an array, it can be made unmodifiable (shallowly immutable); and it can be value-based. See John Rose's comments in the bug report:
>>
>> https://bugs.openjdk.java.net/browse/JDK-8180352?focusedCommentId=14133065&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14133065
>>
>> This operation is null-tolerant, which matches the rest of Streams. This isn't specified, though; a general statement about null handling in Streams is probably warranted at some point.
>>
>> Finally, this method is indeed quite convenient (if the caller can deal with what this operation returns), as collecting into a List is the most common stream terminal operation.
>
> Stuart Marks has updated the pull request incrementally with one additional commit since the last revision:
>
> Add comment and assert regarding array class; use switch expression.
Recommend you review the problem list of this source file in IntelliJ, which shows some potential cleanups. This can be done as a follow on PR if appropriate, as can any improvements to the default implementation.
src/java.base/share/classes/java/util/ImmutableCollections.java line 163:
> 161: *
> 162: * @param <E> the List's element type
> 163: * @param input the input array
Rogue parameter declaration.
-------------
Marked as reviewed by psandoz (Reviewer).
PR: https://git.openjdk.java.net/jdk/pull/1026
More information about the core-libs-dev
mailing list