Class AbstractStreamEx<T,​S extends AbstractStreamEx<T,​S>>

    • Method Detail

      • parallel

        public S parallel​(ForkJoinPool fjp)
        Returns an equivalent stream that is parallel and bound to the supplied ForkJoinPool.

        This is an intermediate operation.

        The terminal operation of this stream or any derived stream (except the streams created via BaseStream.parallel() or BaseStream.sequential() methods) will be executed inside the supplied ForkJoinPool. If current thread does not belong to that pool, it will wait till calculation finishes.

        Parameters:
        fjp - a ForkJoinPool to submit the stream operation to.
        Returns:
        a parallel stream bound to the supplied ForkJoinPool
      • intersperse

        public S intersperse​(T delimiter)
        Returns a new stream containing all the elements of the original stream interspersed with given delimiter.

        For example, StreamEx.of("a", "b", "c").intersperse("x") will yield a stream containing five elements: a, x, b, x, c.

        This is an intermediate operation.

        Parameters:
        delimiter - a delimiter to be inserted between each pair of elements
        Returns:
        the new stream
        Since:
        0.6.6
      • distinct

        public S distinct()
        Specified by:
        distinct in interface Stream<T>
      • distinct

        public S distinct​(Function<? super T,​?> keyExtractor)
        Returns a stream consisting of the distinct elements of this stream (according to object equality of the results of applying the given function).

        For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.

        This is a stateful intermediate operation.

        Parameters:
        keyExtractor - a non-interfering, stateless function which classifies input elements.
        Returns:
        the new stream
        Since:
        0.3.8
      • distinct

        public S distinct​(long atLeast)
        Returns a StreamEx consisting of the distinct elements (according to Object.equals(Object)) which appear at least specified number of times in this stream.

        This operation is not guaranteed to be stable: any of equal elements can be selected for the output. However if this stream is ordered then order is preserved.

        This is a stateful quasi-intermediate operation.

        Parameters:
        atLeast - minimal number of occurrences required to select the element. If atLeast is 1 or less, then this method is equivalent to distinct().
        Returns:
        the new stream
        Since:
        0.3.1
        See Also:
        distinct()
      • sorted

        public S sorted()
        Specified by:
        sorted in interface Stream<T>
      • limit

        public S limit​(long maxSize)
        Specified by:
        limit in interface Stream<T>
      • skip

        public S skip​(long n)
        Specified by:
        skip in interface Stream<T>
      • reduceWithZero

        public T reduceWithZero​(T zero,
                                T identity,
                                BinaryOperator<T> accumulator)
        Performs a possibly short-circuiting reduction of the stream elements using the provided identity value and a BinaryOperator.

        This is a short-circuiting terminal operation. It behaves like reduce(Object, BinaryOperator). However, it additionally accepts a zero element (also known as absorbing element). When zero element is passed to the accumulator then the result must be zero as well. So the operation takes the advantage of this and may short-circuit if zero is reached during the reduction.

        Parameters:
        zero - zero element
        identity - an identity element. For all t, accumulator.apply(t, identity) is equal to accumulator.apply(identity, t) and is equal to t.
        accumulator - an associative , non-interfering , stateless function to combine two elements into one.
        Returns:
        the result of reduction. Empty Optional is returned if the input stream is empty.
        Throws:
        NullPointerException - if accumulator is null or the result of reduction is null
        Since:
        0.7.3
        See Also:
        MoreCollectors.reducingWithZero(Object, Object, BinaryOperator), reduceWithZero(Object, BinaryOperator), reduce(Object, BinaryOperator)
      • count

        public long count()
        Specified by:
        count in interface Stream<T>
      • indexOf

        public OptionalLong indexOf​(T element)
        Returns an OptionalLong describing the zero-based index of the first element of this stream, which equals to the given element, or an empty OptionalLong if there's no matching element.

        This is a short-circuiting terminal operation.

        Parameters:
        element - an element to look for
        Returns:
        an OptionalLong describing the index of the first matching element of this stream, or an empty OptionalLong if there's no matching element.
        Since:
        0.4.0
        See Also:
        indexOf(Predicate)
      • indexOf

        public OptionalLong indexOf​(Predicate<? super T> predicate)
        Returns an OptionalLong describing the zero-based index of the first element of this stream, which matches given predicate, or an empty OptionalLong if there's no matching element.

        This is a short-circuiting terminal operation.

        Parameters:
        predicate - a non-interfering , stateless predicate which returned value should match
        Returns:
        an OptionalLong describing the index of the first matching element of this stream, or an empty OptionalLong if there's no matching element.
        Since:
        0.4.0
        See Also:
        findFirst(Predicate), indexOf(Object)
      • flatCollection

        public <R> StreamEx<R> flatCollection​(Function<? super T,​? extends Collection<? extends R>> mapper)
        Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped collection produced by applying the provided mapping function to each element. (If a mapped collection is null nothing is added for given element to the resulting stream.)

        This is an intermediate operation.

        The flatCollection() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.

        Type Parameters:
        R - The element type of the new stream
        Parameters:
        mapper - a non-interfering , stateless function to apply to each element which produces a Collection of new values
        Returns:
        the new stream
      • flatArray

        public <R> StreamEx<R> flatArray​(Function<? super T,​? extends R[]> mapper)
        Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped array produced by applying the provided mapping function to each element. (If a mapped array is null nothing is added for given element to the resulting stream.)

        This is an intermediate operation.

        The flatArray() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.

        Type Parameters:
        R - The element type of the new stream
        Parameters:
        mapper - a non-interfering , stateless function to apply to each element which produces an array of new values
        Returns:
        the new stream
        Since:
        0.6.5
      • mapPartial

        public <R> StreamEx<R> mapPartial​(Function<? super T,​? extends Optional<? extends R>> mapper)
        Performs a mapping of the stream content to a partial function removing the elements to which the function is not applicable.

        If the mapping function returns Optional.empty(), the original value will be removed from the resulting stream. The mapping function may not return null.

        This is an intermediate operation.

        The mapPartial() operation has the effect of applying a one-to-zero-or-one transformation to the elements of the stream, and then flattening the resulting elements into a new stream.

        Type Parameters:
        R - The element type of the new stream
        Parameters:
        mapper - a non-interfering , stateless partial function to apply to each element which returns a present optional if it's applicable, or an empty optional otherwise
        Returns:
        the new stream
        Since:
        0.6.8
      • pairMap

        public <R> StreamEx<R> pairMap​(BiFunction<? super T,​? super T,​? extends R> mapper)
        Returns a stream consisting of the results of applying the given function to the every adjacent pair of elements of this stream.

        This is a quasi-intermediate operation.

        The output stream will contain one element less than this stream. If this stream contains zero or one element the output stream will be empty.

        Type Parameters:
        R - The element type of the new stream
        Parameters:
        mapper - a non-interfering, stateless function to apply to each adjacent pair of this stream elements.
        Returns:
        the new stream
        Since:
        0.2.1
      • findAny

        public Optional<T> findAny​(Predicate<? super T> predicate)
        Returns an Optional describing some element of the stream, which matches given predicate, or an empty Optional if there's no matching element.

        This is a short-circuiting terminal operation.

        The behavior of this operation is explicitly nondeterministic; it is free to select any element in the stream. This is to allow for maximal performance in parallel operations; the cost is that multiple invocations on the same source may not return the same result. (If a stable result is desired, use findFirst(Predicate) instead.)

        Parameters:
        predicate - a non-interfering , stateless predicate which returned value should match
        Returns:
        an Optional describing some matching element of this stream, or an empty Optional if there's no matching element
        Throws:
        NullPointerException - if the element selected is null
        See Also:
        findAny(), findFirst(Predicate)
      • findFirst

        public Optional<T> findFirst​(Predicate<? super T> predicate)
        Returns an Optional describing the first element of this stream, which matches given predicate, or an empty Optional if there's no matching element.

        This is a short-circuiting terminal operation.

        Parameters:
        predicate - a non-interfering , stateless predicate which returned value should match
        Returns:
        an Optional describing the first matching element of this stream, or an empty Optional if there's no matching element
        Throws:
        NullPointerException - if the element selected is null
        See Also:
        findFirst()
      • reverseSorted

        public S reverseSorted​(Comparator<? super T> comparator)
        Returns a stream consisting of the elements of this stream, sorted in descending order according to the provided Comparator.

        For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

        This is a stateful intermediate operation.

        Parameters:
        comparator - a non-interfering , stateless Comparator to be used to compare stream elements
        Returns:
        the new stream
      • sortedBy

        public <V extends Comparable<? super V>> S sortedBy​(Function<? super T,​? extends V> keyExtractor)
        Returns a stream consisting of the elements of this stream, sorted according to the natural order of the keys extracted by provided function.

        For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

        This is a stateful intermediate operation.

        Type Parameters:
        V - the type of the Comparable sort key
        Parameters:
        keyExtractor - a non-interfering , stateless function to be used to extract sorting keys
        Returns:
        the new stream
      • sortedByInt

        public S sortedByInt​(ToIntFunction<? super T> keyExtractor)
        Returns a stream consisting of the elements of this stream, sorted according to the int values extracted by provided function.

        For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

        This is a stateful intermediate operation.

        Parameters:
        keyExtractor - a non-interfering , stateless function to be used to extract sorting keys
        Returns:
        the new stream
      • sortedByLong

        public S sortedByLong​(ToLongFunction<? super T> keyExtractor)
        Returns a stream consisting of the elements of this stream, sorted according to the long values extracted by provided function.

        For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

        This is a stateful intermediate operation.

        Parameters:
        keyExtractor - a non-interfering , stateless function to be used to extract sorting keys
        Returns:
        the new stream
      • sortedByDouble

        public S sortedByDouble​(ToDoubleFunction<? super T> keyExtractor)
        Returns a stream consisting of the elements of this stream, sorted according to the double values extracted by provided function.

        For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

        This is a stateful intermediate operation.

        Parameters:
        keyExtractor - a non-interfering , stateless function to be used to extract sorting keys
        Returns:
        the new stream
      • minBy

        public <V extends Comparable<? super V>> Optional<T> minBy​(Function<? super T,​? extends V> keyExtractor)
        Returns the minimum element of this stream according to the natural order of the keys extracted by provided function. This is a special case of a reduction.

        This is a terminal operation.

        This method is equivalent to min(Comparator.comparing(keyExtractor)), but may work faster as keyExtractor function is applied only once per each input element.

        Type Parameters:
        V - the type of the comparable keys
        Parameters:
        keyExtractor - a non-interfering , stateless function to extract the comparable keys from this stream elements
        Returns:
        an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
        Throws:
        NullPointerException - if the minimum element is null
      • minByInt

        public Optional<T> minByInt​(ToIntFunction<? super T> keyExtractor)
        Returns the minimum element of this stream according to the int values extracted by provided function. This is a special case of a reduction.

        This is a terminal operation.

        This method is equivalent to min(Comparator.comparingInt(keyExtractor)), but may work faster as keyExtractor function is applied only once per each input element.

        Parameters:
        keyExtractor - a non-interfering , stateless function to extract the int keys from this stream elements
        Returns:
        an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
        Throws:
        NullPointerException - if the minimum element is null
      • minByLong

        public Optional<T> minByLong​(ToLongFunction<? super T> keyExtractor)
        Returns the minimum element of this stream according to the long values extracted by provided function. This is a special case of a reduction.

        This is a terminal operation.

        This method is equivalent to min(Comparator.comparingLong(keyExtractor)), but may work faster as keyExtractor function is applied only once per each input element.

        Parameters:
        keyExtractor - a non-interfering , stateless function to extract the long keys from this stream elements
        Returns:
        an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
        Throws:
        NullPointerException - if the minimum element is null
      • minByDouble

        public Optional<T> minByDouble​(ToDoubleFunction<? super T> keyExtractor)
        Returns the minimum element of this stream according to the double values extracted by provided function. This is a special case of a reduction.

        This is a terminal operation.

        This method is equivalent to min(Comparator.comparingDouble(keyExtractor)), but may work faster as keyExtractor function is applied only once per each input element.

        Parameters:
        keyExtractor - a non-interfering , stateless function to extract the double keys from this stream elements
        Returns:
        an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
        Throws:
        NullPointerException - if the minimum element is null
      • maxBy

        public <V extends Comparable<? super V>> Optional<T> maxBy​(Function<? super T,​? extends V> keyExtractor)
        Returns the maximum element of this stream according to the natural order of the keys extracted by provided function. This is a special case of a reduction.

        This is a terminal operation.

        This method is equivalent to max(Comparator.comparing(keyExtractor)), but may work faster as keyExtractor function is applied only once per each input element.

        Type Parameters:
        V - the type of the comparable keys
        Parameters:
        keyExtractor - a non-interfering , stateless function to extract the comparable keys from this stream elements
        Returns:
        an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
        Throws:
        NullPointerException - if the maximum element is null
      • maxByInt

        public Optional<T> maxByInt​(ToIntFunction<? super T> keyExtractor)
        Returns the maximum element of this stream according to the int values extracted by provided function. This is a special case of a reduction.

        This is a terminal operation.

        This method is equivalent to max(Comparator.comparingInt(keyExtractor)), but may work faster as keyExtractor function is applied only once per each input element.

        Parameters:
        keyExtractor - a non-interfering , stateless function to extract the int keys from this stream elements
        Returns:
        an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
        Throws:
        NullPointerException - if the maximum element is null
      • maxByLong

        public Optional<T> maxByLong​(ToLongFunction<? super T> keyExtractor)
        Returns the maximum element of this stream according to the long values extracted by provided function. This is a special case of a reduction.

        This is a terminal operation.

        This method is equivalent to max(Comparator.comparingLong(keyExtractor)), but may work faster as keyExtractor function is applied only once per each input element.

        Parameters:
        keyExtractor - a non-interfering , stateless function to extract the long keys from this stream elements
        Returns:
        an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
        Throws:
        NullPointerException - if the maximum element is null
      • maxByDouble

        public Optional<T> maxByDouble​(ToDoubleFunction<? super T> keyExtractor)
        Returns the maximum element of this stream according to the double values extracted by provided function. This is a special case of a reduction.

        This is a terminal operation.

        This method is equivalent to max(Comparator.comparingDouble(keyExtractor)), but may work faster as keyExtractor function is applied only once per each input element.

        Parameters:
        keyExtractor - a non-interfering , stateless function to extract the double keys from this stream elements
        Returns:
        an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
        Throws:
        NullPointerException - if the maximum element is null
      • append

        public S append​(Stream<? extends T> other)
        Creates a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of the other stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel. When the resulting stream is closed, the close handlers for both input streams are invoked.

        This is a quasi-intermediate operation with tail-stream optimization.

        May return this if the supplied stream is known to be empty.

        Parameters:
        other - the other stream
        Returns:
        this stream appended by the other stream
        See Also:
        Stream.concat(Stream, Stream)
      • prepend

        public S prepend​(Stream<? extends T> other)
        Creates a lazily concatenated stream whose elements are all the elements of the other stream followed by all the elements of this stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel. When the resulting stream is closed, the close handlers for both input streams are invoked.

        This is a quasi-intermediate operation with tail-stream optimization.

        May return this if the supplied stream is known to be empty.

        Parameters:
        other - the other stream
        Returns:
        this stream prepended by the other stream
        See Also:
        Stream.concat(Stream, Stream)
      • ifEmpty

        public S ifEmpty​(Stream<? extends T> other)
        Returns a stream which contents is the same as this stream, except the case when this stream is empty. In this case, its contents is replaced with other stream contents.

        The other stream will not be traversed if this stream is not empty.

        If this stream is parallel and empty, the other stream is not guaranteed to be parallelized.

        This is a quasi-intermediate operation.

        Parameters:
        other - other stream to replace the contents of this stream if this stream is empty.
        Returns:
        the stream which contents is replaced by other stream contents only if this stream is empty.
        Since:
        0.6.6
      • toList

        public List<T> toList()
        Returns a List containing the elements of this stream. The returned List is guaranteed to be mutable, but there are no guarantees on the type, serializability, or thread-safety; if more control over the returned List is required, use toCollection(Supplier).

        This is a terminal operation.

        Returns:
        a List containing the elements of this stream
        See Also:
        Collectors.toList(), toImmutableList()
      • toImmutableList

        public List<T> toImmutableList()
        Returns an immutable List containing the elements of this stream. There's no guarantees on exact type of the returned List. The returned List is guaranteed to be serializable if all its elements are serializable.

        This is a terminal operation.

        Returns:
        a List containing the elements of this stream
        Since:
        0.6.3
        See Also:
        toList()
      • toListAndThen

        public <R> R toListAndThen​(Function<? super List<T>,​R> finisher)
        Creates a List containing the elements of this stream, then performs finishing transformation and returns its result. There are no guarantees on the type, serializability or thread-safety of the List created.

        This is a terminal operation.

        Type Parameters:
        R - the type of the result
        Parameters:
        finisher - a function to be applied to the intermediate list
        Returns:
        result of applying the finisher transformation to the list of the stream elements.
        Since:
        0.2.3
        See Also:
        toList()
      • toSet

        public Set<T> toSet()
        Returns a Set containing the elements of this stream. The returned Set is guaranteed to be mutable, but there are no guarantees on the type, serializability, or thread-safety; if more control over the returned Set is required, use toCollection(Supplier).

        This is a terminal operation.

        Returns:
        a Set containing the elements of this stream
        See Also:
        Collectors.toSet()
      • toImmutableSet

        public Set<T> toImmutableSet()
        Returns an immutable Set containing the elements of this stream. There's no guarantees on exact type of the returned Set. In particular, no specific element order in the resulting set is guaranteed. The returned Set is guaranteed to be serializable if all its elements are serializable.

        This is a terminal operation.

        Returns:
        a Set containing the elements of this stream
        Since:
        0.6.3
        See Also:
        toSet()
      • toSetAndThen

        public <R> R toSetAndThen​(Function<? super Set<T>,​R> finisher)
        Creates a Set containing the elements of this stream, then performs finishing transformation and returns its result. There are no guarantees on the type, serializability or thread-safety of the Set created.

        This is a terminal operation.

        Type Parameters:
        R - the result type
        Parameters:
        finisher - a function to be applied to the intermediate Set
        Returns:
        result of applying the finisher transformation to the Set of the stream elements.
        Since:
        0.2.3
        See Also:
        toSet()
      • toCollectionAndThen

        public <C extends Collection<T>,​R> R toCollectionAndThen​(Supplier<C> collectionFactory,
                                                                       Function<? super C,​R> finisher)
        Creates a custom Collection containing the elements of this stream, then performs finishing transformation and returns its result. The Collection is created by the provided factory.

        This is a terminal operation.

        Type Parameters:
        C - the type of the resulting Collection
        R - the result type
        Parameters:
        collectionFactory - a Supplier which returns a new, empty Collection of the appropriate type
        finisher - a function to be applied to the intermediate Collection
        Returns:
        result of applying the finisher transformation to the Collection of the stream elements.
        Since:
        0.7.3
        See Also:
        toCollection(Supplier)
      • toCollection

        public <C extends Collection<T>> C toCollection​(Supplier<C> collectionFactory)
        Returns a Collection containing the elements of this stream. The Collection is created by the provided factory.

        This is a terminal operation.

        Type Parameters:
        C - the type of the resulting Collection
        Parameters:
        collectionFactory - a Supplier which returns a new, empty Collection of the appropriate type
        Returns:
        a Collection containing the elements of this stream
        See Also:
        Collectors.toCollection(Supplier), toCollectionAndThen(Supplier, Function)
      • foldLeft

        public <U> U foldLeft​(U seed,
                              BiFunction<U,​? super T,​U> accumulator)
        Folds the elements of this stream using the provided seed object and accumulation function, going left to right. This is equivalent to:
         
             U result = seed;
             for (T element : this stream)
                 result = accumulator.apply(result, element)
             return result;
         
         

        This is a terminal operation.

        This method cannot take all the advantages of parallel streams as it must process elements strictly left to right. If your accumulator function is associative and you can provide a combiner function, consider using reduce(Object, BiFunction, BinaryOperator) method.

        For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.

        Type Parameters:
        U - The type of the result
        Parameters:
        seed - the starting value
        accumulator - a non-interfering , stateless function for incorporating an additional element into a result
        Returns:
        the result of the folding
        Since:
        0.2.0
        See Also:
        foldRight(Object, BiFunction), reduce(Object, BinaryOperator), reduce(Object, BiFunction, BinaryOperator)
      • foldLeft

        public Optional<T> foldLeft​(BinaryOperator<T> accumulator)
        Folds the elements of this stream using the provided accumulation function, going left to right. This is equivalent to:
         
             boolean foundAny = false;
             T result = null;
             for (T element : this stream) {
                 if (!foundAny) {
                     foundAny = true;
                     result = element;
                 }
                 else
                     result = accumulator.apply(result, element);
             }
             return foundAny ? Optional.of(result) : Optional.empty();
         
         

        This is a terminal operation.

        This method cannot take all the advantages of parallel streams as it must process elements strictly left to right. If your accumulator function is associative, consider using reduce(BinaryOperator) method.

        For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.

        Parameters:
        accumulator - a non-interfering , stateless function for incorporating an additional element into a result
        Returns:
        the result of the folding
        Since:
        0.4.0
        See Also:
        foldLeft(Object, BiFunction), foldRight(BinaryOperator), reduce(BinaryOperator)
      • foldRight

        public <U> U foldRight​(U seed,
                               BiFunction<? super T,​U,​U> accumulator)
        Folds the elements of this stream using the provided seed object and accumulation function, going right to left.

        This is a terminal operation.

        As this method must process elements strictly right to left, it cannot start processing till all the previous stream stages complete. Also it requires intermediate memory to store the whole content of the stream as the stream natural order is left to right. If your accumulator function is associative and you can provide a combiner function, consider using reduce(Object, BiFunction, BinaryOperator) method.

        For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.

        Type Parameters:
        U - The type of the result
        Parameters:
        seed - the starting value
        accumulator - a non-interfering , stateless function for incorporating an additional element into a result
        Returns:
        the result of the folding
        Since:
        0.2.2
        See Also:
        foldLeft(Object, BiFunction), reduce(Object, BinaryOperator), reduce(Object, BiFunction, BinaryOperator)
      • foldRight

        public Optional<T> foldRight​(BinaryOperator<T> accumulator)
        Folds the elements of this stream using the provided accumulation function, going right to left.

        This is a terminal operation.

        As this method must process elements strictly right to left, it cannot start processing till all the previous stream stages complete. Also it requires intermediate memory to store the whole content of the stream as the stream natural order is left to right. If your accumulator function is associative, consider using reduce(BinaryOperator) method.

        For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.

        Parameters:
        accumulator - a non-interfering , stateless function for incorporating an additional element into a result
        Returns:
        the result of the folding
        Since:
        0.4.0
        See Also:
        foldRight(Object, BiFunction), foldLeft(BinaryOperator), reduce(BinaryOperator)
      • scanLeft

        public <U> List<U> scanLeft​(U seed,
                                    BiFunction<U,​? super T,​U> accumulator)
        Produces a list containing cumulative results of applying the accumulation function going left to right using given seed value.

        This is a terminal operation.

        The resulting List is guaranteed to be mutable.

        For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.

        This method cannot take all the advantages of parallel streams as it must process elements strictly left to right.

        Type Parameters:
        U - The type of the result
        Parameters:
        seed - the starting value
        accumulator - a non-interfering , stateless function for incorporating an additional element into a result
        Returns:
        the List where the first element is the seed and every successor element is the result of applying accumulator function to the previous list element and the corresponding stream element. The resulting list is one element longer than this stream.
        Since:
        0.2.1
        See Also:
        foldLeft(Object, BiFunction), scanRight(Object, BiFunction)
      • scanLeft

        public List<T> scanLeft​(BinaryOperator<T> accumulator)
        Produces a list containing cumulative results of applying the accumulation function going left to right.

        This is a terminal operation.

        The resulting List is guaranteed to be mutable.

        For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.

        This method cannot take all the advantages of parallel streams as it must process elements strictly left to right.

        Parameters:
        accumulator - a non-interfering , stateless function for incorporating an additional element into a result
        Returns:
        the List where the first element is the first element of this stream and every successor element is the result of applying accumulator function to the previous list element and the corresponding stream element. The resulting list has the same size as this stream.
        Since:
        0.4.0
        See Also:
        foldLeft(BinaryOperator), scanRight(BinaryOperator), prefix(BinaryOperator)
      • scanRight

        public <U> List<U> scanRight​(U seed,
                                     BiFunction<? super T,​U,​U> accumulator)
        Produces a list containing cumulative results of applying the accumulation function going right to left using given seed value.

        This is a terminal operation.

        The resulting List is guaranteed to be mutable.

        For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.

        This method cannot take all the advantages of parallel streams as it must process elements strictly right to left.

        Type Parameters:
        U - The type of the result
        Parameters:
        seed - the starting value
        accumulator - a non-interfering , stateless function for incorporating an additional element into a result
        Returns:
        the List where the last element is the seed and every predecessor element is the result of applying accumulator function to the corresponding stream element and the next list element. The resulting list is one element longer than this stream.
        Since:
        0.2.2
        See Also:
        scanLeft(Object, BiFunction), foldRight(Object, BiFunction)
      • scanRight

        public List<T> scanRight​(BinaryOperator<T> accumulator)
        Produces a collection containing cumulative results of applying the accumulation function going right to left.

        This is a terminal operation.

        The result List is guaranteed to be mutable.

        For parallel stream it's not guaranteed that accumulator will always be executed in the same thread.

        This method cannot take all the advantages of parallel streams as it must process elements strictly right to left.

        Parameters:
        accumulator - a non-interfering , stateless function for incorporating an additional element into a result
        Returns:
        the List where the last element is the last element of this stream and every predecessor element is the result of applying accumulator function to the corresponding stream element and the next list element. The resulting list is one element longer than this stream.
        Since:
        0.4.0
        See Also:
        scanLeft(BinaryOperator), foldRight(BinaryOperator)
      • takeWhile

        public S takeWhile​(Predicate<? super T> predicate)
        Returns a stream consisting of all elements from this stream until the first element which does not match the given predicate is found.

        This is a short-circuiting stateful operation. It can be either intermediate or quasi-intermediate. When using with JDK 1.9 or higher it calls the corresponding JDK 1.9 implementation. When using with JDK 1.8 it uses own implementation.

        While this operation is quite cheap for sequential stream, it can be quite expensive on parallel pipelines. Using unordered source or making it explicitly unordered with unordered() call may improve the parallel processing performance if semantics permit.

        Specified by:
        takeWhile in interface Stream<T>
        Parameters:
        predicate - a non-interfering, stateless predicate to apply to elements.
        Returns:
        the new stream.
        Since:
        0.3.6
        See Also:
        takeWhileInclusive(Predicate), dropWhile(Predicate)
      • takeWhileInclusive

        public S takeWhileInclusive​(Predicate<? super T> predicate)
        Returns a stream consisting of all elements from this stream until the first element which does not match the given predicate is found (including the first mismatching element).

        This is a quasi-intermediate operation.

        While this operation is quite cheap for sequential stream, it can be quite expensive on parallel pipelines. Using unordered source or making it explicitly unordered with unordered() call may improve the parallel processing performance if semantics permit.

        Parameters:
        predicate - a non-interfering, stateless predicate to apply to elements.
        Returns:
        the new stream.
        Since:
        0.5.5
        See Also:
        takeWhile(Predicate)
      • dropWhile

        public S dropWhile​(Predicate<? super T> predicate)
        Returns a stream consisting of all elements from this stream starting from the first element which does not match the given predicate. If the predicate is true for all stream elements, an empty stream is returned.

        This is a stateful operation. It can be either intermediate or quasi-intermediate. When using with JDK 1.9 or higher it calls the corresponding JDK 1.9 implementation. When using with JDK 1.8 it uses own implementation.

        While this operation is quite cheap for sequential stream, it can be quite expensive on parallel pipelines. Using unordered source or making it explicitly unordered with unordered() call may improve the parallel processing performance if semantics permit.

        Specified by:
        dropWhile in interface Stream<T>
        Parameters:
        predicate - a non-interfering, stateless predicate to apply to elements.
        Returns:
        the new stream.
        Since:
        0.3.6
      • prefix

        public S prefix​(BinaryOperator<T> op)
        Returns a stream containing cumulative results of applying the accumulation function going left to right.

        This is a stateful quasi-intermediate operation.

        This operation resembles scanLeft(BinaryOperator), but unlike scanLeft this operation is intermediate and accumulation function must be associative.

        This method cannot take all the advantages of parallel streams as it must process elements strictly left to right. Using an unordered source or removing the ordering constraint with unordered() may improve the parallel processing speed.

        Parameters:
        op - an associative, non-interfering , stateless function for computing the next element based on the previous one
        Returns:
        the new stream.
        Since:
        0.6.1
        See Also:
        scanLeft(BinaryOperator)
      • chain

        public <U> U chain​(Function<? super S,​U> mapper)
        Applies the supplied function to this stream and returns the result of the function.

        This method can be used to add more functionality in the fluent style. For example, consider user-defined static method batches(stream, n) which breaks the stream into batches of given length. Normally you would write batches(StreamEx.of(input).map(...), 10).filter(...). Using the chain() method you can write in more fluent manner: StreamEx.of(input).map(...).chain(s -> batches(s, 10)).filter(...).

        You could even go further and define a method which returns a function like <T> UnaryOperator<StreamEx<T>> batches(int n) and use it like this: StreamEx.of(input).map(...).chain(batches(10)).filter(...).

        Type Parameters:
        U - the type of the function result.
        Parameters:
        mapper - function to invoke.
        Returns:
        the result of the function invocation.