T
- The type of the stream elements.public class ParallelStreamSupport<T> extends Object implements Stream<T>
Stream
which uses a custom ForkJoinPool
for parallel aggregate operations.
The following example illustrates an aggregate operation using ParallelStreamSupport
with a custom
ForkJoinPool
:
ForkJoinPool pool = new ForkJoinPool(); int sum = ParallelStreamSupport.parallelStream(widgets, pool) .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight()) .sum();
In case this stream is configured for parallel execution, i.e. isParallel()
returns true
, a
terminal
operation will be executed as ForkJoinTask
in the custom ForkJoinPool
. Otherwise it will be
executed in the calling thread.
Stream
, which are meaningful for parallel streamsCollection.parallelStream()
Arrays.stream(Object[])
StreamSupport.stream(Spliterator, boolean)
StreamSupport.stream(Supplier, int, boolean)
Internally, this stream wraps a stream which is initially created in one of the static factory methods. Whenever a
non-terminal operation is called the underlying stream will be replaced with the result of calling the same method
on that stream. The return value of these operations is always this stream or, in case of operations that return a
different type of stream, one of ParallelIntStreamSupport
, ParallelLongStreamSupport
or
ParallelDoubleStreamSupport
.
Although each factory method returns a parallel stream, calling sequential()
is still possible and leads to
sequential execution of a terminal operation within the calling thread.
See the class documentation for Stream
and the package documentation for
java.util.stream for
additional specification.
Stream.Builder<T>
Modifier and Type | Method and Description |
---|---|
boolean |
allMatch(Predicate<? super T> predicate)
Returns whether all elements of this stream match the provided predicate.
|
boolean |
anyMatch(Predicate<? super T> predicate)
Returns whether any elements of this stream match the provided
predicate.
|
void |
close()
Closes this stream, causing all close handlers for this stream pipeline
to be called.
|
<R,A> R |
collect(Collector<? super T,A,R> collector)
Performs a mutable
reduction operation on the elements of this stream using a
Collector . |
<R> R |
collect(Supplier<R> supplier,
BiConsumer<R,? super T> accumulator,
BiConsumer<R,R> combiner)
Performs a mutable
reduction operation on the elements of this stream.
|
static <T> Stream<T> |
concat(Stream<? extends T> a,
Stream<? extends T> b,
ForkJoinPool workerPool)
Creates a lazily concatenated parallel stream whose elements are all the elements of the first
stream followed by all the elements of the second stream.
|
long |
count()
Returns the count of elements in this stream.
|
Stream<T> |
distinct()
Returns a stream consisting of the distinct elements (according to
Object.equals(Object) ) of this stream. |
protected <R> R |
execute(Callable<R> terminalOperation) |
protected void |
execute(Runnable terminalOperation) |
Stream<T> |
filter(Predicate<? super T> predicate)
Returns a stream consisting of the elements of this stream that match
the given predicate.
|
Optional<T> |
findAny()
Returns an
Optional describing some element of the stream, or an
empty Optional if the stream is empty. |
Optional<T> |
findFirst()
Returns an
Optional describing the first element of this stream,
or an empty Optional if the stream is empty. |
<R> Stream<R> |
flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
DoubleStream |
flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
Returns an
DoubleStream consisting of the results of replacing
each element of this stream with the contents of a mapped stream produced
by applying the provided mapping function to each element. |
IntStream |
flatMapToInt(Function<? super T,? extends IntStream> mapper)
Returns an
IntStream consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. |
LongStream |
flatMapToLong(Function<? super T,? extends LongStream> mapper)
Returns an
LongStream consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. |
void |
forEach(Consumer<? super T> action)
Performs an action for each element of this stream.
|
void |
forEachOrdered(Consumer<? super T> action)
Performs an action for each element of this stream, in the encounter
order of the stream if the stream has a defined encounter order.
|
static <T> Stream<T> |
generate(Supplier<T> supplier,
ForkJoinPool workerPool)
Creates a parallel infinite sequential unordered stream where each element is generated by the
provided
Supplier . |
boolean |
isParallel()
Returns whether this stream, if a terminal operation were to be executed,
would execute in parallel.
|
static <T> Stream<T> |
iterate(T seed,
UnaryOperator<T> operator,
ForkJoinPool workerPool)
Creates a parallel infinite ordered stream produced by iterative application of a function
f to an initial element seed . |
Iterator<T> |
iterator()
Returns an iterator for the elements of this stream.
|
Stream<T> |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
<R> Stream<R> |
map(Function<? super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
|
DoubleStream |
mapToDouble(ToDoubleFunction<? super T> mapper)
Returns a
DoubleStream consisting of the results of applying the
given function to the elements of this stream. |
IntStream |
mapToInt(ToIntFunction<? super T> mapper)
Returns an
IntStream consisting of the results of applying the
given function to the elements of this stream. |
LongStream |
mapToLong(ToLongFunction<? super T> mapper)
Returns a
LongStream consisting of the results of applying the
given function to the elements of this stream. |
Optional<T> |
max(Comparator<? super T> comparator)
Returns the maximum element of this stream according to the provided
Comparator . |
Optional<T> |
min(Comparator<? super T> comparator)
Returns the minimum element of this stream according to the provided
Comparator . |
boolean |
noneMatch(Predicate<? super T> predicate)
Returns whether no elements of this stream match the provided predicate.
|
S |
onClose(Runnable closeHandler)
Returns an equivalent stream with an additional close handler.
|
S |
parallel()
Returns an equivalent stream that is parallel.
|
static <T> Stream<T> |
parallelStream(Collection<T> collection,
ForkJoinPool workerPool)
Creates a parallel stream from the given Collection.
|
static <T> Stream<T> |
parallelStream(Spliterator<T> spliterator,
ForkJoinPool workerPool)
Creates a parallel stream from the given Spliterator.
|
static <T> Stream<T> |
parallelStream(Stream.Builder<T> builder,
ForkJoinPool workerPool)
Creates a parallel stream from the given
Builder . |
static <T> Stream<T> |
parallelStream(Supplier<? extends Spliterator<T>> supplier,
int characteristics,
ForkJoinPool workerPool)
Creates a parallel stream from the given Spliterator supplier.
|
static <T> Stream<T> |
parallelStream(T[] array,
ForkJoinPool workerPool)
Creates a parallel stream from the given Array.
|
Stream<T> |
peek(Consumer<? super T> action)
Returns a stream consisting of the elements of this stream, additionally
performing the provided action on each element as elements are consumed
from the resulting stream.
|
Optional<T> |
reduce(BinaryOperator<T> accumulator)
Performs a reduction on the
elements of this stream, using an
associative accumulation
function, and returns an
Optional describing the reduced value,
if any. |
T |
reduce(T identity,
BinaryOperator<T> accumulator)
Performs a reduction on the
elements of this stream, using the provided identity value and an
associative
accumulation function, and returns the reduced value.
|
<U> U |
reduce(U identity,
BiFunction<U,? super T,U> accumulator,
BinaryOperator<U> combiner)
Performs a reduction on the
elements of this stream, using the provided identity, accumulation and
combining functions.
|
S |
sequential()
Returns an equivalent stream that is sequential.
|
Stream<T> |
skip(long n)
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n elements of the stream. |
Stream<T> |
sorted()
Returns a stream consisting of the elements of this stream, sorted
according to natural order.
|
Stream<T> |
sorted(Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, sorted
according to the provided
Comparator . |
Spliterator<T> |
spliterator()
Returns a spliterator for the elements of this stream.
|
Object[] |
toArray()
Returns an array containing the elements of this stream.
|
<A> A[] |
toArray(IntFunction<A[]> generator)
Returns an array containing the elements of this stream, using the
provided
generator function to allocate the returned array, as
well as any additional arrays that might be required for a partitioned
execution or for resizing. |
S |
unordered()
Returns an equivalent stream that is
unordered.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
builder, concat, empty, generate, iterate, of, of
close, isParallel, iterator, onClose, parallel, sequential, spliterator, unordered
public static <T> Stream<T> parallelStream(Collection<T> collection, ForkJoinPool workerPool)
Collection.parallelStream()
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.T
- The type of stream elements.collection
- Collection to create the parallel stream from. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.ForkJoinPool
.Collection.parallelStream()
public static <T> Stream<T> parallelStream(T[] array, ForkJoinPool workerPool)
Arrays.stream(array).parallel()
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.T
- The type of stream elements.array
- Array to create the parallel stream from. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.ForkJoinPool
.Arrays.stream(Object[])
public static <T> Stream<T> parallelStream(Spliterator<T> spliterator, ForkJoinPool workerPool)
StreamSupport.stream(spliterator, true)
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.T
- The type of stream elements.spliterator
- A Spliterator
describing the stream elements. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.ForkJoinPool
.StreamSupport.stream(Spliterator, boolean)
public static <T> Stream<T> parallelStream(Supplier<? extends Spliterator<T>> supplier, int characteristics, ForkJoinPool workerPool)
StreamSupport.stream(supplier, characteristics, true)
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.T
- The type of stream elements.supplier
- A Supplier
of a Spliterator
. Must not be null
.characteristics
- Spliterator characteristics of the supplied Spliterator
. The characteristics must
be equal to supplier.get().characteristics()
, otherwise undefined behavior may occur when terminal
operation commences.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.ForkJoinPool
.StreamSupport.stream(Supplier, int, boolean)
public static <T> Stream<T> parallelStream(Stream.Builder<T> builder, ForkJoinPool workerPool)
Builder
. This operation is similar to calling
builder.build().parallel()
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.T
- The type of stream elements.builder
- The builder to create the stream from. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.ForkJoinPool
.Stream.builder()
public static <T> Stream<T> iterate(T seed, UnaryOperator<T> operator, ForkJoinPool workerPool)
f
to an initial element seed
. This operation is similar to calling Stream.iterate(seed,
operator).parallel()
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.T
- The type of stream elements.seed
- The initial element.operator
- A function to be applied to to the previous element to produce a new element. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.ForkJoinPool
.Stream.iterate(Object, UnaryOperator)
public static <T> Stream<T> generate(Supplier<T> supplier, ForkJoinPool workerPool)
Supplier
. This operation is similar to calling Stream.generate(supplier).parallel()
with
the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.T
- The type of stream elements.supplier
- The Supplier
of generated elements. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.ForkJoinPool
.Stream.generate(Supplier)
public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b, ForkJoinPool workerPool)
Stream.concat(a, b).parallel()
with the difference that a parallel
terminal
operation will be executed in the given ForkJoinPool
.T
- The type of stream elements.a
- The first stream. Must not be null
.b
- The second stream. Must not be null
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not be null
.ForkJoinPool
.Stream.concat(Stream, Stream)
public Stream<T> filter(Predicate<? super T> predicate)
java.util.stream.Stream
This is an intermediate operation.
filter
in interface Stream<T>
predicate
- a non-interfering,
stateless
predicate to apply to each element to determine if it
should be includedpublic <R> Stream<R> map(Function<? super T,? extends R> mapper)
java.util.stream.Stream
This is an intermediate operation.
map
in interface Stream<T>
R
- The element type of the new streammapper
- a non-interfering,
stateless
function to apply to each elementpublic IntStream mapToInt(ToIntFunction<? super T> mapper)
java.util.stream.Stream
IntStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToInt
in interface Stream<T>
mapper
- a non-interfering,
stateless
function to apply to each elementpublic LongStream mapToLong(ToLongFunction<? super T> mapper)
java.util.stream.Stream
LongStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToLong
in interface Stream<T>
mapper
- a non-interfering,
stateless
function to apply to each elementpublic DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
java.util.stream.Stream
DoubleStream
consisting of the results of applying the
given function to the elements of this stream.
This is an intermediate operation.
mapToDouble
in interface Stream<T>
mapper
- a non-interfering,
stateless
function to apply to each elementpublic <R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
java.util.stream.Stream
closed
after its contents
have been placed into this stream. (If a mapped stream is null
an empty stream is used, instead.)
This is an intermediate operation.
flatMap
in interface Stream<T>
R
- The element type of the new streammapper
- a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuespublic IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)
java.util.stream.Stream
IntStream
consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. Each mapped
stream is closed
after its
contents have been placed into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToInt
in interface Stream<T>
mapper
- a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)
public LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)
java.util.stream.Stream
LongStream
consisting of the results of replacing each
element of this stream with the contents of a mapped stream produced by
applying the provided mapping function to each element. Each mapped
stream is closed
after its
contents have been placed into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToLong
in interface Stream<T>
mapper
- a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)
public DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
java.util.stream.Stream
DoubleStream
consisting of the results of replacing
each element of this stream with the contents of a mapped stream produced
by applying the provided mapping function to each element. Each mapped
stream is closed
after its
contents have placed been into this stream. (If a mapped stream is
null
an empty stream is used, instead.)
This is an intermediate operation.
flatMapToDouble
in interface Stream<T>
mapper
- a non-interfering,
stateless
function to apply to each element which produces a stream
of new valuesStream.flatMap(Function)
public Stream<T> distinct()
java.util.stream.Stream
Object.equals(Object)
) of this stream.
For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
public Stream<T> sorted()
java.util.stream.Stream
Comparable
, a java.lang.ClassCastException
may be thrown
when the terminal operation is executed.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
public Stream<T> sorted(Comparator<? super T> comparator)
java.util.stream.Stream
Comparator
.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
sorted
in interface Stream<T>
comparator
- a non-interfering,
stateless
Comparator
to be used to compare stream elementspublic Stream<T> peek(Consumer<? super T> action)
java.util.stream.Stream
This is an intermediate operation.
For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization.
peek
in interface Stream<T>
action
- a
non-interfering action to perform on the elements as
they are consumed from the streampublic Stream<T> limit(long maxSize)
java.util.stream.Stream
maxSize
in length.
public Stream<T> skip(long n)
java.util.stream.Stream
n
elements of the stream.
If this stream contains fewer than n
elements then an
empty stream will be returned.
This is a stateful intermediate operation.
public void forEach(Consumer<? super T> action)
java.util.stream.Stream
This is a terminal operation.
The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.
forEach
in interface Stream<T>
action
- a
non-interfering action to perform on the elementspublic void forEachOrdered(Consumer<? super T> action)
java.util.stream.Stream
This is a terminal operation.
This operation processes the elements one at a time, in encounter order if one exists. Performing the action for one element happens-before performing the action for subsequent elements, but for any given element, the action may be performed in whatever thread the library chooses.
forEachOrdered
in interface Stream<T>
action
- a
non-interfering action to perform on the elementsStream.forEach(Consumer)
public Object[] toArray()
java.util.stream.Stream
This is a terminal operation.
public <A> A[] toArray(IntFunction<A[]> generator)
java.util.stream.Stream
generator
function to allocate the returned array, as
well as any additional arrays that might be required for a partitioned
execution or for resizing.
This is a terminal operation.
public T reduce(T identity, BinaryOperator<T> accumulator)
java.util.stream.Stream
T result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity
value must be an identity for the accumulator
function. This means that for all t
,
accumulator.apply(identity, t)
is equal to t
.
The accumulator
function must be an
associative function.
This is a terminal operation.
reduce
in interface Stream<T>
identity
- the identity value for the accumulating functionaccumulator
- an associative,
non-interfering,
stateless
function for combining two valuespublic Optional<T> reduce(BinaryOperator<T> accumulator)
java.util.stream.Stream
Optional
describing the reduced value,
if any. This is equivalent to:
boolean foundAny = false;
T result = null;
for (T element : this stream) {
if (!foundAny) {
foundAny = true;
result = element;
}
else
result = accumulator.apply(result, element);
}
return foundAny ? Optional.of(result) : Optional.empty();
but is not constrained to execute sequentially.
The accumulator
function must be an
associative function.
This is a terminal operation.
reduce
in interface Stream<T>
accumulator
- an associative,
non-interfering,
stateless
function for combining two valuesOptional
describing the result of the reductionStream.reduce(Object, BinaryOperator)
,
Stream.min(Comparator)
,
Stream.max(Comparator)
public <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
java.util.stream.Stream
U result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
but is not constrained to execute sequentially.
The identity
value must be an identity for the combiner
function. This means that for all u
, combiner(identity, u)
is equal to u
. Additionally, the combiner
function
must be compatible with the accumulator
function; for all
u
and t
, the following must hold:
combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
This is a terminal operation.
reduce
in interface Stream<T>
U
- The type of the resultidentity
- the identity value for the combiner functionaccumulator
- an associative,
non-interfering,
stateless
function for incorporating an additional element into a resultcombiner
- an associative,
non-interfering,
stateless
function for combining two values, which must be
compatible with the accumulator functionStream.reduce(BinaryOperator)
,
Stream.reduce(Object, BinaryOperator)
public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
java.util.stream.Stream
ArrayList
, and elements are incorporated by updating
the state of the result rather than by replacing the result. This
produces a result equivalent to:
R result = supplier.get();
for (T element : this stream)
accumulator.accept(result, element);
return result;
Like Stream.reduce(Object, BinaryOperator)
, collect
operations
can be parallelized without requiring additional synchronization.
This is a terminal operation.
collect
in interface Stream<T>
R
- type of the resultsupplier
- a function that creates a new result container. For a
parallel execution, this function may be called
multiple times and must return a fresh value each time.accumulator
- an associative,
non-interfering,
stateless
function for incorporating an additional element into a resultcombiner
- an associative,
non-interfering,
stateless
function for combining two values, which must be
compatible with the accumulator functionpublic <R,A> R collect(Collector<? super T,A,R> collector)
java.util.stream.Stream
Collector
. A Collector
encapsulates the functions used as arguments to
Stream.collect(Supplier, BiConsumer, BiConsumer)
, allowing for reuse of
collection strategies and composition of collect operations such as
multiple-level grouping or partitioning.
If the stream is parallel, and the Collector
is concurrent
, and
either the stream is unordered or the collector is
unordered
,
then a concurrent reduction will be performed (see Collector
for
details on concurrent reduction.)
This is a terminal operation.
When executed in parallel, multiple intermediate results may be
instantiated, populated, and merged so as to maintain isolation of
mutable data structures. Therefore, even when executed in parallel
with non-thread-safe data structures (such as ArrayList
), no
additional synchronization is needed for a parallel reduction.
collect
in interface Stream<T>
R
- the type of the resultA
- the intermediate accumulation type of the Collector
collector
- the Collector
describing the reductionStream.collect(Supplier, BiConsumer, BiConsumer)
,
Collectors
public Optional<T> min(Comparator<? super T> comparator)
java.util.stream.Stream
Comparator
. This is a special case of a
reduction.
This is a terminal operation.
min
in interface Stream<T>
comparator
- a non-interfering,
stateless
Comparator
to compare elements of this streamOptional
describing the minimum element of this stream,
or an empty Optional
if the stream is emptypublic Optional<T> max(Comparator<? super T> comparator)
java.util.stream.Stream
Comparator
. This is a special case of a
reduction.
This is a terminal operation.
max
in interface Stream<T>
comparator
- a non-interfering,
stateless
Comparator
to compare elements of this streamOptional
describing the maximum element of this stream,
or an empty Optional
if the stream is emptypublic long count()
java.util.stream.Stream
return mapToLong(e -> 1L).sum();
This is a terminal operation.
public boolean anyMatch(Predicate<? super T> predicate)
java.util.stream.Stream
false
is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
anyMatch
in interface Stream<T>
predicate
- a non-interfering,
stateless
predicate to apply to elements of this streamtrue
if any elements of the stream match the provided
predicate, otherwise false
public boolean allMatch(Predicate<? super T> predicate)
java.util.stream.Stream
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
allMatch
in interface Stream<T>
predicate
- a non-interfering,
stateless
predicate to apply to elements of this streamtrue
if either all elements of the stream match the
provided predicate or the stream is empty, otherwise false
public boolean noneMatch(Predicate<? super T> predicate)
java.util.stream.Stream
true
is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
noneMatch
in interface Stream<T>
predicate
- a non-interfering,
stateless
predicate to apply to elements of this streamtrue
if either no elements of the stream match the
provided predicate or the stream is empty, otherwise false
public Optional<T> findFirst()
java.util.stream.Stream
Optional
describing the first element of this stream,
or an empty Optional
if the stream is empty. If the stream has
no encounter order, then any element may be returned.
This is a short-circuiting terminal operation.
public Optional<T> findAny()
java.util.stream.Stream
Optional
describing some element of the stream, or an
empty Optional
if the stream is empty.
This is a short-circuiting terminal operation.
The behavior of this operation is explicitly nondeterministic; it is
free to select any element in the stream. This is to allow for maximal
performance in parallel operations; the cost is that multiple invocations
on the same source may not return the same result. (If a stable result
is desired, use Stream.findFirst()
instead.)
findAny
in interface Stream<T>
Optional
describing some element of this stream, or an
empty Optional
if the stream is emptyStream.findFirst()
public boolean isParallel()
java.util.stream.BaseStream
isParallel
in interface BaseStream<T,S extends BaseStream<T,S>>
true
if this stream would execute in parallel if executedpublic Iterator<T> iterator()
java.util.stream.BaseStream
This is a terminal operation.
iterator
in interface BaseStream<T,S extends BaseStream<T,S>>
public Spliterator<T> spliterator()
java.util.stream.BaseStream
This is a terminal operation.
spliterator
in interface BaseStream<T,S extends BaseStream<T,S>>
public S sequential()
java.util.stream.BaseStream
This is an intermediate operation.
sequential
in interface BaseStream<T,S extends BaseStream<T,S>>
public S parallel()
java.util.stream.BaseStream
This is an intermediate operation.
parallel
in interface BaseStream<T,S extends BaseStream<T,S>>
public S unordered()
java.util.stream.BaseStream
This is an intermediate operation.
unordered
in interface BaseStream<T,S extends BaseStream<T,S>>
public S onClose(Runnable closeHandler)
java.util.stream.BaseStream
BaseStream.close()
method
is called on the stream, and are executed in the order they were
added. All close handlers are run, even if earlier close handlers throw
exceptions. If any close handler throws an exception, the first
exception thrown will be relayed to the caller of close()
, with
any remaining exceptions added to that exception as suppressed exceptions
(unless one of the remaining exceptions is the same exception as the
first exception, since an exception cannot suppress itself.) May
return itself.
This is an intermediate operation.
onClose
in interface BaseStream<T,S extends BaseStream<T,S>>
closeHandler
- A task to execute when the stream is closedpublic void close()
java.util.stream.BaseStream
close
in interface AutoCloseable
close
in interface BaseStream<T,S extends BaseStream<T,S>>
AutoCloseable.close()
protected void execute(Runnable terminalOperation)
protected <R> R execute(Callable<R> terminalOperation)
Copyright © 2019. All rights reserved.