Modifier and Type | Method and Description |
---|---|
static <T,Z> Stream<Pair<T,Z>> |
combinations(Stream<T> stream,
Functions.Function0<? extends Stream<Z>> streamSupplier)
This function returns a stream of the Cartesian Product of
stream and the elements of the stream
provided by streamSupplier . |
static <T,Z,R> Stream<R> |
combinations(Stream<T> stream,
Functions.Function0<? extends Stream<Z>> streamSupplier,
Functions.Function2<? super T,? super Z,? extends R> combiner)
This function returns a stream of the Cartesian product of
stream and the elements of the stream
provided by streamSupplier . |
static <T,Z> Stream<Pair<T,Z>> |
combinations(Stream<T> stream,
Iterable<Z> combineWith)
This function returns a stream of the Cartesian product of
stream and the iterable combineWith . |
static <T,Z,R> Stream<R> |
combinations(Stream<T> stream,
Iterable<Z> combineWith,
Functions.Function2<? super T,? super Z,? extends R> combiner)
This function returns a stream of the Cartesian product of
stream and the iterable combineWith . |
static <T,U> Stream<U> |
filter(Stream<T> input,
Class<? extends U> clazz)
Filters the given Stream
input to only contain elements instance
of the given class clazz . |
static <T> Stream<T> |
filterNull(Stream<T> stream)
Filters a
stream to not include null values. |
static <T> Optional<T> |
findAny(Stream<T> stream,
Predicate<T> test)
This method is a shortcut for
stream.filter(test).findAny() . |
static <T> Optional<T> |
findFirst(Stream<T> stream,
Predicate<T> test)
This method is a shortcut for
stream.filter(test).findFirst() . |
static <T,U> Stream<U> |
flatMap(Stream<T> stream,
Functions.Function1<? super T,? extends Iterable<? extends U>> flatMapper)
This method provides a convenient way to
flatMap on a Stream by providing an
Iterable instead of a Stream . |
static <T> Stream<T> |
iterate(Class<Stream> clazz,
T seed,
Predicate<? super T> hasNext,
UnaryOperator<T> next)
This method redirects to
<T>iterate(T, Predicate<? super T>, UnaryOperator<T>) .This allows e.g. |
static <T> Stream<T> |
iterate(T seed,
Predicate<? super T> hasNext,
UnaryOperator<T> next)
This method provides functionality that is directly available on the Java 9 Stream class.
This method will construct a Stream that provides seed as an initial element. |
static <T extends Comparable<? super T>> |
max(Stream<T> stream)
This is a shortcut for
stream.max(Comparator.naturalOrder()) . |
static <T extends Comparable<? super T>> |
min(Stream<T> stream)
This is a shortcut for
stream.min(Comparator.naturalOrder()) . |
static <T> Stream<T> |
operator_plus(Stream<? extends T> first,
Stream<? extends T> second)
Operator shortcut for
Stream.concat(first,second) . |
static <T,C extends Collection<T>> |
toCollection(Stream<T> stream,
Supplier<C> collectionFactory)
This function is a simple shortcut for
stream.collect(Collectors.toCollection(collectionFactory)) . |
static <T> List<T> |
toList(Stream<T> stream)
This function is a simple shortcut for
stream.collect(Collectors.toList()) . |
static <T> Set<T> |
toSet(Stream<T> stream)
This function is a simple shortcut for
stream.collect(Collectors.toSet()) . |
public static <T,U> Stream<U> flatMap(Stream<T> stream, Functions.Function1<? super T,? extends Iterable<? extends U>> flatMapper)
flatMap
on a Stream
by providing an
Iterable
instead of a Stream
. If a given Iterable
is instance of
Collection
, the stream()
method of the interface will be called.T
- Type of elements provided by stream
.U
- Type of elements flatMapper
maps to from T
.stream
- the input stream; each element will be passed to flatMapper
. The resulting
elements will be concatenated to a flat Stream
containing all elements of the returned
Iterable
s.flatMapper
- function returning an Iterable
for each element in stream
.flatMapper
for each elements
provided by stream
.NullPointerException
- if stream
or flatMapper
is null
, or if
flatMapper
returns a null
object.public static <T,U> Stream<U> filter(Stream<T> input, Class<? extends U> clazz)
input
to only contain elements instance
of the given class clazz
. Note that the returned stream is of type
Stream<U>
, so a stream of the the class it is filtered by.T
- Type of elements provided by stream
.U
- Type, elements in input
are checked to be. The resulting stream only
contains elements of that type.input
- to be filtered to only contain instances of clazz
clazz
- object of the class the input
should be filtered for.input
Stream filtered to only contain instances of clazz
.public static <T> Optional<T> findFirst(Stream<T> stream, Predicate<T> test)
stream.filter(test).findFirst()
.
Meaning this operation will try to find the fist element in the stream
matching the given test
. If such an element is found, the returned
Optional will have the found element present. Otherwise an empty Optional
will be returned.T
- Type of elements in stream
.stream
- the stream that will be searched for an element matching
the test
test
- the predicate checking for an matching element. An element
is considered a match when the test returns true
for a given
element. If test
throws an exception this exception will be
thrown from this method.stream
or an empty optional if no element matched test
.NullPointerException
- if stream
or test
is null
public static <T> Optional<T> findAny(Stream<T> stream, Predicate<T> test)
stream.filter(test).findAny()
.
Meaning this operation will try to find the an element in the stream
matching the given test
. If such an element is found, the returned
Optional will have the found element present. Otherwise an empty Optional
will be returned.T
- Type of elements in stream
.stream
- the stream that will be searched for an element matching
the test
test
- the predicate checking for an matching element. An element
is considered a match when the test returns true
for a given
element. If test
throws an exception this exception will be
thrown from this method.stream
or an empty optional if no element matched test
.NullPointerException
- if stream
or test
is null
public static <T extends Comparable<? super T>> Optional<T> min(Stream<T> stream)
stream.min(Comparator.naturalOrder())
.
This extension method conveniently seaches the minimum element according
to the order implemented via the Comparable
interface the elements
of the stream implement.T
- Type of elements in stream
.stream
- the stream to be reduced to the minimum element.public static <T extends Comparable<? super T>> Optional<T> max(Stream<T> stream)
stream.max(Comparator.naturalOrder())
.
This extension method conveniently seaches the maximum element according
to the order implemented via the Comparable
interface the elements
of the stream implement.T
- Type of elements in stream
.stream
- the stream to be reduced to the maximum element.public static <T> Stream<T> filterNull(Stream<T> stream)
stream
to not include null
values.T
- Type of elements in stream
.stream
- the stream to be filtered to exclude null
values.public static <T> List<T> toList(Stream<T> stream)
stream.collect(Collectors.toList())
.T
- Type of elements in stream
.stream
- the stream to be collected into a new List
.stream
public static <T> Set<T> toSet(Stream<T> stream)
stream.collect(Collectors.toSet())
.T
- Type of elements in stream
.stream
- the stream to be collected into a new Set
.stream
public static <T,C extends Collection<T>> C toCollection(Stream<T> stream, Supplier<C> collectionFactory)
stream.collect(Collectors.toCollection(collectionFactory))
.
Means that all elements of stream
are added to the collection supplied by collectionFactory
.T
- Type of elements in stream
.C
- Type of (mutable) collection all elements of stream
are added to.stream
- the stream to be collected into a new Set
.collectionFactory
- a Supplier
which returns a new, empty
Collection
(of an arbitrary sub-type) supporting mutation.stream
public static <T,Z> Stream<Pair<T,Z>> combinations(Stream<T> stream, Iterable<Z> combineWith)
stream
and the iterable combineWith
.
Note that this function will multiple times try to create a stream from the Iterable combineWith
. The
combination of elements of the stream
and combineWith
are represented as Pair
s of the
values from both sources.T
- Type of elements in stream
.Z
- Type of elements in combineWith
.stream
- the stream that's elements are combined with every elements from combineWith
combineWith
- the elements to be combined with each element from stream
stream
with every element of combineWith
.public static <T,Z> Stream<Pair<T,Z>> combinations(Stream<T> stream, Functions.Function0<? extends Stream<Z>> streamSupplier)
stream
and the elements of the stream
provided by streamSupplier
.
Note that this function will call streamSupplier
for each element in stream
, so it is expected
that on every call the given streamSupplier
returns a stream of the same elements. The
combination of elements of the stream
and the stream provided by streamSupplier
are represented
as Pair
s of the values from both sources.T
- Type of elements in stream
.Z
- Type of elements provided by stream, provided by streamSupplier
stream
- the stream that's elements are combined with every elements from combineWith
streamSupplier
- the elements to be combined with each element from stream
stream
with every element of the stream provided by streamSupplier
.public static <T,Z,R> Stream<R> combinations(Stream<T> stream, Iterable<Z> combineWith, Functions.Function2<? super T,? super Z,? extends R> combiner)
stream
and the iterable combineWith
.
Note that this function will multiple times try to create a stream from the Iterable combineWith
. Every
combination of elements of the stream
and combineWith
will be passed to the combiner
to
create a resulting combined element.T
- Type of elements in stream
.Z
- Type of elements in combineWith
.R
- Result type of combiner
function, which fuses a T
and a Z
object to one instance of R
.stream
- the stream that's elements are combined with every elements from combineWith
combineWith
- the elements to be combined with each element from stream
combiner
- will be used to combine elements from stream
and combineWith
to result values.stream
with every element of combineWith
combined by using the combiner
function.public static <T,Z,R> Stream<R> combinations(Stream<T> stream, Functions.Function0<? extends Stream<Z>> streamSupplier, Functions.Function2<? super T,? super Z,? extends R> combiner)
stream
and the elements of the stream
provided by streamSupplier
.
Note that this function will call streamSupplier
for each element in stream
, so it is expected
that on every call the given streamSupplier
returns a stream of the same elements. Every
combination of elements of the stream
and elements of stream provided by streamSupplier
will be
passed to the combiner
to create a resulting combined element.T
- Type of elements in stream
.Z
- Type of elements in stream, provided by streamSupplier
.R
- Result type of combiner
function, which fuses a T
and a Z
object to one instance of R
.stream
- the stream that's elements are combined with every elements from combineWith
streamSupplier
- the elements to be combined with each element from stream
combiner
- will be used to combine elements from stream
and stream provided by streamSupplier
to result values.stream
with every element of the stream provided by streamSupplier
combined by using the combiner
function.public static <T> Stream<T> operator_plus(Stream<? extends T> first, Stream<? extends T> second)
Stream.concat(first,second)
.T
- Type of resulting concatinated stream. Both input streams must provide T
objects, or objects of sub-types of T
.first
- containing the elements first provided by the resulting Streamsecond
- elements provided by the resulting stream after the elements of first
.first
and second
public static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)
seed
as an initial element. Elements will
only be provided if hasNext
returns true
when applied to the element. If hasNext
returns false
for the initial element the returned Stream will be empty.
The next value(s) provided by the stream will be computed by the previous element using the next
function. The stream will terminate and not provide this element if hasNext
does not hold for
this element.T
- Type of elements of produced streamseed
- first element returned by the stream and seed for following elements by using next
.hasNext
- before each element is provided (except for the first one)next
- operation providing the next elementseed
, hasNext
, and next
.public static <T> Stream<T> iterate(Class<Stream> clazz, T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)
<T>iterate(T, Predicate<? super T>, UnaryOperator<T>)
.Stream.iterate(0,[it<100],[it+1])
in Xtend, which does not require code changes when
switching to Java 9 to take advantage of the native implementation of this method.T
- Type of elements of produced streamclazz
- will be ignoredseed
- first element returned by the stream and seed for following elements by using next
.hasNext
- before each element is provided (except for the first one)next
- operation providing the next elementseed
, hasNext
, and next
.StreamExtensions#iterate(Object, Predicate, UnaryOperator)
Copyright © 2019. All rights reserved.