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

java.lang.Object
one.util.streamex.BaseStreamEx<T,Stream<T>,Spliterator<T>,S>
one.util.streamex.AbstractStreamEx<T,S>
Type Parameters:
T - the type of the stream elements
S - the type of the stream extending AbstractStreamEx
All Implemented Interfaces:
AutoCloseable, Iterable<T>, BaseStream<T,Stream<T>>, Stream<T>
Direct Known Subclasses:
EntryStream, StreamEx

public abstract class AbstractStreamEx<T,S extends AbstractStreamEx<T,S>> extends BaseStreamEx<T,Stream<T>,Spliterator<T>,S> implements Stream<T>, Iterable<T>
Base class providing common functionality for StreamEx and EntryStream.
  • Constructor Details

  • Method Details

    • createStream

      final Stream<T> createStream()
      Specified by:
      createStream in class BaseStreamEx<T,Stream<T>,Spliterator<T>,S extends AbstractStreamEx<T,S>>
    • toMapThrowing

      final <K, V, M extends Map<K, V>> M toMapThrowing(Function<? super T,? extends K> keyMapper, Function<? super T,? extends V> valMapper, M map)
    • addToMap

      static <K, V, M extends Map<K, V>> void addToMap(M map, K key, V val)
    • rawCollect

      <R, A> R rawCollect(Collector<? super T,A,R> collector)
    • appendSpliterator

      S appendSpliterator(Stream<? extends T> other, Spliterator<? extends T> right)
    • prependSpliterator

      S prependSpliterator(Stream<? extends T> other, Spliterator<? extends T> left)
    • ifEmpty

      S ifEmpty(Stream<? extends T> other, Spliterator<? extends T> right)
    • supply

      abstract S supply(Stream<T> stream)
    • supply

      abstract S supply(Spliterator<T> spliterator)
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface BaseStream<T,S extends AbstractStreamEx<T,S>>
      Specified by:
      iterator in interface Iterable<T>
    • sequential

      public S sequential()
      Specified by:
      sequential in interface BaseStream<T,S extends AbstractStreamEx<T,S>>
      Overrides:
      sequential in class BaseStreamEx<T,Stream<T>,Spliterator<T>,S extends AbstractStreamEx<T,S>>
    • parallel

      public S parallel()
      Description copied from class: BaseStreamEx

      If this stream was created using BaseStreamEx.parallel(ForkJoinPool), the new stream forgets about supplied custom ForkJoinPool and its terminal operation will be executed in common pool.

      Specified by:
      parallel in interface BaseStream<T,S extends AbstractStreamEx<T,S>>
      Overrides:
      parallel in class BaseStreamEx<T,Stream<T>,Spliterator<T>,S extends AbstractStreamEx<T,S>>
    • parallel

      public S parallel(ForkJoinPool fjp)
      Description copied from class: BaseStreamEx
      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 BaseStreamEx.parallel() or BaseStreamEx.sequential() methods) will be executed inside the supplied ForkJoinPool. If current thread does not belong to that pool, it will wait till calculation finishes.

      Overrides:
      parallel in class BaseStreamEx<T,Stream<T>,Spliterator<T>,S extends AbstractStreamEx<T,S>>
      Parameters:
      fjp - a ForkJoinPool to submit the stream operation to.
      Returns:
      a parallel stream bound to the supplied ForkJoinPool
    • unordered

      public S unordered()
      Specified by:
      unordered in interface BaseStream<T,S extends AbstractStreamEx<T,S>>
      Overrides:
      unordered in class BaseStreamEx<T,Stream<T>,Spliterator<T>,S extends AbstractStreamEx<T,S>>
    • onClose

      public S onClose(Runnable closeHandler)
      Specified by:
      onClose in interface BaseStream<T,S extends AbstractStreamEx<T,S>>
      Overrides:
      onClose in class BaseStreamEx<T,Stream<T>,Spliterator<T>,S extends AbstractStreamEx<T,S>>
    • filter

      public S filter(Predicate<? super T> predicate)
      Specified by:
      filter in interface Stream<T>
      See Also:
    • flatMap

      public <R> StreamEx<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
      Specified by:
      flatMap in interface Stream<T>
    • map

      public <R> StreamEx<R> map(Function<? super T,? extends R> mapper)
      Specified by:
      map in interface Stream<T>
    • mapToInt

      public IntStreamEx mapToInt(ToIntFunction<? super T> mapper)
      Specified by:
      mapToInt in interface Stream<T>
    • mapToLong

      public LongStreamEx mapToLong(ToLongFunction<? super T> mapper)
      Specified by:
      mapToLong in interface Stream<T>
    • mapToDouble

      public DoubleStreamEx mapToDouble(ToDoubleFunction<? super T> mapper)
      Specified by:
      mapToDouble in interface Stream<T>
    • flatMapToInt

      public IntStreamEx flatMapToInt(Function<? super T,? extends IntStream> mapper)
      Specified by:
      flatMapToInt in interface Stream<T>
    • flatMapToLong

      public LongStreamEx flatMapToLong(Function<? super T,? extends LongStream> mapper)
      Specified by:
      flatMapToLong in interface Stream<T>
    • flatMapToDouble

      public DoubleStreamEx flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
      Specified by:
      flatMapToDouble in interface Stream<T>
    • 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:
    • sorted

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

      public S sorted(Comparator<? super T> comparator)
      Specified by:
      sorted in interface Stream<T>
    • peek

      public S peek(Consumer<? super T> action)
      Specified by:
      peek 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>
    • forEach

      public void forEach(Consumer<? super T> action)
      Specified by:
      forEach in interface Iterable<T>
      Specified by:
      forEach in interface Stream<T>
    • forEachOrdered

      public void forEachOrdered(Consumer<? super T> action)
      Specified by:
      forEachOrdered in interface Stream<T>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Stream<T>
    • toArray

      public <A> A[] toArray(IntFunction<A[]> generator)
      Specified by:
      toArray in interface Stream<T>
    • reduce

      public T reduce(T identity, BinaryOperator<T> accumulator)
      Specified by:
      reduce in interface Stream<T>
    • reduce

      public Optional<T> reduce(BinaryOperator<T> accumulator)
      Specified by:
      reduce in interface Stream<T>
    • reduce

      public <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
      Specified by:
      reduce in interface Stream<T>
    • reduceWithZero

      public Optional<T> reduceWithZero(T zero, BinaryOperator<T> accumulator)
      Performs a possibly short-circuiting reduction of the stream elements using the provided BinaryOperator. The result is described as an Optional<T>.

      This is a short-circuiting terminal operation. It behaves like reduce(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
      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:
    • 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:
    • collect

      public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
      Specified by:
      collect in interface Stream<T>
    • collect

      public <R, A> R collect(Collector<? super T,A,R> collector)

      If special short-circuiting collector is passed, this operation becomes short-circuiting as well.

      Specified by:
      collect in interface Stream<T>
    • min

      public Optional<T> min(Comparator<? super T> comparator)
      Specified by:
      min in interface Stream<T>
    • max

      public Optional<T> max(Comparator<? super T> comparator)
      Specified by:
      max in interface Stream<T>
    • count

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

      public long count(Predicate<? super T> predicate)
      Counts the number of elements in the stream that satisfy the predicate.

      This is a terminal operation.

      Parameters:
      predicate - a non-interfering , stateless predicate to apply to stream elements. Only elements passing the predicate will be counted.
      Returns:
      the count of elements in this stream satisfying the predicate.
    • anyMatch

      public boolean anyMatch(Predicate<? super T> predicate)
      Specified by:
      anyMatch in interface Stream<T>
    • allMatch

      public boolean allMatch(Predicate<? super T> predicate)
      Specified by:
      allMatch in interface Stream<T>
    • noneMatch

      public boolean noneMatch(Predicate<? super T> predicate)
      Specified by:
      noneMatch in interface Stream<T>
    • findFirst

      public Optional<T> findFirst()
      Specified by:
      findFirst in interface Stream<T>
    • findAny

      public Optional<T> findAny()
      Specified by:
      findAny 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

      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:
    • 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
    • remove

      public S remove(Predicate<? super T> predicate)
      Returns a stream consisting of the elements of this stream that don't match the given predicate.

      This is an intermediate operation.

      Parameters:
      predicate - a non-interfering , stateless predicate to apply to each element to determine if it should be excluded
      Returns:
      the new stream
      See Also:
    • nonNull

      public S nonNull()
      Returns a stream consisting of the elements of this stream that aren't null.

      This is an intermediate operation.

      Returns:
      the new stream
      See Also:
    • 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:
    • 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:
    • 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:
    • 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:
    • ifEmpty

      public S ifEmpty(Stream<? extends T> other)
      Returns a stream whose content 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 whose content 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. There are no guarantees on the type, mutability, serializability, or thread-safety of the returned List; if more control over the returned List is required, use toCollection(Supplier).

      This is a terminal operation.

      Specified by:
      toList in interface Stream<T>
      Returns:
      a List containing the elements of this stream
      See Also:
    • toMutableList

      public List<T> toMutableList()
      Returns a mutable List containing the elements of this stream. There are no guarantees on the type, serializability, or thread-safety of the returned List; 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
      Since:
      0.8.0
      See Also:
    • 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:
    • 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:
    • toSet

      public Set<T> toSet()
      Returns a Set containing the elements of this stream. There are no guarantees on the type, mutability, serializability, or thread-safety of the returned Set; 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:
    • toMutableSet

      public Set<T> toMutableSet()
      Returns a mutable Set containing the elements of this stream. There are no guarantees on the type, serializability, or thread-safety of the returned Set; 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
      Since:
      0.8.0
      See Also:
    • 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:
    • 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:
    • 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

      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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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

      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:
    • 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:
    • chain

      public <U> U chain(Function<? super S,U> mapper)
      Description copied from class: BaseStreamEx
      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(...).

      Specified by:
      chain in class BaseStreamEx<T,Stream<T>,Spliterator<T>,S extends AbstractStreamEx<T,S>>
      Type Parameters:
      U - the type of the function result.
      Parameters:
      mapper - function to invoke.
      Returns:
      the result of the function invocation.