T - the type of the stream elementspublic class Stream<T> extends Object
| 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.
|
<K> Stream<List<T>> |
chunkBy(Function<? super T,? extends K> classifier)
Partitions
Stream into Lists according to the given classifier function. |
<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(Stream<? extends T> stream1,
Stream<? extends T> stream2)
Concatenates two streams.
|
long |
count()
Counts the number 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 determinated by equals method). |
Stream<T> |
dropWhile(Predicate<? super T> predicate)
Drops elements while the predicate is true and returns the rest.
|
static <T> Stream<T> |
empty()
Returns an empty stream.
|
Stream<T> |
filter(Predicate<? super T> predicate)
Returns
Stream with elements that satisfy the given predicate. |
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. |
<R> Stream<R> |
flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Generates
Stream by concatenating elements that obtained by applying the given function. |
void |
forEach(Consumer<? super T> action)
Performs the given action to each element.
|
static <T> Stream<T> |
generate(Supplier<T> supplier)
Creates a
Stream by elements that generated by Supplier. |
Iterator<? extends T> |
getIterator()
Returns internal stream iterator.
|
<K> Stream<Map.Entry<K,List<T>>> |
groupBy(Function<? super T,? extends K> classifier)
Partitions
Stream into Map entries according to the given classifier function. |
static <T> Stream<T> |
iterate(T seed,
UnaryOperator<T> op)
Creates a
Stream by applying UnaryOperator operation to an initial element seed. |
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. |
Optional<T> |
max(Comparator<? super T> comparator)
Finds the maximum element according to the given comparator.
|
Optional<T> |
min(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.
|
static <T> Stream<T> |
of(Iterable<? extends T> iterable)
Creates a
Stream from any class that implements Iterable interface. |
static <T> Stream<T> |
of(Iterator<? extends T> iterator)
Creates a
Stream from any class that implements Iterator interface. |
static <T> Stream<T> |
of(List<? extends T> list)
Creates a
Stream from List. |
static <K,V> Stream<Map.Entry<K,V>> |
of(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 Stream<Integer> |
ofRange(int from,
int to)
Deprecated.
As of release 1.0.7, replaced by
range(int, int) |
static Stream<Long> |
ofRange(long from,
long to)
Deprecated.
As of release 1.0.7, replaced by
range(long, long) |
static Stream<Integer> |
ofRangeClosed(int from,
int to)
Deprecated.
As of release 1.0.7, replaced by
rangeClosed(int, int) |
static Stream<Long> |
ofRangeClosed(long from,
long to)
Deprecated.
As of release 1.0.7, replaced by
rangeClosed(long, long) |
Stream<T> |
peek(Consumer<? super T> action)
Perform provided action to each elements.
|
static Stream<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<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<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<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.
|
Stream<T> |
sample(int stepWidth)
Samples the
Stream by emitting every n-th element. |
<TT> Stream<TT> |
select(Class<TT> clazz)
Returns a stream consisting of the elements of this stream which are
instances of given class.
|
Stream<T> |
skip(long n)
Skips first
n elements and returns Stream with remaining elements. |
Stream<List<T>> |
slidingWindow(int windowSize)
Partitions
Stream into Lists of fixed size by sliding over the elements of the stream. |
Stream<List<T>> |
slidingWindow(int windowSize,
int stepWidth)
Partitions
Stream into Lists of fixed size by sliding over the elements of the stream. |
<R extends 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(Comparator<? super T> comparator)
Returns
Stream with sorted elements (as determinated by provided Comparator). |
Stream<T> |
takeWhile(Predicate<? super T> predicate)
Takes elements while the predicate is true.
|
Object[] |
toArray()
Collects elements to an array.
|
<R> R[] |
toArray(IntFunction<R[]> generator)
Collects elements to an array, the
generator constructor of provided. |
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.
|
public static <T> Stream<T> empty()
T - the type of the stream elementspublic static <K,V> Stream<Map.Entry<K,V>> of(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 streampublic static <T> Stream<T> of(List<? extends T> list)
Stream from List.T - the type of the stream elementslist - the list with elements to be passed to streampublic static <T> Stream<T> of(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 streampublic static <T> Stream<T> of(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 streampublic static <T> Stream<T> of(T... elements)
Stream from the specified values.T - the type of the stream elementselements - the elements to be passed to streampublic static Stream<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)@Deprecated public static Stream<Integer> ofRange(int from, int to)
range(int, int)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)public static Stream<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)@Deprecated public static Stream<Long> ofRange(long from, long to)
range(long, long)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)public static Stream<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)@Deprecated public static Stream<Integer> ofRangeClosed(int from, int to)
rangeClosed(int, int)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)public static Stream<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)@Deprecated public static Stream<Long> ofRangeClosed(long from, long to)
rangeClosed(long, long)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)public static <T> Stream<T> generate(Supplier<T> supplier)
Stream by elements that generated by Supplier.T - the type of the stream elementssupplier - the Supplier of generated elementspublic static <T> Stream<T> iterate(T seed, UnaryOperator<T> op)
Stream by applying UnaryOperator operation to an initial element seed.T - the type of the stream elementsseed - the initial valueop - operator to produce new element by previous onepublic static <T> Stream<T> concat(Stream<? extends T> stream1, Stream<? extends T> stream2)
T - The type of stream elementsstream1 - the first streamstream2 - the second streampublic static <F,S,R> Stream<R> zip(Stream<? extends F> stream1, Stream<? extends S> stream2, BiFunction<? super F,? super S,? extends R> combiner)
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 elementpublic Iterator<? extends T> getIterator()
public <R> R custom(Function<Stream<T>,R> function)
Stream for intermediate operations,
or any value for terminal operation.
Operator examples:
// Intermediate operator
public static class Reverse<T> implements Function<Stream<T>, Stream<T>> {
@Override
public Stream<T> apply(Stream<T> stream) {
final Iterator<? extends T> iterator = stream.getIterator();
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 static 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 static 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 resultfunction - a transforming functionpublic Stream<T> filter(Predicate<? super T> predicate)
Stream with elements that satisfy the given predicate.
This is an intermediate operation.
predicate - the predicate used to filter elementspublic Stream<T> filterNot(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 elementspublic <TT> Stream<TT> select(Class<TT> clazz)
This is an intermediate operation.
TT - a type of instances to select.clazz - a class which instances should be selectedpublic <R> Stream<R> map(Function<? super T,? extends R> mapper)
Stream with elements that obtained by applying the given function.
This is an intermediate operation.
R - the type of elements in resulting streammapper - the mapper function used to apply to each elementpublic <R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Stream by concatenating elements that obtained by applying the given function.
This is an intermediate operation.
R - the type of elements in resulting streammapper - the mapper function used to apply to each elementpublic Stream<T> distinct()
Stream with distinct elements (as determinated by equals method).
This is a stateful intermediate operation.
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.
public Stream<T> sorted(Comparator<? super T> comparator)
Stream with sorted elements (as determinated by provided Comparator).
This is a stateful intermediate operation.
comparator - the Comparator to compare elementspublic <R extends Comparable<? super R>> Stream<T> sortBy(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.
R - the type of the result of transforming functionf - the transformation functionpublic <K> Stream<Map.Entry<K,List<T>>> groupBy(Function<? super T,? extends K> classifier)
Stream into Map entries according to the given classifier function.
This is a stateful intermediate operation.
K - the type of the keys, which are result of the classifier functionclassifier - the classifier functionpublic <K> Stream<List<T>> chunkBy(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.
K - the type of the keys, which are the result of the classifier functionclassifier - the classifier functionpublic Stream<T> sample(int stepWidth)
Stream by emitting every n-th element.
This is an intermediate operation.
stepWidth - step widthpublic Stream<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.
windowSize - number of elements that will be emitted together in a listslidingWindow(int, int)public Stream<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.
Examples:
elements: [1, 1, 1, 2, 2, 2, 3, 3, 3] windowSize: 3 stepWidth: 3 => [1, 1, 1], [2, 2, 2] [3, 3, 3] elements: [1, 2, 3, 1, 2, 3, 1, 2, 3] windowSize: 2 stepWidth: 3 => [1, 2], [1, 2], [1, 2] elements: [1, 2, 3, 4, 5, 6] windowSize: 3 stepWidth: 1 => [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 widthpublic Stream<T> peek(Consumer<? super T> action)
This is an intermediate operation.
action - the action to be performed on each elementpublic Stream<T> takeWhile(Predicate<? super T> predicate)
This is an intermediate operation.
predicate - the predicate used to take elementspublic Stream<T> dropWhile(Predicate<? super T> predicate)
This is an intermediate operation.
predicate - the predicate used to drop elementspublic Stream<T> limit(long maxSize)
Stream with first maxSize elements.
This is a short-circuiting stateful intermediate operation.
maxSize - the number of elements to limitpublic Stream<T> skip(long n)
n elements and returns Stream with remaining elements.
This is a stateful intermediate operation.
n - the number of elements to skippublic void forEach(Consumer<? super T> action)
This is a terminal operation.
action - the action to be performed on each elementpublic <R> R reduce(R identity,
BiFunction<? super R,? super T,? extends R> accumulator)
This is a terminal operation.
R - the type of the resultidentity - the initial valueaccumulator - the accumulation functionpublic Optional<T> reduce(BiFunction<T,T,T> accumulator)
This is a terminal operation.
accumulator - the accumulation functionpublic Object[] toArray()
This is a terminal operation.
toArray(com.annimon.stream.function.IntFunction)public <R> R[] toArray(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 sizepublic <R> R collect(Supplier<R> supplier, 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)public <R,A> R collect(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)public Optional<T> min(Comparator<? super T> comparator)
This is a terminal operation.
comparator - the Comparator to compare elementspublic Optional<T> max(Comparator<? super T> comparator)
This is a terminal operation.
comparator - the Comparator to compare elementspublic long count()
This is a terminal operation.
public boolean anyMatch(Predicate<? super T> predicate)
This is a short-circuiting terminal operation.
predicate - the predicate used to match elementstrue if any elements match the given predicate, otherwise falsepublic boolean allMatch(Predicate<? super T> predicate)
This is a short-circuiting terminal operation.
predicate - the predicate used to match elementstrue if all elements match the given predicate, otherwise falsepublic boolean noneMatch(Predicate<? super T> predicate)
This is a short-circuiting terminal operation.
predicate - the predicate used to match elementstrue if no elements match the given predicate, otherwise falseCopyright © 2016. All rights reserved.