mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8180352: Add Stream.toList() method
Reviewed-by: psandoz
This commit is contained in:
parent
89690699b2
commit
41dbc139ab
8 changed files with 403 additions and 87 deletions
|
@ -1162,6 +1162,40 @@ public interface Stream<T> extends BaseStream<T, Stream<T>> {
|
|||
*/
|
||||
<R, A> R collect(Collector<? super T, A, R> collector);
|
||||
|
||||
/**
|
||||
* 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="../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())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the minimum element of this stream according to the provided
|
||||
* {@code Comparator}. This is a special case of a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue