T - the type of the stream elementspublic class Stream<T>
extends java.lang.Object
implements java.io.Closeable
| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(Predicate<? super T> predicate)
Tests whether all elements match the given predicate.
|
boolean |
anyMatch(Predicate<? super T> predicate)
Tests whether any elements match the given predicate.
|
Stream<T> |
append(Stream<? extends T> stream)
Appends given
Stream to current and returns a new stream. |
<K> Stream<java.util.List<T>> |
chunkBy(Function<? super T,? extends K> classifier)
Partitions
Stream into Lists according to the given classifier function. |
void |
close()
Causes close handler to be invoked if it exists.
|
<R,A> R |
collect(Collector<? super T,A,R> collector)
Collects elements with
collector that encapsulates supplier, accumulator and combiner functions. |
<R> R |
collect(Supplier<R> supplier,
BiConsumer<R,? super T> accumulator)
Collects elements to
supplier provided container by applying the given accumulation function. |
static <T> Stream<T> |
concat(java.util.Iterator<? extends T> iterator1,
java.util.Iterator<? extends T> iterator2)
Concatenates two iterators to a stream.
|
static <T> Stream<T> |
concat(java.util.Iterator<? extends T> iterator1,
java.util.Iterator<? extends T> iterator2,
java.util.Iterator<? extends T>... rest)
Concatenates three or more iterators to a stream.
|
static <T> Stream<T> |
concat(java.util.List<? extends Stream<? extends T>> streams)
Lazily concatenates a
List of streams. |
static <T> Stream<T> |
concat(Stream<? extends T> stream1,
Stream<? extends T> stream2)
Lazily concatenates two streams.
|
long |
count()
Returns the count of elements in this stream.
|
<R> R |
custom(Function<Stream<T>,R> function)
Applies custom operator on stream.
|
Stream<T> |
distinct()
Returns
Stream with distinct elements (as determined by hashCode and equals methods). |
<K> Stream<T> |
distinctBy(Function<? super T,? extends K> classifier)
Returns
Stream with distinct elements (as determined by hashCode
and equals methods) according to the given classifier function. |
Stream<T> |
dropWhile(Predicate<? super T> predicate)
Drops elements while the predicate is true, then returns the rest.
|
Stream<T> |
dropWhileIndexed(IndexedPredicate<? super T> predicate)
Drops elements while the
IndexedPredicate is true, then returns the rest. |
Stream<T> |
dropWhileIndexed(int from,
int step,
IndexedPredicate<? super T> predicate)
Drops elements while the
IndexedPredicate is true, then returns the rest. |
static <T> Stream<T> |
empty()
Returns an empty stream.
|
Stream<T> |
equalsOnly(T object)
Returns
Stream with elements that is equality of object only. |
Stream<T> |
filter(Predicate<? super T> predicate)
Returns
Stream with elements that satisfy the given predicate. |
Stream<T> |
filterIndexed(IndexedPredicate<? super T> predicate)
Returns a
Stream with elements that satisfy the given IndexedPredicate. |
Stream<T> |
filterIndexed(int from,
int step,
IndexedPredicate<? super T> predicate)
Returns a
Stream with elements that satisfy the given IndexedPredicate. |
Stream<T> |
filterNot(Predicate<? super T> predicate)
Returns
Stream with elements that does not satisfy the given predicate. |
Optional<T> |
findFirst()
Returns the first element wrapped by
Optional class. |
T |
findFirstOrElse(T other)
Returns the first element if stream is not empty,
otherwise returns
other. |
Optional<IntPair<T>> |
findIndexed(IndexedPredicate<? super T> predicate)
Finds the first element and its index that matches the given predicate.
|
Optional<IntPair<T>> |
findIndexed(int from,
int step,
IndexedPredicate<? super T> predicate)
Finds the first element and its index that matches the given predicate.
|
Optional<T> |
findLast()
Returns the last element wrapped by
Optional class. |
Optional<T> |
findSingle()
Returns the single element wrapped by
Optional class. |
<R> Stream<R> |
flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
DoubleStream |
flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
IntStream |
flatMapToInt(Function<? super T,? extends IntStream> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
LongStream |
flatMapToLong(Function<? super T,? extends LongStream> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with the contents of a mapped stream produced by applying
the provided mapping function to each element.
|
void |
forEach(Consumer<? super T> action)
Performs the given action on each element.
|
void |
forEachIndexed(IndexedConsumer<? super T> action)
Performs the given indexed action on each element.
|
void |
forEachIndexed(int from,
int step,
IndexedConsumer<? super T> action)
Performs the given indexed action on each element.
|
static <T> Stream<T> |
generate(Supplier<T> supplier)
Creates a
Stream by elements that generated by Supplier. |
<K> Stream<java.util.Map.Entry<K,java.util.List<T>>> |
groupBy(Function<? super T,? extends K> classifier)
Partitions
Stream into Map entries according to the given classifier function. |
Stream<IntPair<T>> |
indexed()
Returns
Stream with indexed elements. |
Stream<IntPair<T>> |
indexed(int from,
int step)
Returns
Stream with indexed elements. |
static <T> Stream<T> |
iterate(T seed,
Predicate<? super T> predicate,
UnaryOperator<T> op)
Creates a
Stream by iterative application UnaryOperator function
to an initial element seed, conditioned on satisfying the supplied predicate. |
static <T> Stream<T> |
iterate(T seed,
UnaryOperator<T> op)
Creates a
Stream by iterative application UnaryOperator function
to an initial element seed. |
java.util.Iterator<? extends T> |
iterator()
Returns internal stream iterator.
|
Stream<T> |
limit(long maxSize)
Returns
Stream with first maxSize elements. |
<R> Stream<R> |
map(Function<? super T,? extends R> mapper)
Returns
Stream with elements that obtained by applying the given function. |
<R> Stream<R> |
mapIndexed(IndexedFunction<? super T,? extends R> mapper)
Returns a
Stream with elements that obtained by applying the given IndexedFunction. |
<R> Stream<R> |
mapIndexed(int from,
int step,
IndexedFunction<? super T,? extends R> mapper)
Returns a
Stream with elements that obtained by applying the given IndexedFunction. |
<R> Stream<R> |
mapMulti(BiConsumer<? super T,? super Consumer<R>> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with zero or more elements,
that passed to the provided consumer function.
|
DoubleStream |
mapMultiToDouble(BiConsumer<? super T,? super DoubleConsumer> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with zero or more elements,
that passed to the provided consumer function.
|
IntStream |
mapMultiToInt(BiConsumer<? super T,? super IntConsumer> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with zero or more elements,
that passed to the provided consumer function.
|
LongStream |
mapMultiToLong(BiConsumer<? super T,? super LongConsumer> mapper)
Returns a stream consisting of the results of replacing each element of
this stream with zero or more elements,
that passed to the provided consumer function.
|
DoubleStream |
mapToDouble(ToDoubleFunction<? super T> mapper)
Returns
DoubleStream with elements that obtained by applying the given function. |
IntStream |
mapToInt(ToIntFunction<? super T> mapper)
Returns
IntStream with elements that obtained by applying the given function. |
LongStream |
mapToLong(ToLongFunction<? super T> mapper)
Returns
LongStream with elements that obtained by applying the given function. |
Optional<T> |
max(java.util.Comparator<? super T> comparator)
Finds the maximum element according to the given comparator.
|
static <T> Stream<T> |
merge(java.util.Iterator<? extends T> iterator1,
java.util.Iterator<? extends T> iterator2,
BiFunction<? super T,? super T,ObjMerge.MergeResult> selector)
Merges elements of two iterators according to the supplied selector function.
|
static <T> Stream<T> |
merge(Stream<? extends T> stream1,
Stream<? extends T> stream2,
BiFunction<? super T,? super T,ObjMerge.MergeResult> selector)
Merges elements of two streams according to the supplied selector function.
|
Optional<T> |
min(java.util.Comparator<? super T> comparator)
Finds the minimum element according to the given comparator.
|
boolean |
noneMatch(Predicate<? super T> predicate)
Tests whether no elements match the given predicate.
|
Stream<T> |
nullsOnly()
Returns
Stream with elements that is null only. |
static <T> Stream<T> |
of(java.lang.Iterable<? extends T> iterable)
Creates a
Stream from any class that implements Iterable interface. |
static <T> Stream<T> |
of(java.util.Iterator<? extends T> iterator)
Creates a
Stream from any class that implements Iterator interface. |
static <K,V> Stream<java.util.Map.Entry<K,V>> |
of(java.util.Map<K,V> map)
Creates a
Stream from Map entries. |
static <T> Stream<T> |
of(T... elements)
Creates a
Stream from the specified values. |
static <T> Stream<T> |
ofNullable(java.lang.Iterable<? extends T> iterable)
If specified iterable is null, returns an empty
Stream,
otherwise returns a Stream containing elements of this iterable. |
static <T> Stream<T> |
ofNullable(java.util.Iterator<? extends T> iterator)
If specified iterator is null, returns an empty
Stream,
otherwise returns a Stream containing entries of this iterator. |
static <K,V> Stream<java.util.Map.Entry<K,V>> |
ofNullable(java.util.Map<K,V> map)
If specified map is null, returns an empty
Stream,
otherwise returns a Stream containing entries of this map. |
static <T> Stream<T> |
ofNullable(T element)
If specified element is null, returns an empty
Stream,
otherwise returns a Stream containing a single element. |
static <T> Stream<T> |
ofNullable(T[] array)
If specified array is null, returns an empty
Stream,
otherwise returns a Stream containing elements of this array. |
Stream<T> |
onClose(java.lang.Runnable closeHandler)
Adds close handler to the current stream.
|
Stream<T> |
peek(Consumer<? super T> action)
Performs provided action on each element.
|
Stream<T> |
prepend(Stream<? extends T> stream)
Prepends given
Stream to current and returns a new stream. |
static Stream<java.lang.Integer> |
range(int from,
int to)
Creates a
Stream<Integer> from not closed range
(from from inclusive to to exclusive and incremental step 1). |
static Stream<java.lang.Long> |
range(long from,
long to)
Creates a
Stream<Long> from not closed range
(from from inclusive to to exclusive and incremental step 1). |
static Stream<java.lang.Integer> |
rangeClosed(int from,
int to)
Creates a
Stream<Integer> from closed range
(from from inclusive to to inclusive and incremental step 1). |
static Stream<java.lang.Long> |
rangeClosed(long from,
long to)
Creates a
Stream<Long> from closed range
(from from inclusive to to inclusive and incremental step 1). |
Optional<T> |
reduce(BiFunction<T,T,T> accumulator)
Reduces the elements using provided associative accumulation function.
|
<R> R |
reduce(R identity,
BiFunction<? super R,? super T,? extends R> accumulator)
Reduces the elements using provided identity value and the associative accumulation function.
|
<R> R |
reduceIndexed(int from,
int step,
R identity,
IndexedBiFunction<? super R,? super T,? extends R> accumulator)
Reduces the elements using provided identity value and
the associative accumulation indexed function.
|
<R> R |
reduceIndexed(R identity,
IndexedBiFunction<? super R,? super T,? extends R> accumulator)
Reduces the elements using provided identity value and
the associative accumulation indexed function.
|
Stream<T> |
sample(int stepWidth)
Samples the
Stream by emitting every n-th element. |
Stream<T> |
scan(BiFunction<T,T,T> accumulator)
Returns a
Stream produced by iterative application of a accumulation function
to reduction value and next element of the current stream. |
<R> Stream<R> |
scan(R identity,
BiFunction<? super R,? super T,? extends R> accumulator)
Returns a
Stream produced by iterative application of a accumulation function
to an initial element identity and next element of the current stream. |
<TT> Stream<TT> |
select(java.lang.Class<TT> clazz)
Returns a stream consisting of the elements of this stream which are
instances of given class.
|
T |
single()
Returns the single element of stream.
|
Stream<T> |
skip(long n)
Skips first
n elements and returns Stream with remaining elements. |
Stream<java.util.List<T>> |
slidingWindow(int windowSize)
Partitions
Stream into Lists of fixed size by sliding over the elements of the stream. |
Stream<java.util.List<T>> |
slidingWindow(int windowSize,
int stepWidth)
Partitions
Stream into Lists of fixed size by sliding over the elements of the stream. |
<R extends java.lang.Comparable<? super R>> |
sortBy(Function<? super T,? extends R> f)
Returns
Stream with sorted elements (as determinated by Comparable interface). |
Stream<T> |
sorted()
Returns
Stream with sorted elements (as determinated by Comparable interface). |
Stream<T> |
sorted(java.util.Comparator<? super T> comparator)
Returns
Stream with sorted elements (as determinated by provided Comparator). |
Stream<T> |
takeUntil(Predicate<? super T> stopPredicate)
Takes elements while the predicate returns
false. |
Stream<T> |
takeUntilIndexed(IndexedPredicate<? super T> stopPredicate)
Takes elements while the
IndexedPredicate returns false. |
Stream<T> |
takeUntilIndexed(int from,
int step,
IndexedPredicate<? super T> stopPredicate)
Takes elements while the
IndexedPredicate returns false. |
Stream<T> |
takeWhile(Predicate<? super T> predicate)
Takes elements while the predicate returns
true. |
Stream<T> |
takeWhileIndexed(IndexedPredicate<? super T> predicate)
Takes elements while the
IndexedPredicate returns true. |
Stream<T> |
takeWhileIndexed(int from,
int step,
IndexedPredicate<? super T> predicate)
Takes elements while the
IndexedPredicate returns true. |
java.lang.Object[] |
toArray()
Collects elements to an array.
|
<R> R[] |
toArray(IntFunction<R[]> generator)
Collects elements to an array, the
generator constructor of provided. |
java.util.List<T> |
toList()
Collects elements to a new
List. |
Stream<T> |
withoutNulls()
Returns
Stream without null elements. |
static <F,S,R> Stream<R> |
zip(java.util.Iterator<? extends F> iterator1,
java.util.Iterator<? extends S> iterator2,
BiFunction<? super F,? super S,? extends R> combiner)
Combines two iterators to a stream by applying specified combiner function to each element at same position.
|
static <F,S,R> Stream<R> |
zip(Stream<? extends F> stream1,
Stream<? extends S> stream2,
BiFunction<? super F,? super S,? extends R> combiner)
Combines two streams by applying specified combiner function to each element at same position.
|
@NotNull public static <T> Stream<T> empty()
T - the type of the stream elements@NotNull public static <K,V> Stream<java.util.Map.Entry<K,V>> of(@NotNull java.util.Map<K,V> map)
Stream from Map entries.K - the type of map keysV - the type of map valuesmap - the map with elements to be passed to streamjava.lang.NullPointerException - if map is null@NotNull public static <T> Stream<T> of(@NotNull java.util.Iterator<? extends T> iterator)
Stream from any class that implements Iterator interface.T - the type of the stream elementsiterator - the iterator with elements to be passed to streamjava.lang.NullPointerException - if iterator is null@NotNull public static <T> Stream<T> of(@NotNull java.lang.Iterable<? extends T> iterable)
Stream from any class that implements Iterable interface.T - the type of the stream elementsiterable - the Iterable with elements to be passed to streamjava.lang.NullPointerException - if iterable is null@NotNull public static <T> Stream<T> of(@NotNull T... elements)
Stream from the specified values.T - the type of the stream elementselements - the elements to be passed to streamjava.lang.NullPointerException - if elements is null@NotNull public static <T> Stream<T> ofNullable(@Nullable T element)
Stream,
otherwise returns a Stream containing a single element.T - the type of the stream elementelement - the element to be passed to stream if it is non-null@NotNull public static <T> Stream<T> ofNullable(@Nullable T[] array)
Stream,
otherwise returns a Stream containing elements of this array.T - the type of the stream elementsarray - the array whose elements to be passed to stream@NotNull public static <K,V> Stream<java.util.Map.Entry<K,V>> ofNullable(@Nullable java.util.Map<K,V> map)
Stream,
otherwise returns a Stream containing entries of this map.K - the type of map keysV - the type of map valuesmap - the map with elements to be passed to stream@NotNull public static <T> Stream<T> ofNullable(@Nullable java.util.Iterator<? extends T> iterator)
Stream,
otherwise returns a Stream containing entries of this iterator.T - the type of the stream elementsiterator - the iterator with elements to be passed to stream@NotNull public static <T> Stream<T> ofNullable(@Nullable java.lang.Iterable<? extends T> iterable)
Stream,
otherwise returns a Stream containing elements of this iterable.T - the type of the stream elementsiterable - the Iterable with elements to be passed to stream@NotNull public static Stream<java.lang.Integer> range(int from, int to)
Stream<Integer> from not closed range
(from from inclusive to to exclusive and incremental step 1).from - the initial value (inclusive)to - the upper bound (exclusive)IntStream.range(int, int)@NotNull public static Stream<java.lang.Long> range(long from, long to)
Stream<Long> from not closed range
(from from inclusive to to exclusive and incremental step 1).from - the initial value (inclusive)to - the upper bound (exclusive)@NotNull public static Stream<java.lang.Integer> rangeClosed(int from, int to)
Stream<Integer> from closed range
(from from inclusive to to inclusive and incremental step 1).from - the initial value (inclusive)to - the upper bound (inclusive)IntStream.rangeClosed(int, int)@NotNull public static Stream<java.lang.Long> rangeClosed(long from, long to)
Stream<Long> from closed range
(from from inclusive to to inclusive and incremental step 1).from - the initial value (inclusive)to - the upper bound (inclusive)@NotNull public static <T> Stream<T> generate(@NotNull Supplier<T> supplier)
Stream by elements that generated by Supplier.T - the type of the stream elementssupplier - the Supplier of generated elementsjava.lang.NullPointerException - if supplier is null@NotNull public static <T> Stream<T> iterate(@Nullable T seed, @NotNull UnaryOperator<T> op)
Stream by iterative application UnaryOperator function
to an initial element seed. Produces Stream consisting of
seed, op(seed), op(op(seed)), etc.
Example:
seed: 1 op: (a) -> a + 5 result: [1, 6, 11, 16, ...]
T - the type of the stream elementsseed - the initial valueop - operator to produce new element by previous onejava.lang.NullPointerException - if op is null@NotNull public static <T> Stream<T> iterate(@Nullable T seed, @NotNull Predicate<? super T> predicate, @NotNull UnaryOperator<T> op)
Stream by iterative application UnaryOperator function
to an initial element seed, conditioned on satisfying the supplied predicate.
Example:
seed: 0 predicate: (a) -> a < 20 op: (a) -> a + 5 result: [0, 5, 10, 15]
T - the type of the stream elementsseed - the initial valuepredicate - a predicate to determine when the stream must terminateop - operator to produce new element by previous onejava.lang.NullPointerException - if op is null@NotNull public static <T> Stream<T> concat(@NotNull Stream<? extends T> stream1, @NotNull Stream<? extends T> stream2)
Example:
stream 1: [1, 2, 3, 4] stream 2: [5, 6] result: [1, 2, 3, 4, 5, 6]
T - The type of stream elementsstream1 - the first streamstream2 - the second streamjava.lang.NullPointerException - if stream1 or stream2 is null@NotNull public static <T> Stream<T> concat(@NotNull java.util.List<? extends Stream<? extends T>> streams)
List of streams.
Example:
stream 1: [1, 2, 3, 4] stream 2: [5, 6] stream 3: [7] stream 4: [8, 9, 10] result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
T - The type of stream elementsstreams - the list of streamsjava.lang.NullPointerException - if streams is null@NotNull public static <T> Stream<T> concat(@NotNull java.util.Iterator<? extends T> iterator1, @NotNull java.util.Iterator<? extends T> iterator2)
Example:
iterator 1: [1, 2, 3, 4] iterator 2: [5, 6] result: [1, 2, 3, 4, 5, 6]
T - The type of iterator elementsiterator1 - the first iteratoriterator2 - the second iteratorjava.lang.NullPointerException - if iterator1 or iterator2 is null@NotNull public static <T> Stream<T> concat(@NotNull java.util.Iterator<? extends T> iterator1, @NotNull java.util.Iterator<? extends T> iterator2, @NotNull java.util.Iterator<? extends T>... rest)
Example:
iterator 1: [1, 2, 3, 4] iterator 2: [5, 6] iterator 3: [7] iterator 4: [8, 9, 10] result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
T - The type of iterator elementsiterator1 - the first iteratoriterator2 - the second iteratorrest - the rest iteratorsjava.lang.NullPointerException - if iterator1 or iterator2
or rest is null@NotNull public static <F,S,R> Stream<R> zip(@NotNull Stream<? extends F> stream1, @NotNull Stream<? extends S> stream2, @NotNull BiFunction<? super F,? super S,? extends R> combiner)
Example:
combiner: (a, b) -> a + b stream 1: [1, 2, 3, 4] stream 2: [5, 6, 7, 8] result: [6, 8, 10, 12]
F - the type of first stream elementsS - the type of second stream elementsR - the type of elements in resulting streamstream1 - the first streamstream2 - the second streamcombiner - the combiner function used to apply to each elementjava.lang.NullPointerException - if stream1 or stream2 is null@NotNull public static <F,S,R> Stream<R> zip(@NotNull java.util.Iterator<? extends F> iterator1, @NotNull java.util.Iterator<? extends S> iterator2, @NotNull BiFunction<? super F,? super S,? extends R> combiner)
Example:
combiner: (a, b) -> a + b stream 1: [1, 2, 3, 4] stream 2: [5, 6, 7, 8] result: [6, 8, 10, 12]
F - the type of first iterator elementsS - the type of second iterator elementsR - the type of elements in resulting streamiterator1 - the first iteratoriterator2 - the second iteratorcombiner - the combiner function used to apply to each elementjava.lang.NullPointerException - if iterator1 or iterator2 is null@NotNull public static <T> Stream<T> merge(@NotNull Stream<? extends T> stream1, @NotNull Stream<? extends T> stream2, @NotNull BiFunction<? super T,? super T,ObjMerge.MergeResult> selector)
Example 1 — Merge two sorted streams:
stream1: [1, 3, 8, 10] stream2: [2, 5, 6, 12] selector: (a, b) -> a < b ? TAKE_FIRST : TAKE_SECOND result: [1, 2, 3, 5, 6, 8, 10, 12]
Example 2 — Concat two streams:
stream1: [0, 3, 1] stream2: [2, 5, 6, 1] selector: (a, b) -> TAKE_SECOND result: [2, 5, 6, 1, 0, 3, 1]
T - the type of the elementsstream1 - the first streamstream2 - the second streamselector - the selector function used to choose elementsjava.lang.NullPointerException - if stream1 or stream2 is nullpublic static <T> Stream<T> merge(@NotNull java.util.Iterator<? extends T> iterator1, @NotNull java.util.Iterator<? extends T> iterator2, @NotNull BiFunction<? super T,? super T,ObjMerge.MergeResult> selector)
Example 1 — Merge two sorted iterators:
iterator1: [1, 3, 8, 10] iterator2: [2, 5, 6, 12] selector: (a, b) -> a < b ? TAKE_FIRST : TAKE_SECOND result: [1, 2, 3, 5, 6, 8, 10, 12]
Example 2 — Concat two iterators:
iterator1: [0, 3, 1] iterator2: [2, 5, 6, 1] selector: (a, b) -> TAKE_SECOND result: [2, 5, 6, 1, 0, 3, 1]
T - the type of the elementsiterator1 - the first iteratoriterator2 - the second iteratorselector - the selector function used to choose elementsjava.lang.NullPointerException - if iterator1 or iterator2 is nullpublic java.util.Iterator<? extends T> iterator()
@Nullable
public <R> R custom(@NotNull
Function<Stream<T>,R> function)
Stream for intermediate operations,
or any value for terminal operation.
Operator examples:
// Intermediate operator
public class Reverse<T> implements Function<Stream<T>, Stream<T>> {
@Override
public Stream<T> apply(Stream<T> stream) {
final Iterator<? extends T> iterator = stream.iterator();
final ArrayDeque<T> deque = new ArrayDeque<T>();
while (iterator.hasNext()) {
deque.addFirst(iterator.next());
}
return Stream.of(deque.iterator());
}
}
// Intermediate operator based on existing stream operators
public class SkipAndLimit<T> implements UnaryOperator<Stream<T>> {
private final int skip, limit;
public SkipAndLimit(int skip, int limit) {
this.skip = skip;
this.limit = limit;
}
@Override
public Stream<T> apply(Stream<T> stream) {
return stream.skip(skip).limit(limit);
}
}
// Terminal operator
public class Sum implements Function<Stream<Integer>, Integer> {
@Override
public Integer apply(Stream<Integer> stream) {
return stream.reduce(0, new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer value1, Integer value2) {
return value1 + value2;
}
});
}
}
R - the type of the resultfunction - a transforming functionjava.lang.NullPointerException - if function is null@NotNull public Stream<T> prepend(@NotNull Stream<? extends T> stream)
Stream to current and returns a new stream.
This is similar to Stream.concat(stream, this)
Example:
current: [1, 2, 3] stream: [4, 5, 6] result: [4, 5, 6, 1, 2, 3]
stream - the stream to prependconcat(Stream, Stream)@NotNull public Stream<T> append(@NotNull Stream<? extends T> stream)
Stream to current and returns a new stream.
This is similar to Stream.concat(this, stream)
Example:
current: [1, 2, 3] stream: [4, 5, 6] result: [1, 2, 3, 4, 5, 6]
stream - the stream to appendconcat(Stream, Stream)@NotNull public Stream<T> filter(@NotNull Predicate<? super T> predicate)
Stream with elements that satisfy the given predicate.
This is an intermediate operation.
Example:
predicate: (a) -> a > 2 stream: [1, 2, 3, 4, -8, 0, 11] result: [3, 4, 11]
predicate - the predicate used to filter elements@NotNull public Stream<T> filterIndexed(@NotNull IndexedPredicate<? super T> predicate)
Stream with elements that satisfy the given IndexedPredicate.
This is an intermediate operation.
Example:
predicate: (index, value) -> (index + value) > 6 stream: [1, 2, 3, 4, 0, 11] index: [0, 1, 2, 3, 4, 5] sum: [1, 3, 5, 7, 4, 16] filter: [ 7, 16] result: [4, 11]
predicate - the IndexedPredicate used to filter elements@NotNull public Stream<T> filterIndexed(int from, int step, @NotNull IndexedPredicate<? super T> predicate)
Stream with elements that satisfy the given IndexedPredicate.
This is an intermediate operation.
Example:
from: 4 step: 3 predicate: (index, value) -> (index + value) > 15 stream: [1, 2, 3, 4, 0, 11] index: [4, 7, 10, 13, 16, 19] sum: [5, 9, 13, 17, 16, 30] filter: [ 17, 16, 30] result: [4, 0, 11]
from - the initial value of the index (inclusive)step - the step of the indexpredicate - the IndexedPredicate used to filter elements@NotNull public Stream<T> filterNot(@NotNull Predicate<? super T> predicate)
Stream with elements that does not satisfy the given predicate.
This is an intermediate operation.
predicate - the predicate used to filter elements@NotNull public <TT> Stream<TT> select(@NotNull java.lang.Class<TT> clazz)
This is an intermediate operation.
TT - a type of instances to select.clazz - a class which instances should be selected@NotNull public Stream<T> withoutNulls()
Stream without null elements.
This is an intermediate operation.
@NotNull public Stream<T> nullsOnly()
Stream with elements that is null only.
This is an intermediate operation.
@NotNull public Stream<T> equalsOnly(@Nullable T object)
Stream with elements that is equality of object only.
This is an intermediate operation.
object - object@NotNull public <R> Stream<R> map(@NotNull Function<? super T,? extends R> mapper)
Stream with elements that obtained by applying the given function.
This is an intermediate operation.
Example:
mapper: (a) -> a + 5 stream: [1, 2, 3, 4] result: [6, 7, 8, 9]
R - the type of elements in resulting streammapper - the mapper function used to apply to each element@NotNull public <R> Stream<R> mapIndexed(@NotNull IndexedFunction<? super T,? extends R> mapper)
Stream with elements that obtained by applying the given IndexedFunction.
This is an intermediate operation.
Example:
predicate: (index, value) -> (index * value) stream: [1, 2, 3, 4] index: [0, 1, 2, 3] result: [0, 2, 6, 12]
R - the type of elements in resulting streammapper - the mapper function used to apply to each element@NotNull public <R> Stream<R> mapIndexed(int from, int step, @NotNull IndexedFunction<? super T,? extends R> mapper)
Stream with elements that obtained by applying the given IndexedFunction.
This is an intermediate operation.
Example:
from: -2 step: 2 predicate: (index, value) -> (index * value) stream: [ 1, 2, 3, 4] index: [-2, 0, 2, 4] result: [-2, 0, 6, 16]
R - the type of elements in resulting streamfrom - the initial value of the index (inclusive)step - the step of the indexmapper - the mapper function used to apply to each element@NotNull public IntStream mapToInt(@NotNull ToIntFunction<? super T> mapper)
IntStream with elements that obtained by applying the given function.
This is an intermediate operation.
mapper - the mapper function used to apply to each elementIntStreammap(com.annimon.stream.function.Function)@NotNull public LongStream mapToLong(@NotNull ToLongFunction<? super T> mapper)
LongStream with elements that obtained by applying the given function.
This is an intermediate operation.
mapper - the mapper function used to apply to each elementLongStreammap(com.annimon.stream.function.Function)@NotNull public DoubleStream mapToDouble(@NotNull ToDoubleFunction<? super T> mapper)
DoubleStream with elements that obtained by applying the given function.
This is an intermediate operation.
mapper - the mapper function used to apply to each elementDoubleStreammap(com.annimon.stream.function.Function)@NotNull public <R> Stream<R> flatMap(@NotNull Function<? super T,? extends Stream<? extends R>> mapper)
This is an intermediate operation.
Example:
mapper: (a) -> [a, a + 5] stream: [1, 2, 3, 4] result: [1, 6, 2, 7, 3, 8, 4, 9]
R - the type of elements in resulting streammapper - the mapper function used to apply to each element@NotNull public IntStream flatMapToInt(@NotNull Function<? super T,? extends IntStream> mapper)
This is an intermediate operation.
mapper - the mapper function used to apply to each elementIntStreamflatMap(com.annimon.stream.function.Function)@NotNull public LongStream flatMapToLong(@NotNull Function<? super T,? extends LongStream> mapper)
This is an intermediate operation.
mapper - the mapper function used to apply to each elementLongStreamflatMap(com.annimon.stream.function.Function)@NotNull public DoubleStream flatMapToDouble(@NotNull Function<? super T,? extends DoubleStream> mapper)
This is an intermediate operation.
mapper - the mapper function used to apply to each elementDoubleStreamflatMap(com.annimon.stream.function.Function)@NotNull public <R> Stream<R> mapMulti(@NotNull BiConsumer<? super T,? super Consumer<R>> mapper)
This is an intermediate operation.
Example:
stream: [1, 2, 3, 4]
mapper: (a, consumer) -> {
consumer.accept(a);
consumer.accept(-a);
}
result: [1, -1, 2, -2, 3, -3, 4, -4]
stream: [1, 2, 3, 4]
mapper: (a, consumer) -> {
if (a % 2 == 0)
consumer.accept(a * 2);
}
result: [4, 8]
R - the type of elements in resulting streammapper - the mapper function used to apply to each element for producing replacing elementsflatMap(com.annimon.stream.function.Function)@NotNull public IntStream mapMultiToInt(@NotNull BiConsumer<? super T,? super IntConsumer> mapper)
This is an intermediate operation.
mapper - the mapper function used to apply to each element for producing replacing elementsmapMulti(com.annimon.stream.function.BiConsumer),
flatMapToInt(com.annimon.stream.function.Function)@NotNull public LongStream mapMultiToLong(@NotNull BiConsumer<? super T,? super LongConsumer> mapper)
This is an intermediate operation.
mapper - the mapper function used to apply to each element for producing replacing elementsmapMulti(com.annimon.stream.function.BiConsumer),
flatMapToLong(com.annimon.stream.function.Function)@NotNull public DoubleStream mapMultiToDouble(@NotNull BiConsumer<? super T,? super DoubleConsumer> mapper)
This is an intermediate operation.
mapper - the mapper function used to apply to each element for producing replacing elementsmapMulti(com.annimon.stream.function.BiConsumer),
flatMapToDouble(com.annimon.stream.function.Function)@NotNull public Stream<IntPair<T>> indexed()
Stream with indexed elements.
Indexing starts from 0 with step 1.
This is an intermediate operation.
Example:
stream: ["a", "b", "c"] result: [(0, "a"), (1, "b"), (2, "c")]
IntPair stream@NotNull public Stream<IntPair<T>> indexed(int from, int step)
Stream with indexed elements.
This is an intermediate operation.
Example:
from: 5, step: 10 stream: ["a", "b", "c"] result: [(5, "a"), (15, "b"), (25, "c")]
from - the initial value (inclusive)step - the stepIntPair stream@NotNull public Stream<T> distinct()
Stream with distinct elements (as determined by hashCode and equals methods).
This is a stateful intermediate operation.
Example:
stream: [1, 4, 2, 3, 3, 4, 1] result: [1, 4, 2, 3]
@NotNull public <K> Stream<T> distinctBy(@NotNull Function<? super T,? extends K> classifier)
Stream with distinct elements (as determined by hashCode
and equals methods) according to the given classifier function.
This is a stateful intermediate operation.
Example:
classifier: (str) -> str.length() stream: ["a", "bc", "d", "ef", "ghij"] result: ["a", "bc", "ghij"]
K - the type of the result of classifier functionclassifier - the classifier function@NotNull public Stream<T> sorted()
Stream with sorted elements (as determinated by Comparable interface).
This is a stateful intermediate operation.
If the elements of this stream are not Comparable,
a java.lang.ClassCastException may be thrown when the terminal operation is executed.
Example:
stream: [3, 4, 1, 2] result: [1, 2, 3, 4]
@NotNull public Stream<T> sorted(@Nullable java.util.Comparator<? super T> comparator)
Stream with sorted elements (as determinated by provided Comparator).
This is a stateful intermediate operation.
Example:
comparator: (a, b) -> -a.compareTo(b) stream: [1, 2, 3, 4] result: [4, 3, 2, 1]
comparator - the Comparator to compare elements@NotNull public <R extends java.lang.Comparable<? super R>> Stream<T> sortBy(@NotNull Function<? super T,? extends R> f)
Stream with sorted elements (as determinated by Comparable interface).
Each element transformed by given function f before comparing.
This is a stateful intermediate operation.
Example:
f: (a) -> -a stream: [1, 2, 3, 4] result: [4, 3, 2, 1]
R - the type of the result of transforming functionf - the transformation function@NotNull public <K> Stream<java.util.Map.Entry<K,java.util.List<T>>> groupBy(@NotNull Function<? super T,? extends K> classifier)
Stream into Map entries according to the given classifier function.
This is a stateful intermediate operation.
Example:
classifier: (str) -> str.length()
stream: ["a", "bc", "d", "ef", "ghij"]
result: [{1: ["a", "d"]}, {2: ["bc", "ef"]}, {4: ["ghij"]}]
K - the type of the keys, which are result of the classifier functionclassifier - the classifier function@NotNull public <K> Stream<java.util.List<T>> chunkBy(@NotNull Function<? super T,? extends K> classifier)
Stream into Lists according to the given classifier function. In contrast
to groupBy(Function), this method assumes that the elements of the stream are sorted.
Because of this assumption, it does not need to first collect all elements and then partition them.
Instead, it can emit a List of elements when it reaches the first element that does not
belong to the same chunk as the previous elements.
This is an intermediate operation.
Example:
classifier: (a) -> a % 5 == 0 stream: [1, 2, 5, 6, 7, 9, 10, 12, 14] result: [[1, 2], [5], [6, 7, 9], [10], [12, 14]]
K - the type of the keys, which are the result of the classifier functionclassifier - the classifier function@NotNull public Stream<T> sample(int stepWidth)
Stream by emitting every n-th element.
This is an intermediate operation.
Example:
stepWidth: 3 stream: [1, 2, 3, 4, 5, 6, 7, 8] result: [1, 4, 7]
stepWidth - step widthjava.lang.IllegalArgumentException - if stepWidth is zero or negative@NotNull public Stream<java.util.List<T>> slidingWindow(int windowSize)
Stream into Lists of fixed size by sliding over the elements of the stream.
It starts with the first element and in each iteration moves by 1. This method yields the same results
as calling slidingWindow(int, int) with a stepWidth of 1.
This is an intermediate operation.
Example:
windowSize: 3 stream: [1, 2, 3, 4, 5] result: [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
windowSize - number of elements that will be emitted together in a listslidingWindow(int, int)@NotNull public Stream<java.util.List<T>> slidingWindow(int windowSize, int stepWidth)
Stream into Lists of fixed size by sliding over the elements of the stream.
It starts with the first element and in each iteration moves by the given step width. This method
allows, for example, to partition the elements into batches of windowSize elements (by using a
step width equal to the specified window size) or to sample every n-th element (by using a window size
of 1 and a step width of n).
This is an intermediate operation.
Example:
windowSize: 3, stepWidth: 3 stream: [1, 1, 1, 2, 2, 2, 3, 3, 3] result: [[1, 1, 1], [2, 2, 2] [3, 3, 3]] windowSize: 2, stepWidth: 3 stream: [1, 2, 3, 1, 2, 3, 1, 2, 3] result: [[1, 2], [1, 2], [1, 2]] windowSize: 3, stepWidth: 1 stream: [1, 2, 3, 4, 5, 6] result: [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]
windowSize - number of elements that will be emitted together in a liststepWidth - step widthjava.lang.IllegalArgumentException - if windowSize is zero or negativejava.lang.IllegalArgumentException - if stepWidth is zero or negative@NotNull public Stream<T> peek(@NotNull Consumer<? super T> action)
This is an intermediate operation.
action - the action to be performed on each element@NotNull public Stream<T> scan(@NotNull BiFunction<T,T,T> accumulator)
Stream produced by iterative application of a accumulation function
to reduction value and next element of the current stream.
Produces a Stream consisting of value1, acc(value1, value2),
acc(acc(value1, value2), value3), etc.
This is an intermediate operation.
Example:
accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: [1, 3, 6, 10, 15]
accumulator - the accumulation functionjava.lang.NullPointerException - if accumulator is null@NotNull public <R> Stream<R> scan(@Nullable R identity, @NotNull BiFunction<? super R,? super T,? extends R> accumulator)
Stream produced by iterative application of a accumulation function
to an initial element identity and next element of the current stream.
Produces a Stream consisting of identity, acc(identity, value1),
acc(acc(identity, value1), value2), etc.
This is an intermediate operation.
Example:
identity: 0 accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: [0, 1, 3, 6, 10, 15]
R - the type of the resultidentity - the initial valueaccumulator - the accumulation functionjava.lang.NullPointerException - if accumulator is null@NotNull public Stream<T> takeWhile(@NotNull Predicate<? super T> predicate)
true.
This is an intermediate operation.
Example:
predicate: (a) -> a < 3 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [1, 2]
predicate - the predicate used to take elements@NotNull public Stream<T> takeWhileIndexed(@NotNull IndexedPredicate<? super T> predicate)
IndexedPredicate returns true.
This is an intermediate operation.
Example:
predicate: (index, value) -> (index + value) < 5 stream: [1, 2, 3, 4, -5, -6, -7] index: [0, 1, 2, 3, 4, 5, 6] sum: [1, 3, 5, 7, -1, -1, -1] result: [1, 2]
predicate - the IndexedPredicate used to take elements@NotNull public Stream<T> takeWhileIndexed(int from, int step, @NotNull IndexedPredicate<? super T> predicate)
IndexedPredicate returns true.
This is an intermediate operation.
Example:
from: 2 step: 2 predicate: (index, value) -> (index + value) < 8 stream: [1, 2, 3, 4, -5, -6, -7] index: [2, 4, 6, 8, 10, 12, 14] sum: [3, 6, 9, 12, 5, 6, 7] result: [1, 2]
from - the initial value of the index (inclusive)step - the step of the indexpredicate - the IndexedPredicate used to take elements@NotNull public Stream<T> takeUntil(@NotNull Predicate<? super T> stopPredicate)
false.
Once predicate condition is satisfied by an element, the stream
finishes with this element.
This is an intermediate operation.
Example:
stopPredicate: (a) -> a > 2 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [1, 2, 3]
stopPredicate - the predicate used to take elements@NotNull public Stream<T> takeUntilIndexed(@NotNull IndexedPredicate<? super T> stopPredicate)
IndexedPredicate returns false.
Once predicate condition is satisfied by an element, the stream
finishes with this element.
This is an intermediate operation.
Example:
stopPredicate: (index, value) -> (index + value) > 4 stream: [1, 2, 3, 4, 0, 1, 2] index: [0, 1, 2, 3, 4, 5, 6] sum: [1, 3, 5, 7, 4, 6, 8] result: [1, 2, 3]
stopPredicate - the IndexedPredicate used to take elements@NotNull public Stream<T> takeUntilIndexed(int from, int step, @NotNull IndexedPredicate<? super T> stopPredicate)
IndexedPredicate returns false.
Once predicate condition is satisfied by an element, the stream
finishes with this element.
This is an intermediate operation.
Example:
from: 2 step: 2 stopPredicate: (index, value) -> (index + value) > 8 stream: [1, 2, 3, 4, 0, 1, 2] index: [2, 4, 6, 8, 10, 11, 14] sum: [3, 6, 9, 12, 10, 12, 16] result: [1, 2, 3]
from - the initial value of the index (inclusive)step - the step of the indexstopPredicate - the IndexedPredicate used to take elements@NotNull public Stream<T> dropWhile(@NotNull Predicate<? super T> predicate)
This is an intermediate operation.
Example:
predicate: (a) -> a < 3 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [3, 4, 1, 2, 3, 4]
predicate - the predicate used to drop elements@NotNull public Stream<T> dropWhileIndexed(@NotNull IndexedPredicate<? super T> predicate)
IndexedPredicate is true, then returns the rest.
This is an intermediate operation.
Example:
predicate: (index, value) -> (index + value) < 5 stream: [1, 2, 3, 4, 0, 1, 2] index: [0, 1, 2, 3, 4, 5, 6] sum: [1, 3, 5, 7, 4, 6, 8] result: [3, 4, 0, 1, 2]
predicate - the IndexedPredicate used to drop elements@NotNull public Stream<T> dropWhileIndexed(int from, int step, @NotNull IndexedPredicate<? super T> predicate)
IndexedPredicate is true, then returns the rest.
This is an intermediate operation.
Example:
from: 2 step: 2 predicate: (index, value) -> (index + value) < 10 stream: [1, 2, 3, 4, -5, -6, -7] index: [2, 4, 6, 8, 10, 12, 14] sum: [3, 6, 9, 12, 5, 6, 7] result: [4, -5, -6, -7]
from - the initial value of the index (inclusive)step - the step of the indexpredicate - the IndexedPredicate used to drop elements@NotNull public Stream<T> limit(long maxSize)
Stream with first maxSize elements.
This is a short-circuiting stateful intermediate operation.
Example:
maxSize: 3 stream: [1, 2, 3, 4, 5] result: [1, 2, 3] maxSize: 10 stream: [1, 2] result: [1, 2]
maxSize - the number of elements to limitjava.lang.IllegalArgumentException - if maxSize is negative@NotNull public Stream<T> skip(long n)
n elements and returns Stream with remaining elements.
If stream contains fewer than n elements, then an empty stream will be returned.
This is a stateful intermediate operation.
Example:
n: 3 stream: [1, 2, 3, 4, 5] result: [4, 5] n: 10 stream: [1, 2] result: []
n - the number of elements to skipjava.lang.IllegalArgumentException - if n is negativepublic void forEach(@NotNull
Consumer<? super T> action)
This is a terminal operation.
action - the action to be performed on each elementpublic void forEachIndexed(@NotNull
IndexedConsumer<? super T> action)
This is a terminal operation.
action - the action to be performed on each elementpublic void forEachIndexed(int from,
int step,
@NotNull
IndexedConsumer<? super T> action)
This is a terminal operation.
from - the initial value of the index (inclusive)step - the step of the indexaction - the action to be performed on each element@Nullable
public <R> R reduce(@Nullable
R identity,
@NotNull
BiFunction<? super R,? super T,? extends R> accumulator)
This is a terminal operation.
Example:
identity: 0 accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: 15
R - the type of the resultidentity - the initial valueaccumulator - the accumulation function@Nullable
public <R> R reduceIndexed(@Nullable
R identity,
@NotNull
IndexedBiFunction<? super R,? super T,? extends R> accumulator)
This is a terminal operation.
Example:
identity: 10 accumulator: (index, a, b) -> index + a + b stream: [1, 2, 3, 4, 5] index: [0, 1, 2, 3, 4] result: 10 + 1 + 3 + 5 + 7 + 9 = 35
R - the type of the resultidentity - the initial valueaccumulator - the accumulation function@Nullable
public <R> R reduceIndexed(int from,
int step,
@Nullable
R identity,
@NotNull
IndexedBiFunction<? super R,? super T,? extends R> accumulator)
This is a terminal operation.
Example:
from: 1 step: 2 identity: 10 accumulator: (index, a, b) -> index + a + b stream: [1, 2, 3, 4, 5] index: [1, 3, 5, 7, 9] result: 10 + 2 + 5 + 8 + 11 + 14 = 50
R - the type of the resultfrom - the initial value of the index (inclusive)step - the step of the indexidentity - the initial valueaccumulator - the accumulation function@NotNull public Optional<T> reduce(@NotNull BiFunction<T,T,T> accumulator)
This is a terminal operation.
accumulator - the accumulation functionreduce(java.lang.Object, com.annimon.stream.function.BiFunction)@NotNull public java.lang.Object[] toArray()
This is a terminal operation.
toArray(com.annimon.stream.function.IntFunction)@NotNull
public <R> R[] toArray(@NotNull
IntFunction<R[]> generator)
generator constructor of provided.
This is a terminal operation.
R - the type of the resultgenerator - the array constructor reference that accommodates future array of assigned size@NotNull public java.util.List<T> toList()
List.
This implementation does not call collect(Collectors.toList()), so
it can be faster by reducing method calls.
This is a terminal operation.
ListCollectors.toList()@Nullable
public <R> R collect(@NotNull
Supplier<R> supplier,
@NotNull
BiConsumer<R,? super T> accumulator)
supplier provided container by applying the given accumulation function.
This is a terminal operation.
R - the type of the resultsupplier - the supplier function that provides containeraccumulator - the accumulation functioncollect(com.annimon.stream.Collector)@Nullable
public <R,A> R collect(@NotNull
Collector<? super T,A,R> collector)
collector that encapsulates supplier, accumulator and combiner functions.
This is a terminal operation.
R - the type of resultA - the intermediate used by Collectorcollector - the Collectorcollect(com.annimon.stream.function.Supplier, com.annimon.stream.function.BiConsumer)@NotNull public Optional<T> min(@NotNull java.util.Comparator<? super T> comparator)
This is a terminal operation.
Example:
comparator: (a, b) -> a.compareTo(b) stream: [1, 2, 3, 4, 5] result: 1
comparator - the Comparator to compare elements@NotNull public Optional<T> max(@NotNull java.util.Comparator<? super T> comparator)
This is a terminal operation.
Example:
comparator: (a, b) -> a.compareTo(b) stream: [1, 2, 3, 4, 5] result: 5
comparator - the Comparator to compare elementspublic long count()
This is a terminal operation.
public boolean anyMatch(@NotNull
Predicate<? super T> predicate)
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: true predicate: (a) -> a == 5 stream: [5, 5, 5] result: true
predicate - the predicate used to match elementstrue if any elements match the given predicate, otherwise falsepublic boolean allMatch(@NotNull
Predicate<? super T> predicate)
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: false predicate: (a) -> a == 5 stream: [5, 5, 5] result: true
predicate - the predicate used to match elementstrue if all elements match the given predicate, otherwise falsepublic boolean noneMatch(@NotNull
Predicate<? super T> predicate)
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: false predicate: (a) -> a == 5 stream: [1, 2, 3] result: true
predicate - the predicate used to match elementstrue if no elements match the given predicate, otherwise false@NotNull public Optional<IntPair<T>> findIndexed(@NotNull IndexedPredicate<? super T> predicate)
This is a short-circuiting terminal operation.
Example:
predicate: (index, value) -> index + value == 7 stream: [1, 2, 3, 4, 5, 2, 0] index: [0, 1, 2, 3, 4, 5, 6] result: Optional.of(IntPair(3, 4))
predicate - the predicate to find valueOptional with IntPair
or Optional.empty() if stream is empty or no value was found.@NotNull public Optional<IntPair<T>> findIndexed(int from, int step, @NotNull IndexedPredicate<? super T> predicate)
This is a short-circuiting terminal operation.
Example:
from: 0 step: 10 predicate: (index, value) -> index + value == 42 stream: [1, 11, 22, 12, 40] index: [0, 10, 20, 30, 40] result: Optional.of(IntPair(20, 22))
from - the initial value of the index (inclusive)step - the step of the indexpredicate - the predicate to find valueOptional with IntPair
or Optional.empty() if stream is empty or no value was found.@NotNull public Optional<T> findFirst()
Optional class.
If stream is empty, returns Optional.empty().
This is a short-circuiting terminal operation.
Optional with the first element
or Optional.empty() if stream is empty@Nullable public T findFirstOrElse(@Nullable T other)
other.
This is a short-circuiting terminal operation.
other - the value to be returned if stream is emptyother if stream is empty@NotNull public Optional<T> findLast()
Optional class.
If stream is empty, returns Optional.empty().
This is a short-circuiting terminal operation.
Optional with the last element
or Optional.empty() if the stream is empty@Nullable public T single()
NoSuchElementException.
If stream contains more than one element, throws IllegalStateException.
This is a short-circuiting terminal operation.
Example:
stream: [] result: NoSuchElementException stream: [1] result: 1 stream: [1, 2, 3] result: IllegalStateException
java.util.NoSuchElementException - if stream is emptyjava.lang.IllegalStateException - if stream contains more than one element@NotNull public Optional<T> findSingle()
Optional class.
If stream is empty, returns Optional.empty().
If stream contains more than one element, throws IllegalStateException.
This is a short-circuiting terminal operation.
Example:
stream: [] result: Optional.empty() stream: [1] result: Optional.of(1) stream: [1, 2, 3] result: IllegalStateException
Optional with single element or Optional.empty() if stream is emptyjava.lang.IllegalStateException - if stream contains more than one element@NotNull public Stream<T> onClose(@NotNull java.lang.Runnable closeHandler)
This is an intermediate operation.
closeHandler - an action to execute when the stream is closedpublic void close()
close in interface java.io.Closeableclose in interface java.lang.AutoCloseable