Interface ConsumingOperators<T>

Type Parameters:
T - The type of the elements that the stream emits.
All Known Subinterfaces:
ProcessorBuilder<T,R>, PublisherBuilder<T>

public interface ConsumingOperators<T>
Operators for completing a stream.

The documentation for each operator uses marble diagrams to visualize how the operator functions. Each element flowing in and out of the stream is represented as a coloured marble that has a value, with the operator applying some transformation or having some side effect, termination and error signals potentially being passed, and for operators that subscribe to the stream, an output value being redeemed at the end.

Below is an example diagram labelling all the parts of the stream.

Example marble diagram

  • Method Details

    • forEach

      ProducesResult<Void> forEach(Consumer<? super T> action)
      Performs an action for each element on this stream.

      forEach marble diagram

      The returned CompletionStage will be redeemed when the stream completes, either successfully if the stream completes normally, or with an error if the stream completes with an error or if the action throws an exception.

      Parameters:
      action - The action.
      Returns:
      A new completion runner.
    • ignore

      ProducesResult<Void> ignore()
      Ignores each element of this stream.

      ignore marble diagram

      The returned CompletionStage will be redeemed when the stream completes, either successfully if the stream completes normally, or with an error if the stream completes with an error or if the action throws an exception.

      Returns:
      A new completion builder.
    • cancel

      ProducesResult<Void> cancel()
      Cancels the stream as soon as it is run.

      The returned CompletionStage will be immediately redeemed as soon as the stream is run.

      Returns:
      A completion builder for the cancelled stream.
    • reduce

      ProducesResult<T> reduce(T identity, BinaryOperator<T> accumulator)
      Perform a reduction on the elements of this stream, using the provided identity value and the accumulation function.

      reduce marble diagram

      The result of the reduction is returned in the CompletionStage.

      Parameters:
      identity - The identity value.
      accumulator - The accumulator function.
      Returns:
      A completion builder for the reduction.
    • reduce

      ProducesResult<Optional<T>> reduce(BinaryOperator<T> accumulator)
      Perform a reduction on the elements of this stream, using the provided accumulation function.

      reduce marble diagram

      The result of the reduction is returned as an Optional<T> in the CompletionStage. If there are no elements in this stream, empty will be returned.

      Parameters:
      accumulator - The accumulator function.
      Returns:
      A completion builder for the reduction.
    • findFirst

      ProducesResult<Optional<T>> findFirst()
      Find the first element emitted by the Publisher, and return it in a CompletionStage.

      findFirst marble diagram

      If the stream is completed before a single element is emitted, then Optional.empty() will be emitted.

      Returns:
      A new completion builder.
    • collect

      <R, A> ProducesResult<R> collect(Collector<? super T,A,R> collector)
      Collect the elements emitted by this stream using the given Collector.

      Since Reactive Streams are intrinsically sequential, only the accumulator of the collector will be used, the combiner will not be used.

      Type Parameters:
      R - The result of the collector.
      A - The accumulator type.
      Parameters:
      collector - The collector to collect the elements.
      Returns:
      A completion builder that emits the collected result.
    • collect

      <R> ProducesResult<R> collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator)
      Collect the elements emitted by this stream using a Collector built from the given supplier and accumulator.

      collect marble diagram

      Since Reactive Streams are intrinsically sequential, the combiner will not be used. This is why this method does not accept a combiner method.

      Type Parameters:
      R - The result of the collector.
      Parameters:
      supplier - a function that creates a new result container. It creates objects of type <A>.
      accumulator - an associative, non-interfering, stateless function for incorporating an additional element into a result
      Returns:
      A completion builder that emits the collected result.
    • toList

      ProducesResult<List<T>> toList()
      Collect the elements emitted by this stream into a List.

      toList marble diagram

      Returns:
      A completion builder that emits the list.