Top Description Inners Constructors Methods
com.google.common.collect

public final Class Streams

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT, java.lang.Math.min, java.util.Objects.requireNonNull

Static utility methods related to Stream instances.
Since
21.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Streams.DoubleFunctionWithIndex<R extends @Nullable Object>

An analogue of java.util.function.DoubleFunction also accepting an index.

public static interface
Streams.FunctionWithIndex<T extends @Nullable Object, R extends @Nullable Object>

An analogue of java.util.function.Function also accepting an index.

public static interface
Streams.IntFunctionWithIndex<R extends @Nullable Object>

An analogue of java.util.function.IntFunction also accepting an index.

public static interface
Streams.LongFunctionWithIndex<R extends @Nullable Object>

An analogue of java.util.function.LongFunction also accepting an index.

private abstract static class
private static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static void
closeAll(BaseStream<?, ?>[] toClose)

public static <T extends @Nullable Object> Stream<T>
concat(Stream<? extends T>... streams)

Returns a Stream containing the elements of the first stream, followed by the elements of the second stream, and so on.

public static IntStream
concat(IntStream... streams)

Returns an IntStream containing the elements of the first stream, followed by the elements of the second stream, and so on.

public static LongStream
concat(LongStream... streams)

Returns a LongStream containing the elements of the first stream, followed by the elements of the second stream, and so on.

public static DoubleStream
concat(DoubleStream... streams)

Returns a DoubleStream containing the elements of the first stream, followed by the elements of the second stream, and so on.

public static <T> Optional<T>
findLast(Stream<T> stream)

Returns the last element of the specified stream, or java.util.Optional#empty if the stream is empty.

public static OptionalInt
findLast(IntStream stream)

Returns the last element of the specified stream, or OptionalInt#empty if the stream is empty.

public static OptionalLong
findLast(LongStream stream)

Returns the last element of the specified stream, or OptionalLong#empty if the stream is empty.

public static OptionalDouble
findLast(DoubleStream stream)

Returns the last element of the specified stream, or OptionalDouble#empty if the stream is empty.

public static <A extends @Nullable Object, B extends @Nullable Object> void
forEachPair(Stream<A> streamA, Stream<B> streamB, BiConsumer<? super A, ? super B> consumer)

Invokes consumer once for each pair of corresponding elements in streamA and streamB.

public static <T extends @Nullable Object, R extends @Nullable Object> 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 of stream and their indices in the stream.

public static <R extends @Nullable Object> Stream<R>
mapWithIndex(IntStream stream, Streams.IntFunctionWithIndex<R> function)

Returns a stream consisting of the results of applying the given function to the elements of stream and their indexes in the stream.

public static <R extends @Nullable Object> Stream<R>
mapWithIndex(LongStream stream, Streams.LongFunctionWithIndex<R> function)

Returns a stream consisting of the results of applying the given function to the elements of stream and their indexes in the stream.

public static <R extends @Nullable Object> Stream<R>
mapWithIndex(DoubleStream stream, Streams.DoubleFunctionWithIndex<R> function)

Returns a stream consisting of the results of applying the given function to the elements of stream and their indexes in the stream.

public static <T extends @Nullable Object> Stream<T>
stream(Iterable<T> iterable)

Returns a sequential Stream of the contents of iterable, delegating to Collection#stream if possible.

public static <T extends @Nullable Object> Stream<T>
stream(Collection<T> collection)

Deprecated There is no reason to use this; just invoke collection.stream() directly.
Returns Collection#stream.
public static <T extends @Nullable Object> Stream<T>
stream(Iterator<T> iterator)

Returns a sequential Stream of the remaining contents of iterator.

public static <T> Stream<T>
stream(Optional<T> optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

public static <T> Stream<T>
stream(Optional<T> optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

public static IntStream
stream(OptionalInt optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

public static LongStream
stream(OptionalLong optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

public static DoubleStream
stream(OptionalDouble optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

public static <A extends @Nullable Object, B extends @Nullable Object, R extends @Nullable Object> 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 of streamA and streamB to function.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Streamsback to summary
private Streams()

Method Detail

closeAllback to summary
private static void closeAll(BaseStream<?, ?>[] toClose)
concatback to summary
public static <T extends @Nullable Object> Stream<T> concat(Stream<? extends T>... streams)

Returns a Stream 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.

Annotations
@SafeVarargs
See Also
Stream#concat(Stream, Stream)
concatback to summary
public static IntStream concat(IntStream... streams)

Returns an IntStream 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
IntStream#concat(IntStream, IntStream)
concatback to summary
public static LongStream concat(LongStream... streams)

Returns a LongStream 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
LongStream#concat(LongStream, LongStream)
concatback to summary
public static DoubleStream concat(DoubleStream... streams)

Returns a DoubleStream 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
DoubleStream#concat(DoubleStream, DoubleStream)
findLastback to summary
public static <T> Optional<T> findLast(Stream<T> stream)

Returns the last element of the specified stream, or java.util.Optional#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).

Exceptions
NullPointerException:
if the last element of the stream is null
See Also
Stream#findFirst()
findLastback to summary
public static OptionalInt findLast(IntStream stream)

Returns the last element of the specified stream, or OptionalInt#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.

Exceptions
NullPointerException:
if the last element of the stream is null
See Also
IntStream#findFirst()
findLastback to summary
public static OptionalLong findLast(LongStream stream)

Returns the last element of the specified stream, or OptionalLong#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.

Exceptions
NullPointerException:
if the last element of the stream is null
See Also
LongStream#findFirst()
findLastback to summary
public static OptionalDouble findLast(DoubleStream stream)

Returns the last element of the specified stream, or OptionalDouble#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.

Exceptions
NullPointerException:
if the last element of the stream is null
See Also
DoubleStream#findFirst()
forEachPairback to summary
public static <A extends @Nullable Object, B extends @Nullable Object> void forEachPair(Stream<A> streamA, Stream<B> streamB, BiConsumer<? super A, ? super B> consumer)

Invokes consumer once for each pair of corresponding elements in streamA and streamB. 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. This method behaves equivalently to zipping the stream elements into temporary pair objects and then using Stream#forEach on that stream.

Annotations
@Beta
Since
22.0
mapWithIndexback to summary
public static <T extends @Nullable Object, R extends @Nullable Object> 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 of stream 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 reported Spliterator#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.

mapWithIndexback to summary
public static <R extends @Nullable Object> Stream<R> mapWithIndex(IntStream stream, Streams.IntFunctionWithIndex<R> function)

Returns a stream consisting of the results of applying the given function to the elements of stream 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 reported Spliterator#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.

mapWithIndexback to summary
public static <R extends @Nullable Object> Stream<R> mapWithIndex(LongStream stream, Streams.LongFunctionWithIndex<R> function)

Returns a stream consisting of the results of applying the given function to the elements of stream 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 reported Spliterator#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.

mapWithIndexback to summary
public static <R extends @Nullable Object> Stream<R> mapWithIndex(DoubleStream stream, Streams.DoubleFunctionWithIndex<R> function)

Returns a stream consisting of the results of applying the given function to the elements of stream 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 reported Spliterator#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.

streamback to summary
public static <T extends @Nullable Object> Stream<T> stream(Iterable<T> iterable)

Returns a sequential Stream of the contents of iterable, delegating to Collection#stream if possible.

streamback to summary
public static <T extends @Nullable Object> Stream<T> stream(Collection<T> collection)

Deprecated

There is no reason to use this; just invoke collection.stream() directly.

Returns Collection#stream.

Annotations
@Deprecated
@InlineMe
replacement:collection.stream()
streamback to summary
public static <T extends @Nullable Object> Stream<T> stream(Iterator<T> iterator)

Returns a sequential Stream of the remaining contents of iterator. Do not use iterator directly after passing it to this method.

streamback to summary
public static <T> Stream<T> stream(Optional<T> optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

streamback to summary
public static <T> Stream<T> stream(Optional<T> optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

Java 9 users: use optional.stream() instead.

Annotations
@Beta
@InlineMe
replacement:optional.stream()
@InlineMeValidationDisabled:Java 9+ API only
streamback to summary
public static IntStream stream(OptionalInt optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

Java 9 users: use optional.stream() instead.

Annotations
@Beta
@InlineMe
replacement:optional.stream()
@InlineMeValidationDisabled:Java 9+ API only
streamback to summary
public static LongStream stream(OptionalLong optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

Java 9 users: use optional.stream() instead.

Annotations
@Beta
@InlineMe
replacement:optional.stream()
@InlineMeValidationDisabled:Java 9+ API only
streamback to summary
public static DoubleStream stream(OptionalDouble optional)

If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

Java 9 users: use optional.stream() instead.

Annotations
@Beta
@InlineMe
replacement:optional.stream()
@InlineMeValidationDisabled:Java 9+ API only
zipback to summary
public static <A extends @Nullable Object, B extends @Nullable Object, R extends @Nullable Object> 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 of streamA and streamB to function.

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 on the resulting stream, you might want to consider using forEachPair instead of this method.

Performance note: The resulting stream is not efficiently splittable. This may harm parallel performance.

Annotations
@Beta
com.google.common.collect back to summary

public Interface Streams.DoubleFunctionWithIndex<R extends @Nullable Object>


An analogue of java.util.function.DoubleFunction also accepting an index.

This interface is only intended for use by callers of mapWithIndex(DoubleStream, DoubleFunctionWithIndex).

Since
21.0

Method Summary

Modifier and TypeMethod and Description
public R
apply(double from, long index)

Applies this function to the given argument and its index within a stream.

Method Detail

applyback to summary
public R apply(double from, long index)

Applies this function to the given argument and its index within a stream.

Annotations
@ParametricNullness
com.google.common.collect back to summary

public Interface Streams.FunctionWithIndex<T extends @Nullable Object, R extends @Nullable Object>


An analogue of java.util.function.Function also accepting an index.

This interface is only intended for use by callers of mapWithIndex(Stream, FunctionWithIndex).

Since
21.0

Method Summary

Modifier and TypeMethod and Description
public R
apply(T from, long index)

Applies this function to the given argument and its index within a stream.

Method Detail

applyback to summary
public R apply(T from, long index)

Applies this function to the given argument and its index within a stream.

Annotations
@ParametricNullness
com.google.common.collect back to summary

public Interface Streams.IntFunctionWithIndex<R extends @Nullable Object>


An analogue of java.util.function.IntFunction also accepting an index.

This interface is only intended for use by callers of mapWithIndex(IntStream, IntFunctionWithIndex).

Since
21.0

Method Summary

Modifier and TypeMethod and Description
public R
apply(int from, long index)

Applies this function to the given argument and its index within a stream.

Method Detail

applyback to summary
public R apply(int from, long index)

Applies this function to the given argument and its index within a stream.

Annotations
@ParametricNullness
com.google.common.collect back to summary

public Interface Streams.LongFunctionWithIndex<R extends @Nullable Object>


An analogue of java.util.function.LongFunction also accepting an index.

This interface is only intended for use by callers of mapWithIndex(LongStream, LongFunctionWithIndex).

Since
21.0

Method Summary

Modifier and TypeMethod and Description
public R
apply(long from, long index)

Applies this function to the given argument and its index within a stream.

Method Detail

applyback to summary
public R apply(long from, long index)

Applies this function to the given argument and its index within a stream.

Annotations
@ParametricNullness
com.google.common.collect back to summary

private abstract Class Streams.MapWithIndexSpliterator<F extends java.util.Spliterator<?>, R extends @Nullable Object, S extends Streams.MapWithIndexSpliterator<F, R, S>>

extends Object
implements Spliterator<R extends @Nullable Object>
Class Inheritance
All Implemented Interfaces
java.util.Spliterator

Field Summary

Modifier and TypeField and Description
pack-priv final F
pack-priv long

Constructor Summary

AccessConstructor and Description
pack-priv
MapWithIndexSpliterator(F fromSpliterator, long index)

Method Summary

Modifier and TypeMethod and Description
public int
characteristics()

Implements java.util.Spliterator.characteristics.

Returns a set of characteristics of this Spliterator and its elements.
pack-priv abstract S
createSplit(F from, long i)

public long
estimateSize()

Implements java.util.Spliterator.estimateSize.

Returns an estimate of the number of elements that would be encountered by a forEachRemaining traversal, or returns Long#MAX_VALUE if infinite, unknown, or too expensive to compute.
public S
trySplit()

Implements java.util.Spliterator.trySplit.

If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fromSpliteratorback to summary
pack-priv final F fromSpliterator
indexback to summary
pack-priv long index

Constructor Detail

MapWithIndexSpliteratorback to summary
pack-priv MapWithIndexSpliterator(F fromSpliterator, long index)

Method Detail

characteristicsback to summary
public int characteristics()

Implements java.util.Spliterator.characteristics.

Doc from java.util.Spliterator.characteristics.

Returns a set of characteristics of this Spliterator and its elements. The result is represented as ORed values from ORDERED, DISTINCT, SORTED, SIZED, NONNULL, IMMUTABLE, CONCURRENT, SUBSIZED. Repeated calls to characteristics() on a given spliterator, prior to or in-between calls to trySplit, should always return the same result.

If a Spliterator reports an inconsistent set of characteristics (either those returned from a single invocation or across multiple invocations), no guarantees can be made about any computation using this Spliterator.

Returns:int

a representation of characteristics

Annotations
@Override
createSplitback to summary
pack-priv abstract S createSplit(F from, long i)
estimateSizeback to summary
public long estimateSize()

Implements java.util.Spliterator.estimateSize.

Doc from java.util.Spliterator.estimateSize.

Returns an estimate of the number of elements that would be encountered by a forEachRemaining traversal, or returns Long#MAX_VALUE if infinite, unknown, or too expensive to compute.

If this Spliterator is SIZED and has not yet been partially traversed or split, or this Spliterator is SUBSIZED and has not yet been partially traversed, this estimate must be an accurate count of elements that would be encountered by a complete traversal. Otherwise, this estimate may be arbitrarily inaccurate, but must decrease as specified across invocations of trySplit.

Returns:long

the estimated size, or Long.MAX_VALUE if infinite, unknown, or too expensive to compute.

Annotations
@Override
trySplitback to summary
public S trySplit()

Implements java.util.Spliterator.trySplit.

Doc from java.util.Spliterator.trySplit.

If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.

If this Spliterator is ORDERED, the returned Spliterator must cover a strict prefix of the elements.

Unless this Spliterator covers an infinite number of elements, repeated calls to trySplit() must eventually return null. Upon non-null return:

  • the value reported for estimateSize() before splitting, must, after splitting, be greater than or equal to estimateSize() for this and the returned Spliterator; and
  • if this Spliterator is SUBSIZED, then estimateSize() for this spliterator before splitting must be equal to the sum of estimateSize() for this and the returned Spliterator after splitting.

This method may return null for any reason, including emptiness, inability to split after traversal has commenced, data structure constraints, and efficiency considerations.

Returns:S

a Spliterator covering some portion of the elements, or null if this spliterator cannot be split

Annotations
@Override
@CheckForNull
com.google.common.collect back to summary

private Class Streams.TemporaryPair<A extends @Nullable Object, B extends @Nullable Object>

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final A
a

pack-priv final B
b

Constructor Summary

AccessConstructor and Description
pack-priv
TemporaryPair(A a, B b)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

aback to summary
pack-priv final A a
Annotations
@ParametricNullness
bback to summary
pack-priv final B b
Annotations
@ParametricNullness

Constructor Detail

TemporaryPairback to summary
pack-priv TemporaryPair(A a, B b)