Class Streams
Stream
instances.- Since:
- 21.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
An analogue ofDoubleFunction
also accepting an index.static interface
An analogue ofFunction
also accepting an index.static interface
An analogue ofIntFunction
also accepting an index.static interface
An analogue ofLongFunction
also accepting an index.private static class
Streams.MapWithIndexSpliterator<F extends Spliterator<?>,
R, S extends Streams.MapWithIndexSpliterator<F, R, S>> private static class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
closeAll
(BaseStream<?, ?>[] toClose) static DoubleStream
concat
(DoubleStream... streams) Returns aDoubleStream
containing the elements of the first stream, followed by the elements of the second stream, and so on.static IntStream
Returns anIntStream
containing the elements of the first stream, followed by the elements of the second stream, and so on.static LongStream
concat
(LongStream... streams) Returns aLongStream
containing the elements of the first stream, followed by the elements of the second stream, and so on.static <T> Stream
<T> Returns aStream
containing the elements of the first stream, followed by the elements of the second stream, and so on.static OptionalDouble
findLast
(DoubleStream stream) Returns the last element of the specified stream, orOptionalDouble.empty()
if the stream is empty.static OptionalInt
Returns the last element of the specified stream, orOptionalInt.empty()
if the stream is empty.static OptionalLong
findLast
(LongStream stream) Returns the last element of the specified stream, orOptionalLong.empty()
if the stream is empty.static <T> Optional
<T> Returns the last element of the specified stream, orOptional.empty()
if the stream is empty.static <A,
B> void forEachPair
(Stream<A> streamA, Stream<B> streamB, BiConsumer<? super A, ? super B> consumer) Invokesconsumer
once for each pair of corresponding elements instreamA
andstreamB
.static <R> Stream
<R> mapWithIndex
(DoubleStream stream, Streams.DoubleFunctionWithIndex<R> function) Returns a stream consisting of the results of applying the given function to the elements ofstream
and their indexes in the stream.static <R> Stream
<R> mapWithIndex
(IntStream stream, Streams.IntFunctionWithIndex<R> function) Returns a stream consisting of the results of applying the given function to the elements ofstream
and their indexes in the stream.static <R> Stream
<R> mapWithIndex
(LongStream stream, Streams.LongFunctionWithIndex<R> function) Returns a stream consisting of the results of applying the given function to the elements ofstream
and their indexes in the stream.static <T,
R> Stream <R> mapWithIndex
(Stream<T> stream, Streams.FunctionWithIndex<? super T, ? extends R> function) Returns a stream consisting of the results of applying the given function to the elements ofstream
and their indices in the stream.private static void
Throws an undeclared checked exception.static <T> Stream
<T> If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.static <T> Stream
<T> Returns a sequentialStream
of the contents ofiterable
, delegating toCollection.stream()
if possible.static <T> Stream
<T> stream
(Collection<T> collection) Deprecated.static <T> Stream
<T> Returns a sequentialStream
of the remaining contents ofiterator
.static <T> Stream
<T> If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.static DoubleStream
stream
(OptionalDouble optional) If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.static IntStream
stream
(OptionalInt optional) If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.static LongStream
stream
(OptionalLong optional) If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.static <A,
B, R> Stream <R> zip
(Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, R> function) Returns a stream in which each element is the result of passing the corresponding element of each ofstreamA
andstreamB
tofunction
.
-
Constructor Details
-
Streams
private Streams()
-
-
Method Details
-
stream
Returns a sequentialStream
of the contents ofiterable
, delegating toCollection.stream()
if possible. -
stream
Deprecated.There is no reason to use this; just invokecollection.stream()
directly.ReturnsCollection.stream()
. -
stream
Returns a sequentialStream
of the remaining contents ofiterator
. Do not useiterator
directly after passing it to this method. -
stream
If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream. -
stream
If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.Java 9 users: use
optional.stream()
instead. -
stream
If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.Java 9 users: use
optional.stream()
instead. -
stream
If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.Java 9 users: use
optional.stream()
instead. -
stream
If a value is present inoptional
, returns a stream containing only that element, otherwise returns an empty stream.Java 9 users: use
optional.stream()
instead. -
closeAll
-
sneakyThrow
Throws an undeclared checked exception. -
concat
Returns aStream
containing the elements of the first stream, followed by the elements of the second stream, and so on.This is equivalent to
Stream.of(streams).flatMap(stream -> stream)
, but the returned stream may perform better.- See Also:
-
concat
Returns anIntStream
containing the elements of the first stream, followed by the elements of the second stream, and so on.This is equivalent to
Stream.of(streams).flatMapToInt(stream -> stream)
, but the returned stream may perform better.- See Also:
-
concat
Returns aLongStream
containing the elements of the first stream, followed by the elements of the second stream, and so on.This is equivalent to
Stream.of(streams).flatMapToLong(stream -> stream)
, but the returned stream may perform better.- See Also:
-
concat
Returns aDoubleStream
containing the elements of the first stream, followed by the elements of the second stream, and so on.This is equivalent to
Stream.of(streams).flatMapToDouble(stream -> stream)
, but the returned stream may perform better.- See Also:
-
zip
public static <A,B, Stream<R> zipR> (Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, R> function) Returns a stream in which each element is the result of passing the corresponding element of each ofstreamA
andstreamB
tofunction
.For example:
Streams.zip( Stream.of("foo1", "foo2", "foo3"), Stream.of("bar1", "bar2"), (arg1, arg2) -> arg1 + ":" + arg2)
will return
Stream.of("foo1:bar1", "foo2:bar2")
.The resulting stream will only be as long as the shorter of the two input streams; if one stream is longer, its extra elements will be ignored.
Note that if you are calling
Stream.forEach(java.util.function.Consumer<? super T>)
on the resulting stream, you might want to consider usingforEachPair(java.util.stream.Stream<A>, java.util.stream.Stream<B>, java.util.function.BiConsumer<? super A, ? super B>)
instead of this method.Performance note: The resulting stream is not efficiently splittable. This may harm parallel performance.
-
forEachPair
public static <A,B> void forEachPair(Stream<A> streamA, Stream<B> streamB, BiConsumer<? super A, ? super B> consumer) Invokesconsumer
once for each pair of corresponding elements instreamA
andstreamB
. If one stream is longer than the other, the extra elements are silently ignored. Elements passed to the consumer are guaranteed to come from the same position in their respective source streams. For example:Streams.forEachPair( Stream.of("foo1", "foo2", "foo3"), Stream.of("bar1", "bar2"), (arg1, arg2) -> System.out.println(arg1 + ":" + arg2)
will print:
foo1:bar1 foo2:bar2
Warning: If either supplied stream is a parallel stream, the same correspondence between elements will be made, but the order in which those pairs of elements are passed to the consumer is not defined.
Note that many usages of this method can be replaced with simpler calls to
zip(java.util.stream.Stream<A>, java.util.stream.Stream<B>, java.util.function.BiFunction<? super A, ? super B, R>)
. This method behaves equivalently to zipping the stream elements into temporary pair objects and then usingStream.forEach(java.util.function.Consumer<? super T>)
on that stream.- Since:
- 22.0
-
mapWithIndex
public static <T,R> Stream<R> mapWithIndex(Stream<T> stream, Streams.FunctionWithIndex<? super T, ? extends R> function) Returns a stream consisting of the results of applying the given function to the elements ofstream
and their indices in the stream. For example,mapWithIndex( Stream.of("a", "b", "c"), (e, index) -> index + ":" + e)
would return
Stream.of("0:a", "1:b", "2:c")
.The resulting stream is efficiently splittable if and only if
stream
was efficiently splittable and its underlying spliterator reportedSpliterator.SUBSIZED
. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.The order of the resulting stream is defined if and only if the order of the original stream was defined.
-
mapWithIndex
public static <R> Stream<R> mapWithIndex(IntStream stream, Streams.IntFunctionWithIndex<R> function) Returns a stream consisting of the results of applying the given function to the elements ofstream
and their indexes in the stream. For example,mapWithIndex( IntStream.of(10, 11, 12), (e, index) -> index + ":" + e)
...would return
Stream.of("0:10", "1:11", "2:12")
.The resulting stream is efficiently splittable if and only if
stream
was efficiently splittable and its underlying spliterator reportedSpliterator.SUBSIZED
. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.The order of the resulting stream is defined if and only if the order of the original stream was defined.
-
mapWithIndex
public static <R> Stream<R> mapWithIndex(LongStream stream, Streams.LongFunctionWithIndex<R> function) Returns a stream consisting of the results of applying the given function to the elements ofstream
and their indexes in the stream. For example,mapWithIndex( LongStream.of(10, 11, 12), (e, index) -> index + ":" + e)
...would return
Stream.of("0:10", "1:11", "2:12")
.The resulting stream is efficiently splittable if and only if
stream
was efficiently splittable and its underlying spliterator reportedSpliterator.SUBSIZED
. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.The order of the resulting stream is defined if and only if the order of the original stream was defined.
-
mapWithIndex
public static <R> Stream<R> mapWithIndex(DoubleStream stream, Streams.DoubleFunctionWithIndex<R> function) Returns a stream consisting of the results of applying the given function to the elements ofstream
and their indexes in the stream. For example,mapWithIndex( DoubleStream.of(0.0, 1.0, 2.0) (e, index) -> index + ":" + e)
...would return
Stream.of("0:0.0", "1:1.0", "2:2.0")
.The resulting stream is efficiently splittable if and only if
stream
was efficiently splittable and its underlying spliterator reportedSpliterator.SUBSIZED
. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.The order of the resulting stream is defined if and only if the order of the original stream was defined.
-
findLast
Returns the last element of the specified stream, orOptional.empty()
if the stream is empty.Equivalent to
stream.reduce((a, b) -> b)
, but may perform significantly better. This method's runtime will be between O(log n) and O(n), performing better on efficiently splittable streams.If the stream has nondeterministic order, this has equivalent semantics to
Stream.findAny()
(which you might as well use).- Throws:
NullPointerException
- if the last element of the stream is null- See Also:
-
findLast
Returns the last element of the specified stream, orOptionalInt.empty()
if the stream is empty.Equivalent to
stream.reduce((a, b) -> b)
, but may perform significantly better. This method's runtime will be between O(log n) and O(n), performing better on efficiently splittable streams.- Throws:
NullPointerException
- if the last element of the stream is null- See Also:
-
findLast
Returns the last element of the specified stream, orOptionalLong.empty()
if the stream is empty.Equivalent to
stream.reduce((a, b) -> b)
, but may perform significantly better. This method's runtime will be between O(log n) and O(n), performing better on efficiently splittable streams.- Throws:
NullPointerException
- if the last element of the stream is null- See Also:
-
findLast
Returns the last element of the specified stream, orOptionalDouble.empty()
if the stream is empty.Equivalent to
stream.reduce((a, b) -> b)
, but may perform significantly better. This method's runtime will be between O(log n) and O(n), performing better on efficiently splittable streams.- Throws:
NullPointerException
- if the last element of the stream is null- See Also:
-
collection.stream()
directly.