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 Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      ProducesResult<java.lang.Void> cancel()
      Cancels the stream as soon as it is run.
      <R> ProducesResult<R> collect​(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,​? super T> accumulator)
      Collect the elements emitted by this stream using a Collector built from the given supplier and accumulator.
      <R,​A>
      ProducesResult<R>
      collect​(java.util.stream.Collector<? super T,​A,​R> collector)
      Collect the elements emitted by this stream using the given Collector.
      ProducesResult<java.util.Optional<T>> findFirst()
      Find the first element emitted by the Publisher, and return it in a CompletionStage.
      ProducesResult<java.lang.Void> forEach​(java.util.function.Consumer<? super T> action)
      Performs an action for each element on this stream.
      ProducesResult<java.lang.Void> ignore()
      Ignores each element of this stream.
      ProducesResult<java.util.Optional<T>> reduce​(java.util.function.BinaryOperator<T> accumulator)
      Perform a reduction on the elements of this stream, using the provided accumulation function.
      ProducesResult<T> reduce​(T identity, java.util.function.BinaryOperator<T> accumulator)
      Perform a reduction on the elements of this stream, using the provided identity value and the accumulation function.
      ProducesResult<java.util.List<T>> toList()
      Collect the elements emitted by this stream into a List.
    • Method Detail

      • forEach

        ProducesResult<java.lang.Void> forEach​(java.util.function.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<java.lang.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<java.lang.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,
                                 java.util.function.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<java.util.Optional<T>> reduce​(java.util.function.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<java.util.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​(java.util.stream.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​(java.util.function.Supplier<R> supplier,
                                      java.util.function.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<java.util.List<T>> toList()
        Collect the elements emitted by this stream into a List.

        toList marble diagram

        Returns:
        A completion builder that emits the list.