Interface FilteringOperators<T>

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

    public interface FilteringOperators<T>
    Operations for transforming 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 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 Detail

      • filter

        FilteringOperators<T> filter​(java.util.function.Predicate<? super T> predicate)
        Filter elements emitted by this publisher using the given Predicate.

        Any elements that return true when passed to the Predicate will be emitted, all other elements will be dropped.

        filter marbles diagram

        Parameters:
        predicate - The predicate to apply to each element.
        Returns:
        A new stream builder.
      • distinct

        FilteringOperators<T> distinct()
        Creates a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.

        distinct marbles diagram

        Returns:
        A new stream builder emitting the distinct elements from this stream.
      • limit

        FilteringOperators<T> limit​(long maxSize)
        Truncate this stream, ensuring the stream is no longer than maxSize elements in length.

        limit marble diagram

        If maxSize is reached, the stream will be completed, and upstream will be cancelled. Completion of the stream will occur immediately when the element that satisfies the maxSize is received.

        Parameters:
        maxSize - The maximum size of the returned stream.
        Returns:
        A new stream builder.
        Throws:
        java.lang.IllegalArgumentException - If maxSize is less than zero.
      • skip

        FilteringOperators<T> skip​(long n)
        Discard the first n of this stream. If this stream contains fewer than n elements, this stream will effectively be an empty stream.

        skip marble diagram

        Parameters:
        n - The number of elements to discard.
        Returns:
        A new stream builder.
        Throws:
        java.lang.IllegalArgumentException - If n is less than zero.
      • takeWhile

        FilteringOperators<T> takeWhile​(java.util.function.Predicate<? super T> predicate)
        Take the longest prefix of elements from this stream that satisfy the given predicate.

        takeWhile marble diagram

        When the predicate returns false, the stream will be completed, and upstream will be cancelled.

        Parameters:
        predicate - The predicate.
        Returns:
        A new stream builder.
      • dropWhile

        FilteringOperators<T> dropWhile​(java.util.function.Predicate<? super T> predicate)
        Drop the longest prefix of elements from this stream that satisfy the given predicate.

        dropWhile marble diagram

        As long as the predicate returns true, no elements will be emitted from this stream. Once the first element is encountered for which the predicate returns false, all subsequent elements will be emitted, and the predicate will no longer be invoked.

        Parameters:
        predicate - The predicate.
        Returns:
        A new stream builder.