Interface StreamSupplier<T>
- Type Parameters:
T- stream element type
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
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).
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidTraverses the stream returned bystream()in a non-deterministic order, executing the action specified on each stream element.default voidforEachOrdered(Consumer<? super T> action) Traverses the stream returned bystream()in stream encounter order (if stream has one), executing the action specified on each stream element.get()Allows to useStreamSupplieras aSupplier.stream()Returns a fresh stream, guaranteed to be open and not consumed by a terminal stream operation.
-
Method Details
-
stream
Returns a fresh stream, guaranteed to be open and not consumed by a terminal stream operation.Whether the stream returned should be
closedafter use depends on thestream()method implementation. Library utilities, e.g.,StreamSupplier.forEach[Ordered](), will play safe and callStream.close()after consuming the stream returned.- Returns:
- fresh stream
-
get
-
forEach
Traverses the stream returned bystream()in a non-deterministic order, executing the action specified on each stream element. This method behaves as ifstream().forEach(action)is called, except that the stream returned bystream()isclosedafter traversal.- Parameters:
action- action to execute for each stream element- See Also:
-
forEachOrdered
Traverses the stream returned bystream()in stream encounter order (if stream has one), executing the action specified on each stream element. This method behaves as ifstream().forEachOrdered(action)is called, except that the stream returned bystream()isclosedafter traversal.- Parameters:
action- action to execute for each stream element- See Also:
-