Interface StreamSupplier<T>

Type Parameters:
T - stream element type
All Superinterfaces:
Supplier<Stream<T>>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface StreamSupplier<T> extends Supplier<Stream<T>>
Stream-processing methods should take a Stream-returning lambda, not a raw Stream. StreamSupplier is such a lambda: its stream() method is defined to return a fresh stream on every call.

(Motivation: stream-processing methods almost surely will traverse the stream, and also might do a terminal operation, e.g., forEach, max(), collect() etc.) or even close() the stream. Subsequent calls to the same Stream might process only a part of the stream, or result in IllegalStateExceptions.)

StreamSupplier has convenience forEach[Ordered]() methods for immediately traversing the stream returned by stream(), and can be used everywhere where a Supplier is expected (as it extends the Supplier interface).

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    forEach(Consumer<? super T> action)
    Traverses the stream returned by stream() in a non-deterministic order, executing the action specified on each stream element.
    default void
    forEachOrdered(Consumer<? super T> action)
    Traverses the stream returned by stream() in stream encounter order (if stream has one), executing the action specified on each stream element.
    default Stream<T>
    get()
    Allows to use StreamSupplier as a Supplier.
    Returns a fresh stream, guaranteed to be open and not consumed by a terminal stream operation.
  • Method Details

    • stream

      Stream<T> stream()
      Returns a fresh stream, guaranteed to be open and not consumed by a terminal stream operation.

      Whether the stream returned should be closed after use depends on the stream() method implementation. Library utilities, e.g., StreamSupplier.forEach[Ordered](), will play safe and call Stream.close() after consuming the stream returned.

      Returns:
      fresh stream
    • get

      default Stream<T> get()
      Allows to use StreamSupplier as a Supplier. Result is the same as stream()
      Specified by:
      get in interface Supplier<T>
      Returns:
      fresh stream
    • forEach

      default void forEach(Consumer<? super T> action)
      Traverses the stream returned by stream() in a non-deterministic order, executing the action specified on each stream element. This method behaves as if stream().forEach(action) is called, except that the stream returned by stream() is closed after traversal.
      Parameters:
      action - action to execute for each stream element
      See Also:
    • forEachOrdered

      default void forEachOrdered(Consumer<? super T> action)
      Traverses the stream returned by stream() in stream encounter order (if stream has one), executing the action specified on each stream element. This method behaves as if stream().forEachOrdered(action) is called, except that the stream returned by stream() is closed after traversal.
      Parameters:
      action - action to execute for each stream element
      See Also: