Class AbstractStreamEx<T,S extends AbstractStreamEx<T,S>>
- java.lang.Object
-
- one.util.streamex.BaseStreamEx<T,java.util.stream.Stream<T>,java.util.Spliterator<T>,S>
-
- one.util.streamex.AbstractStreamEx<T,S>
-
- Type Parameters:
T
- the type of the stream elementsS
- the type of the stream extendingAbstractStreamEx
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.lang.Iterable<T>
,java.util.stream.BaseStream<T,java.util.stream.Stream<T>>
,java.util.stream.Stream<T>
- Direct Known Subclasses:
EntryStream
,StreamEx
public abstract class AbstractStreamEx<T,S extends AbstractStreamEx<T,S>> extends BaseStreamEx<T,java.util.stream.Stream<T>,java.util.Spliterator<T>,S> implements java.util.stream.Stream<T>, java.lang.Iterable<T>
Base class providing common functionality forStreamEx
andEntryStream
.
-
-
Field Summary
-
Fields inherited from class one.util.streamex.BaseStreamEx
CONSUMED_MESSAGE, context, spliterator
-
-
Constructor Summary
Constructors Constructor Description AbstractStreamEx(java.util.Spliterator<? extends T> spliterator, StreamContext context)
AbstractStreamEx(java.util.stream.Stream<? extends T> stream, StreamContext context)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) static <K,V,M extends java.util.Map<K,V>>
voidaddToMap(M map, K key, V val)
boolean
allMatch(java.util.function.Predicate<? super T> predicate)
boolean
anyMatch(java.util.function.Predicate<? super T> predicate)
S
append(java.util.stream.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.(package private) S
appendSpliterator(java.util.stream.Stream<? extends T> other, java.util.Spliterator<? extends T> right)
<U> U
chain(java.util.function.Function<? super S,U> mapper)
Applies the supplied function to this stream and returns the result of the function.<R> R
collect(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,? super T> accumulator, java.util.function.BiConsumer<R,R> combiner)
<R,A>
Rcollect(java.util.stream.Collector<? super T,A,R> collector)
long
count()
long
count(java.util.function.Predicate<? super T> predicate)
Counts the number of elements in the stream that satisfy the predicate.(package private) java.util.stream.Stream<T>
createStream()
S
distinct()
S
distinct(long atLeast)
Returns aStreamEx
consisting of the distinct elements (according toObject.equals(Object)
) which appear at least specified number of times in this stream.S
distinct(java.util.function.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).S
dropWhile(java.util.function.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.S
filter(java.util.function.Predicate<? super T> predicate)
java.util.Optional<T>
findAny()
java.util.Optional<T>
findAny(java.util.function.Predicate<? super T> predicate)
Returns anOptional
describing some element of the stream, which matches given predicate, or an emptyOptional
if there's no matching element.java.util.Optional<T>
findFirst()
java.util.Optional<T>
findFirst(java.util.function.Predicate<? super T> predicate)
Returns anOptional
describing the first element of this stream, which matches given predicate, or an emptyOptional
if there's no matching element.<R> StreamEx<R>
flatArray(java.util.function.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.<R> StreamEx<R>
flatCollection(java.util.function.Function<? super T,? extends java.util.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.<R> StreamEx<R>
flatMap(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> mapper)
DoubleStreamEx
flatMapToDouble(java.util.function.Function<? super T,? extends java.util.stream.DoubleStream> mapper)
IntStreamEx
flatMapToInt(java.util.function.Function<? super T,? extends java.util.stream.IntStream> mapper)
LongStreamEx
flatMapToLong(java.util.function.Function<? super T,? extends java.util.stream.LongStream> mapper)
java.util.Optional<T>
foldLeft(java.util.function.BinaryOperator<T> accumulator)
Folds the elements of this stream using the provided accumulation function, going left to right.<U> U
foldLeft(U seed, java.util.function.BiFunction<U,? super T,U> accumulator)
Folds the elements of this stream using the provided seed object and accumulation function, going left to right.java.util.Optional<T>
foldRight(java.util.function.BinaryOperator<T> accumulator)
Folds the elements of this stream using the provided accumulation function, going right to left.<U> U
foldRight(U seed, java.util.function.BiFunction<? super T,U,U> accumulator)
Folds the elements of this stream using the provided seed object and accumulation function, going right to left.void
forEach(java.util.function.Consumer<? super T> action)
void
forEachOrdered(java.util.function.Consumer<? super T> action)
S
ifEmpty(java.util.stream.Stream<? extends T> other)
Returns a stream whose content is the same as this stream, except the case when this stream is empty.(package private) S
ifEmpty(java.util.stream.Stream<? extends T> other, java.util.Spliterator<? extends T> right)
java.util.OptionalLong
indexOf(java.util.function.Predicate<? super T> predicate)
Returns anOptionalLong
describing the zero-based index of the first element of this stream, which matches given predicate, or an emptyOptionalLong
if there's no matching element.java.util.OptionalLong
indexOf(T element)
Returns anOptionalLong
describing the zero-based index of the first element of this stream, which equals to the given element, or an emptyOptionalLong
if there's no matching element.S
intersperse(T delimiter)
Returns a new stream containing all the elements of the original stream interspersed with given delimiter.java.util.Iterator<T>
iterator()
S
limit(long maxSize)
<R> StreamEx<R>
map(java.util.function.Function<? super T,? extends R> mapper)
<R> StreamEx<R>
mapPartial(java.util.function.Function<? super T,? extends java.util.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.DoubleStreamEx
mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper)
IntStreamEx
mapToInt(java.util.function.ToIntFunction<? super T> mapper)
LongStreamEx
mapToLong(java.util.function.ToLongFunction<? super T> mapper)
java.util.Optional<T>
max(java.util.Comparator<? super T> comparator)
<V extends java.lang.Comparable<? super V>>
java.util.Optional<T>maxBy(java.util.function.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.java.util.Optional<T>
maxByDouble(java.util.function.ToDoubleFunction<? super T> keyExtractor)
Returns the maximum element of this stream according to the double values extracted by provided function.java.util.Optional<T>
maxByInt(java.util.function.ToIntFunction<? super T> keyExtractor)
Returns the maximum element of this stream according to the int values extracted by provided function.java.util.Optional<T>
maxByLong(java.util.function.ToLongFunction<? super T> keyExtractor)
Returns the maximum element of this stream according to the long values extracted by provided function.java.util.Optional<T>
min(java.util.Comparator<? super T> comparator)
<V extends java.lang.Comparable<? super V>>
java.util.Optional<T>minBy(java.util.function.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.java.util.Optional<T>
minByDouble(java.util.function.ToDoubleFunction<? super T> keyExtractor)
Returns the minimum element of this stream according to the double values extracted by provided function.java.util.Optional<T>
minByInt(java.util.function.ToIntFunction<? super T> keyExtractor)
Returns the minimum element of this stream according to the int values extracted by provided function.java.util.Optional<T>
minByLong(java.util.function.ToLongFunction<? super T> keyExtractor)
Returns the minimum element of this stream according to the long values extracted by provided function.boolean
noneMatch(java.util.function.Predicate<? super T> predicate)
S
nonNull()
Returns a stream consisting of the elements of this stream that aren't null.S
onClose(java.lang.Runnable closeHandler)
<R> StreamEx<R>
pairMap(java.util.function.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.S
parallel()
S
parallel(java.util.concurrent.ForkJoinPool fjp)
Returns an equivalent stream that is parallel and bound to the suppliedForkJoinPool
.S
peek(java.util.function.Consumer<? super T> action)
S
prefix(java.util.function.BinaryOperator<T> op)
Returns a stream containing cumulative results of applying the accumulation function going left to right.S
prepend(java.util.stream.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.(package private) S
prependSpliterator(java.util.stream.Stream<? extends T> other, java.util.Spliterator<? extends T> left)
(package private) <R,A>
RrawCollect(java.util.stream.Collector<? super T,A,R> collector)
java.util.Optional<T>
reduce(java.util.function.BinaryOperator<T> accumulator)
T
reduce(T identity, java.util.function.BinaryOperator<T> accumulator)
<U> U
reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator, java.util.function.BinaryOperator<U> combiner)
java.util.Optional<T>
reduceWithZero(T zero, java.util.function.BinaryOperator<T> accumulator)
Performs a possibly short-circuiting reduction of the stream elements using the providedBinaryOperator
.T
reduceWithZero(T zero, T identity, java.util.function.BinaryOperator<T> accumulator)
Performs a possibly short-circuiting reduction of the stream elements using the provided identity value and aBinaryOperator
.S
remove(java.util.function.Predicate<? super T> predicate)
Returns a stream consisting of the elements of this stream that don't match the given predicate.S
reverseSorted(java.util.Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, sorted in descending order according to the providedComparator
.java.util.List<T>
scanLeft(java.util.function.BinaryOperator<T> accumulator)
Produces a list containing cumulative results of applying the accumulation function going left to right.<U> java.util.List<U>
scanLeft(U seed, java.util.function.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.java.util.List<T>
scanRight(java.util.function.BinaryOperator<T> accumulator)
Produces a collection containing cumulative results of applying the accumulation function going right to left.<U> java.util.List<U>
scanRight(U seed, java.util.function.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.S
sequential()
S
skip(long n)
S
sorted()
S
sorted(java.util.Comparator<? super T> comparator)
<V extends java.lang.Comparable<? super V>>
SsortedBy(java.util.function.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.S
sortedByDouble(java.util.function.ToDoubleFunction<? super T> keyExtractor)
Returns a stream consisting of the elements of this stream, sorted according to the double values extracted by provided function.S
sortedByInt(java.util.function.ToIntFunction<? super T> keyExtractor)
Returns a stream consisting of the elements of this stream, sorted according to the int values extracted by provided function.S
sortedByLong(java.util.function.ToLongFunction<? super T> keyExtractor)
Returns a stream consisting of the elements of this stream, sorted according to the long values extracted by provided function.(package private) abstract S
supply(java.util.Spliterator<T> spliterator)
(package private) abstract S
supply(java.util.stream.Stream<T> stream)
S
takeWhile(java.util.function.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.S
takeWhileInclusive(java.util.function.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).java.lang.Object[]
toArray()
<A> A[]
toArray(java.util.function.IntFunction<A[]> generator)
<C extends java.util.Collection<T>>
CtoCollection(java.util.function.Supplier<C> collectionFactory)
Returns aCollection
containing the elements of this stream.<C extends java.util.Collection<T>,R>
RtoCollectionAndThen(java.util.function.Supplier<C> collectionFactory, java.util.function.Function<? super C,R> finisher)
Creates a customCollection
containing the elements of this stream, then performs finishing transformation and returns its result.java.util.List<T>
toImmutableList()
Returns an immutableList
containing the elements of this stream.java.util.Set<T>
toImmutableSet()
Returns an immutableSet
containing the elements of this stream.java.util.List<T>
toList()
Returns aList
containing the elements of this stream.<R> R
toListAndThen(java.util.function.Function<? super java.util.List<T>,R> finisher)
Creates aList
containing the elements of this stream, then performs finishing transformation and returns its result.(package private) <K,V,M extends java.util.Map<K,V>>
MtoMapThrowing(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valMapper, M map)
java.util.List<T>
toMutableList()
Returns a mutableList
containing the elements of this stream.java.util.Set<T>
toMutableSet()
Returns a mutableSet
containing the elements of this stream.java.util.Set<T>
toSet()
Returns aSet
containing the elements of this stream.<R> R
toSetAndThen(java.util.function.Function<? super java.util.Set<T>,R> finisher)
Creates aSet
containing the elements of this stream, then performs finishing transformation and returns its result.S
unordered()
-
Methods inherited from class one.util.streamex.BaseStreamEx
close, isParallel, spliterator, stream
-
-
-
-
Constructor Detail
-
AbstractStreamEx
AbstractStreamEx(java.util.stream.Stream<? extends T> stream, StreamContext context)
-
AbstractStreamEx
AbstractStreamEx(java.util.Spliterator<? extends T> spliterator, StreamContext context)
-
-
Method Detail
-
createStream
final java.util.stream.Stream<T> createStream()
- Specified by:
createStream
in classBaseStreamEx<T,java.util.stream.Stream<T>,java.util.Spliterator<T>,S extends AbstractStreamEx<T,S>>
-
toMapThrowing
final <K,V,M extends java.util.Map<K,V>> M toMapThrowing(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valMapper, M map)
-
addToMap
static <K,V,M extends java.util.Map<K,V>> void addToMap(M map, K key, V val)
-
rawCollect
<R,A> R rawCollect(java.util.stream.Collector<? super T,A,R> collector)
-
appendSpliterator
S appendSpliterator(java.util.stream.Stream<? extends T> other, java.util.Spliterator<? extends T> right)
-
prependSpliterator
S prependSpliterator(java.util.stream.Stream<? extends T> other, java.util.Spliterator<? extends T> left)
-
ifEmpty
S ifEmpty(java.util.stream.Stream<? extends T> other, java.util.Spliterator<? extends T> right)
-
iterator
public java.util.Iterator<T> iterator()
-
sequential
public S sequential()
- Specified by:
sequential
in interfacejava.util.stream.BaseStream<T,S extends AbstractStreamEx<T,S>>
- Overrides:
sequential
in classBaseStreamEx<T,java.util.stream.Stream<T>,java.util.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 customForkJoinPool
and its terminal operation will be executed in common pool.- Specified by:
parallel
in interfacejava.util.stream.BaseStream<T,S extends AbstractStreamEx<T,S>>
- Overrides:
parallel
in classBaseStreamEx<T,java.util.stream.Stream<T>,java.util.Spliterator<T>,S extends AbstractStreamEx<T,S>>
-
parallel
public S parallel(java.util.concurrent.ForkJoinPool fjp)
Description copied from class:BaseStreamEx
Returns an equivalent stream that is parallel and bound to the suppliedForkJoinPool
.This is an intermediate operation.
The terminal operation of this stream or any derived stream (except the streams created via
BaseStreamEx.parallel()
orBaseStreamEx.sequential()
methods) will be executed inside the suppliedForkJoinPool
. If current thread does not belong to that pool, it will wait till calculation finishes.- Overrides:
parallel
in classBaseStreamEx<T,java.util.stream.Stream<T>,java.util.Spliterator<T>,S extends AbstractStreamEx<T,S>>
- Parameters:
fjp
- aForkJoinPool
to submit the stream operation to.- Returns:
- a parallel stream bound to the supplied
ForkJoinPool
-
unordered
public S unordered()
- Specified by:
unordered
in interfacejava.util.stream.BaseStream<T,S extends AbstractStreamEx<T,S>>
- Overrides:
unordered
in classBaseStreamEx<T,java.util.stream.Stream<T>,java.util.Spliterator<T>,S extends AbstractStreamEx<T,S>>
-
onClose
public S onClose(java.lang.Runnable closeHandler)
- Specified by:
onClose
in interfacejava.util.stream.BaseStream<T,S extends AbstractStreamEx<T,S>>
- Overrides:
onClose
in classBaseStreamEx<T,java.util.stream.Stream<T>,java.util.Spliterator<T>,S extends AbstractStreamEx<T,S>>
-
filter
public S filter(java.util.function.Predicate<? super T> predicate)
- Specified by:
filter
in interfacejava.util.stream.Stream<T>
- See Also:
nonNull()
,remove(Predicate)
,StreamEx.select(Class)
-
flatMap
public <R> StreamEx<R> flatMap(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> mapper)
- Specified by:
flatMap
in interfacejava.util.stream.Stream<T>
-
map
public <R> StreamEx<R> map(java.util.function.Function<? super T,? extends R> mapper)
- Specified by:
map
in interfacejava.util.stream.Stream<T>
-
mapToInt
public IntStreamEx mapToInt(java.util.function.ToIntFunction<? super T> mapper)
- Specified by:
mapToInt
in interfacejava.util.stream.Stream<T>
-
mapToLong
public LongStreamEx mapToLong(java.util.function.ToLongFunction<? super T> mapper)
- Specified by:
mapToLong
in interfacejava.util.stream.Stream<T>
-
mapToDouble
public DoubleStreamEx mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper)
- Specified by:
mapToDouble
in interfacejava.util.stream.Stream<T>
-
flatMapToInt
public IntStreamEx flatMapToInt(java.util.function.Function<? super T,? extends java.util.stream.IntStream> mapper)
- Specified by:
flatMapToInt
in interfacejava.util.stream.Stream<T>
-
flatMapToLong
public LongStreamEx flatMapToLong(java.util.function.Function<? super T,? extends java.util.stream.LongStream> mapper)
- Specified by:
flatMapToLong
in interfacejava.util.stream.Stream<T>
-
flatMapToDouble
public DoubleStreamEx flatMapToDouble(java.util.function.Function<? super T,? extends java.util.stream.DoubleStream> mapper)
- Specified by:
flatMapToDouble
in interfacejava.util.stream.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(java.util.function.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 aStreamEx
consisting of the distinct elements (according toObject.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 todistinct()
.- Returns:
- the new stream
- Since:
- 0.3.1
- See Also:
distinct()
-
sorted
public S sorted(java.util.Comparator<? super T> comparator)
- Specified by:
sorted
in interfacejava.util.stream.Stream<T>
-
peek
public S peek(java.util.function.Consumer<? super T> action)
- Specified by:
peek
in interfacejava.util.stream.Stream<T>
-
forEach
public void forEach(java.util.function.Consumer<? super T> action)
-
forEachOrdered
public void forEachOrdered(java.util.function.Consumer<? super T> action)
- Specified by:
forEachOrdered
in interfacejava.util.stream.Stream<T>
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArray
in interfacejava.util.stream.Stream<T>
-
toArray
public <A> A[] toArray(java.util.function.IntFunction<A[]> generator)
- Specified by:
toArray
in interfacejava.util.stream.Stream<T>
-
reduce
public T reduce(T identity, java.util.function.BinaryOperator<T> accumulator)
- Specified by:
reduce
in interfacejava.util.stream.Stream<T>
-
reduce
public java.util.Optional<T> reduce(java.util.function.BinaryOperator<T> accumulator)
- Specified by:
reduce
in interfacejava.util.stream.Stream<T>
-
reduce
public <U> U reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator, java.util.function.BinaryOperator<U> combiner)
- Specified by:
reduce
in interfacejava.util.stream.Stream<T>
-
reduceWithZero
public java.util.Optional<T> reduceWithZero(T zero, java.util.function.BinaryOperator<T> accumulator)
Performs a possibly short-circuiting reduction of the stream elements using the providedBinaryOperator
. The result is described as anOptional<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 elementaccumulator
- 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:
java.lang.NullPointerException
- if accumulator is null or the result of reduction is null- Since:
- 0.7.3
- See Also:
MoreCollectors.reducingWithZero(Object, BinaryOperator)
,reduceWithZero(Object, Object, BinaryOperator)
,reduce(BinaryOperator)
-
reduceWithZero
public T reduceWithZero(T zero, T identity, java.util.function.BinaryOperator<T> accumulator)
Performs a possibly short-circuiting reduction of the stream elements using the provided identity value and aBinaryOperator
.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 elementidentity
- an identity element. For allt
,accumulator.apply(t, identity)
is equal toaccumulator.apply(identity, t)
and is equal tot
.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:
java.lang.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)
-
collect
public <R> R collect(java.util.function.Supplier<R> supplier, java.util.function.BiConsumer<R,? super T> accumulator, java.util.function.BiConsumer<R,R> combiner)
- Specified by:
collect
in interfacejava.util.stream.Stream<T>
-
collect
public <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
If special short-circuiting collector is passed, this operation becomes short-circuiting as well.
- Specified by:
collect
in interfacejava.util.stream.Stream<T>
-
min
public java.util.Optional<T> min(java.util.Comparator<? super T> comparator)
- Specified by:
min
in interfacejava.util.stream.Stream<T>
-
max
public java.util.Optional<T> max(java.util.Comparator<? super T> comparator)
- Specified by:
max
in interfacejava.util.stream.Stream<T>
-
count
public long count()
- Specified by:
count
in interfacejava.util.stream.Stream<T>
-
count
public long count(java.util.function.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(java.util.function.Predicate<? super T> predicate)
- Specified by:
anyMatch
in interfacejava.util.stream.Stream<T>
-
allMatch
public boolean allMatch(java.util.function.Predicate<? super T> predicate)
- Specified by:
allMatch
in interfacejava.util.stream.Stream<T>
-
noneMatch
public boolean noneMatch(java.util.function.Predicate<? super T> predicate)
- Specified by:
noneMatch
in interfacejava.util.stream.Stream<T>
-
findFirst
public java.util.Optional<T> findFirst()
- Specified by:
findFirst
in interfacejava.util.stream.Stream<T>
-
findAny
public java.util.Optional<T> findAny()
- Specified by:
findAny
in interfacejava.util.stream.Stream<T>
-
indexOf
public java.util.OptionalLong indexOf(T element)
Returns anOptionalLong
describing the zero-based index of the first element of this stream, which equals to the given element, or an emptyOptionalLong
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 emptyOptionalLong
if there's no matching element. - Since:
- 0.4.0
- See Also:
indexOf(Predicate)
-
indexOf
public java.util.OptionalLong indexOf(java.util.function.Predicate<? super T> predicate)
Returns anOptionalLong
describing the zero-based index of the first element of this stream, which matches given predicate, or an emptyOptionalLong
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 emptyOptionalLong
if there's no matching element. - Since:
- 0.4.0
- See Also:
findFirst(Predicate)
,indexOf(Object)
-
flatCollection
public <R> StreamEx<R> flatCollection(java.util.function.Function<? super T,? extends java.util.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 isnull
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 aCollection
of new values- Returns:
- the new stream
-
flatArray
public <R> StreamEx<R> flatArray(java.util.function.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 isnull
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(java.util.function.Function<? super T,? extends java.util.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(java.util.function.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(java.util.function.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:
filter(Predicate)
,nonNull()
,StreamEx.select(Class)
-
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:
filter(Predicate)
,remove(Predicate)
,StreamEx.select(Class)
-
findAny
public java.util.Optional<T> findAny(java.util.function.Predicate<? super T> predicate)
Returns anOptional
describing some element of the stream, which matches given predicate, or an emptyOptional
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 emptyOptional
if there's no matching element - Throws:
java.lang.NullPointerException
- if the element selected is null- See Also:
findAny()
,findFirst(Predicate)
-
findFirst
public java.util.Optional<T> findFirst(java.util.function.Predicate<? super T> predicate)
Returns anOptional
describing the first element of this stream, which matches given predicate, or an emptyOptional
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 emptyOptional
if there's no matching element - Throws:
java.lang.NullPointerException
- if the element selected is null- See Also:
findFirst()
-
reverseSorted
public S reverseSorted(java.util.Comparator<? super T> comparator)
Returns a stream consisting of the elements of this stream, sorted in descending order according to the providedComparator
.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 , statelessComparator
to be used to compare stream elements- Returns:
- the new stream
-
sortedBy
public <V extends java.lang.Comparable<? super V>> S sortedBy(java.util.function.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 theComparable
sort key- Parameters:
keyExtractor
- a non-interfering , stateless function to be used to extract sorting keys- Returns:
- the new stream
-
sortedByInt
public S sortedByInt(java.util.function.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(java.util.function.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(java.util.function.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 java.lang.Comparable<? super V>> java.util.Optional<T> minBy(java.util.function.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 emptyOptional
if the stream is empty - Throws:
java.lang.NullPointerException
- if the minimum element is null
-
minByInt
public java.util.Optional<T> minByInt(java.util.function.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 emptyOptional
if the stream is empty - Throws:
java.lang.NullPointerException
- if the minimum element is null
-
minByLong
public java.util.Optional<T> minByLong(java.util.function.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 emptyOptional
if the stream is empty - Throws:
java.lang.NullPointerException
- if the minimum element is null
-
minByDouble
public java.util.Optional<T> minByDouble(java.util.function.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 emptyOptional
if the stream is empty - Throws:
java.lang.NullPointerException
- if the minimum element is null
-
maxBy
public <V extends java.lang.Comparable<? super V>> java.util.Optional<T> maxBy(java.util.function.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 emptyOptional
if the stream is empty - Throws:
java.lang.NullPointerException
- if the maximum element is null
-
maxByInt
public java.util.Optional<T> maxByInt(java.util.function.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 emptyOptional
if the stream is empty - Throws:
java.lang.NullPointerException
- if the maximum element is null
-
maxByLong
public java.util.Optional<T> maxByLong(java.util.function.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 emptyOptional
if the stream is empty - Throws:
java.lang.NullPointerException
- if the maximum element is null
-
maxByDouble
public java.util.Optional<T> maxByDouble(java.util.function.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 emptyOptional
if the stream is empty - Throws:
java.lang.NullPointerException
- if the maximum element is null
-
append
public S append(java.util.stream.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(java.util.stream.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(java.util.stream.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 java.util.List<T> toList()
Returns aList
containing the elements of this stream. There are no guarantees on the type, mutability, serializability, or thread-safety of the returnedList
; if more control over the returnedList
is required, usetoCollection(Supplier)
.This is a terminal operation.
- Returns:
- a
List
containing the elements of this stream - See Also:
Collectors.toList()
,toMutableList()
,toImmutableList()
-
toMutableList
public java.util.List<T> toMutableList()
Returns a mutableList
containing the elements of this stream. There are no guarantees on the type, serializability, or thread-safety of the returnedList
; if more control over the returnedList
is required, usetoCollection(Supplier)
.This is a terminal operation.
- Returns:
- a
List
containing the elements of this stream - Since:
- 0.8.0
- See Also:
Collectors.toList()
,toImmutableList()
-
toImmutableList
public java.util.List<T> toImmutableList()
Returns an immutableList
containing the elements of this stream. There's no guarantees on exact type of the returnedList
. The returnedList
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()
,toMutableList()
-
toListAndThen
public <R> R toListAndThen(java.util.function.Function<? super java.util.List<T>,R> finisher)
Creates aList
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 theList
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 java.util.Set<T> toSet()
Returns aSet
containing the elements of this stream. There are no guarantees on the type, mutability, serializability, or thread-safety of the returnedSet
; if more control over the returnedSet
is required, usetoCollection(Supplier)
.This is a terminal operation.
- Returns:
- a
Set
containing the elements of this stream - See Also:
Collectors.toSet()
,toMutableSet()
,toImmutableSet()
-
toMutableSet
public java.util.Set<T> toMutableSet()
Returns a mutableSet
containing the elements of this stream. There are no guarantees on the type, serializability, or thread-safety of the returnedSet
; if more control over the returnedSet
is required, usetoCollection(Supplier)
.This is a terminal operation.
- Returns:
- a
Set
containing the elements of this stream - Since:
- 0.8.0
- See Also:
Collectors.toSet()
,toImmutableSet()
-
toImmutableSet
public java.util.Set<T> toImmutableSet()
Returns an immutableSet
containing the elements of this stream. There's no guarantees on exact type of the returnedSet
. In particular, no specific element order in the resulting set is guaranteed. The returnedSet
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()
,toMutableSet()
-
toSetAndThen
public <R> R toSetAndThen(java.util.function.Function<? super java.util.Set<T>,R> finisher)
Creates aSet
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 theSet
created.This is a terminal operation.
- Type Parameters:
R
- the result type- Parameters:
finisher
- a function to be applied to the intermediateSet
- 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 java.util.Collection<T>,R> R toCollectionAndThen(java.util.function.Supplier<C> collectionFactory, java.util.function.Function<? super C,R> finisher)
Creates a customCollection
containing the elements of this stream, then performs finishing transformation and returns its result. TheCollection
is created by the provided factory.This is a terminal operation.
- Type Parameters:
C
- the type of the resultingCollection
R
- the result type- Parameters:
collectionFactory
- aSupplier
which returns a new, emptyCollection
of the appropriate typefinisher
- a function to be applied to the intermediateCollection
- 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 java.util.Collection<T>> C toCollection(java.util.function.Supplier<C> collectionFactory)
Returns aCollection
containing the elements of this stream. TheCollection
is created by the provided factory.This is a terminal operation.
- Type Parameters:
C
- the type of the resultingCollection
- Parameters:
collectionFactory
- aSupplier
which returns a new, emptyCollection
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, java.util.function.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 valueaccumulator
- 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 java.util.Optional<T> foldLeft(java.util.function.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, java.util.function.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 valueaccumulator
- 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 java.util.Optional<T> foldRight(java.util.function.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> java.util.List<U> scanLeft(U seed, java.util.function.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 valueaccumulator
- 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 java.util.List<T> scanLeft(java.util.function.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> java.util.List<U> scanRight(U seed, java.util.function.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 valueaccumulator
- 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 java.util.List<T> scanRight(java.util.function.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(java.util.function.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 interfacejava.util.stream.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(java.util.function.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(java.util.function.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 interfacejava.util.stream.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(java.util.function.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 unlikescanLeft
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(java.util.function.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 writebatches(StreamEx.of(input).map(...), 10).filter(...)
. Using thechain()
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 classBaseStreamEx<T,java.util.stream.Stream<T>,java.util.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.
-
-