Class Streams
- java.lang.Object
-
- com.google.common.collect.Streams
-
@GwtCompatible public final class Streams extends java.lang.Object
Static utility methods related toStreaminstances.- Since:
- 21.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceStreams.DoubleFunctionWithIndex<R>An analogue ofDoubleFunctionalso accepting an index.static interfaceStreams.FunctionWithIndex<T,R>An analogue ofFunctionalso accepting an index.static interfaceStreams.IntFunctionWithIndex<R>An analogue ofIntFunctionalso accepting an index.static interfaceStreams.LongFunctionWithIndex<R>An analogue ofLongFunctionalso accepting an index.private static classStreams.MapWithIndexSpliterator<F extends java.util.Spliterator<?>,R,S extends Streams.MapWithIndexSpliterator<F,R,S>>private static classStreams.TemporaryPair<A,B>
-
Constructor Summary
Constructors Modifier Constructor Description privateStreams()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static voidcloseAll(java.util.stream.BaseStream<?,?>[] toClose)static java.util.stream.DoubleStreamconcat(java.util.stream.DoubleStream... streams)Returns aDoubleStreamcontaining the elements of the first stream, followed by the elements of the second stream, and so on.static java.util.stream.IntStreamconcat(java.util.stream.IntStream... streams)Returns anIntStreamcontaining the elements of the first stream, followed by the elements of the second stream, and so on.static java.util.stream.LongStreamconcat(java.util.stream.LongStream... streams)Returns aLongStreamcontaining the elements of the first stream, followed by the elements of the second stream, and so on.static <T> java.util.stream.Stream<T>concat(java.util.stream.Stream<? extends T>... streams)Returns aStreamcontaining the elements of the first stream, followed by the elements of the second stream, and so on.static java.util.OptionalDoublefindLast(java.util.stream.DoubleStream stream)Returns the last element of the specified stream, orOptionalDouble.empty()if the stream is empty.static java.util.OptionalIntfindLast(java.util.stream.IntStream stream)Returns the last element of the specified stream, orOptionalInt.empty()if the stream is empty.static java.util.OptionalLongfindLast(java.util.stream.LongStream stream)Returns the last element of the specified stream, orOptionalLong.empty()if the stream is empty.static <T> java.util.Optional<T>findLast(java.util.stream.Stream<T> stream)Returns the last element of the specified stream, orOptional.empty()if the stream is empty.static <A,B>
voidforEachPair(java.util.stream.Stream<A> streamA, java.util.stream.Stream<B> streamB, java.util.function.BiConsumer<? super A,? super B> consumer)Invokesconsumeronce for each pair of corresponding elements instreamAandstreamB.static <R> java.util.stream.Stream<R>mapWithIndex(java.util.stream.DoubleStream stream, Streams.DoubleFunctionWithIndex<R> function)Returns a stream consisting of the results of applying the given function to the elements ofstreamand their indexes in the stream.static <R> java.util.stream.Stream<R>mapWithIndex(java.util.stream.IntStream stream, Streams.IntFunctionWithIndex<R> function)Returns a stream consisting of the results of applying the given function to the elements ofstreamand their indexes in the stream.static <R> java.util.stream.Stream<R>mapWithIndex(java.util.stream.LongStream stream, Streams.LongFunctionWithIndex<R> function)Returns a stream consisting of the results of applying the given function to the elements ofstreamand their indexes in the stream.static <T,R>
java.util.stream.Stream<R>mapWithIndex(java.util.stream.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 ofstreamand their indices in the stream.static <T> java.util.stream.Stream<T>stream(Optional<T> optional)If a value is present inoptional, returns a stream containing only that element, otherwise returns an empty stream.static <T> java.util.stream.Stream<T>stream(java.lang.Iterable<T> iterable)Returns a sequentialStreamof the contents ofiterable, delegating toCollection.stream()if possible.static <T> java.util.stream.Stream<T>stream(java.util.Collection<T> collection)Deprecated.There is no reason to use this; just invokecollection.stream()directly.static <T> java.util.stream.Stream<T>stream(java.util.Iterator<T> iterator)Returns a sequentialStreamof the remaining contents ofiterator.static <T> java.util.stream.Stream<T>stream(java.util.Optional<T> optional)If a value is present inoptional, returns a stream containing only that element, otherwise returns an empty stream.static java.util.stream.DoubleStreamstream(java.util.OptionalDouble optional)If a value is present inoptional, returns a stream containing only that element, otherwise returns an empty stream.static java.util.stream.IntStreamstream(java.util.OptionalInt optional)If a value is present inoptional, returns a stream containing only that element, otherwise returns an empty stream.static java.util.stream.LongStreamstream(java.util.OptionalLong optional)If a value is present inoptional, returns a stream containing only that element, otherwise returns an empty stream.static <A,B,R>
java.util.stream.Stream<R>zip(java.util.stream.Stream<A> streamA, java.util.stream.Stream<B> streamB, java.util.function.BiFunction<? super A,? super B,R> function)Returns a stream in which each element is the result of passing the corresponding element of each ofstreamAandstreamBtofunction.
-
-
-
Method Detail
-
stream
public static <T> java.util.stream.Stream<T> stream(java.lang.Iterable<T> iterable)
Returns a sequentialStreamof the contents ofiterable, delegating toCollection.stream()if possible.
-
stream
@Beta @Deprecated public static <T> java.util.stream.Stream<T> stream(java.util.Collection<T> collection)
Deprecated.There is no reason to use this; just invokecollection.stream()directly.ReturnsCollection.stream().
-
stream
@Beta public static <T> java.util.stream.Stream<T> stream(java.util.Iterator<T> iterator)
Returns a sequentialStreamof the remaining contents ofiterator. Do not useiteratordirectly after passing it to this method.
-
stream
@Beta public static <T> java.util.stream.Stream<T> stream(Optional<T> optional)
If a value is present inoptional, returns a stream containing only that element, otherwise returns an empty stream.
-
stream
@Beta public static <T> java.util.stream.Stream<T> stream(java.util.Optional<T> optional)
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
@Beta public static java.util.stream.IntStream stream(java.util.OptionalInt optional)
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
@Beta public static java.util.stream.LongStream stream(java.util.OptionalLong optional)
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
@Beta public static java.util.stream.DoubleStream stream(java.util.OptionalDouble optional)
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
private static void closeAll(java.util.stream.BaseStream<?,?>[] toClose)
-
concat
@SafeVarargs public static <T> java.util.stream.Stream<T> concat(java.util.stream.Stream<? extends T>... streams)
Returns aStreamcontaining 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:
Stream.concat(Stream, Stream)
-
concat
public static java.util.stream.IntStream concat(java.util.stream.IntStream... streams)
Returns anIntStreamcontaining 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:
IntStream.concat(IntStream, IntStream)
-
concat
public static java.util.stream.LongStream concat(java.util.stream.LongStream... streams)
Returns aLongStreamcontaining 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:
LongStream.concat(LongStream, LongStream)
-
concat
public static java.util.stream.DoubleStream concat(java.util.stream.DoubleStream... streams)
Returns aDoubleStreamcontaining 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:
DoubleStream.concat(DoubleStream, DoubleStream)
-
zip
@Beta public static <A,B,R> java.util.stream.Stream<R> zip(java.util.stream.Stream<A> streamA, java.util.stream.Stream<B> streamB, java.util.function.BiFunction<? super A,? super B,R> function)
Returns a stream in which each element is the result of passing the corresponding element of each ofstreamAandstreamBtofunction.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
@Beta public static <A,B> void forEachPair(java.util.stream.Stream<A> streamA, java.util.stream.Stream<B> streamB, java.util.function.BiConsumer<? super A,? super B> consumer)
Invokesconsumeronce for each pair of corresponding elements instreamAandstreamB. 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:bar2Warning: 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
@Beta public static <T,R> java.util.stream.Stream<R> mapWithIndex(java.util.stream.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 ofstreamand their indices in the stream. For example,mapWithIndex( Stream.of("a", "b", "c"), (str, index) -> str + ":" + index)would return
Stream.of("a:0", "b:1", "c:2").The resulting stream is efficiently splittable if and only if
streamwas 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
@Beta public static <R> java.util.stream.Stream<R> mapWithIndex(java.util.stream.IntStream stream, Streams.IntFunctionWithIndex<R> function)
Returns a stream consisting of the results of applying the given function to the elements ofstreamand their indexes in the stream. For example,mapWithIndex( IntStream.of(0, 1, 2), (i, index) -> i + ":" + index)...would return
Stream.of("0:0", "1:1", "2:2").The resulting stream is efficiently splittable if and only if
streamwas 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
@Beta public static <R> java.util.stream.Stream<R> mapWithIndex(java.util.stream.LongStream stream, Streams.LongFunctionWithIndex<R> function)
Returns a stream consisting of the results of applying the given function to the elements ofstreamand their indexes in the stream. For example,mapWithIndex( LongStream.of(0, 1, 2), (i, index) -> i + ":" + index)...would return
Stream.of("0:0", "1:1", "2:2").The resulting stream is efficiently splittable if and only if
streamwas 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
@Beta public static <R> java.util.stream.Stream<R> mapWithIndex(java.util.stream.DoubleStream stream, Streams.DoubleFunctionWithIndex<R> function)
Returns a stream consisting of the results of applying the given function to the elements ofstreamand their indexes in the stream. For example,mapWithIndex( DoubleStream.of(0, 1, 2), (x, index) -> x + ":" + index)...would return
Stream.of("0.0:0", "1.0:1", "2.0:2").The resulting stream is efficiently splittable if and only if
streamwas 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
@Beta public static <T> java.util.Optional<T> findLast(java.util.stream.Stream<T> stream)
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:
java.lang.NullPointerException- if the last element of the stream is null- See Also:
Stream.findFirst()
-
findLast
@Beta public static java.util.OptionalInt findLast(java.util.stream.IntStream stream)
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:
java.lang.NullPointerException- if the last element of the stream is null- See Also:
IntStream.findFirst()
-
findLast
@Beta public static java.util.OptionalLong findLast(java.util.stream.LongStream stream)
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:
java.lang.NullPointerException- if the last element of the stream is null- See Also:
LongStream.findFirst()
-
findLast
@Beta public static java.util.OptionalDouble findLast(java.util.stream.DoubleStream stream)
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:
java.lang.NullPointerException- if the last element of the stream is null- See Also:
DoubleStream.findFirst()
-
-