public interface FastStream<T>
extends java.lang.Iterable<T>
Whist the Stream API is nice for some operations. There
are some uses cases where a simpler approach would be faster and
more efficient. This API aims to solve this.
FastStream supports multiple evaluation, however, it is undefined what will happen
if the backing collections/Iterables change between evaluations. Specific operations
may perform internal caching, others may not.
This Iterable implements both a fast path, and the regular iteration path.
The fast path exists via the use of Iterable.forEach(java.util.function.Consumer<? super T>). Which
is recommended if you intend on consuming the entire stream. This path
is much faster than regular
Created by covers1624 on 19/1/23.
| Modifier and Type | Interface and Description |
|---|---|
static class |
FastStream.Concatenated<T>
|
static class |
FastStream.ConcatenatedN<T>
A
FastStream for an array of concatenated Iterables. |
static class |
FastStream.Distinct<T>
|
static class |
FastStream.Filtered<T>
A
FastStream with a filtering function applied. |
static class |
FastStream.FlatMapped<T,R>
A
FastStream with a flat mapping option applied
to each element flattened together. |
static class |
FastStream.Group<K,V>
Represents a Key to sequence of values in a
groupBy(java.util.function.Function<? super T, ? extends K>) grouping. |
static class |
FastStream.Grouped<T,K,V>
A
FastStream of elements grouped by a specific key. |
static class |
FastStream.Internal |
static class |
FastStream.Mapped<T,R>
A
FastStream with a mapping function applied. |
static class |
FastStream.OfN<T>
A
FastStream for an array of elements. |
static class |
FastStream.OfSingle<T>
A
FastStream for a single element. |
static class |
FastStream.Peeked<T>
A
FastStream with a listener attached. |
static class |
FastStream.Reversed<T>
A
FastStream in reverse order. |
static class |
FastStream.Sliced<T>
A
FastStream with a min/max filter applied. |
static class |
FastStream.Sorted<T>
A
FastStream sorted by a comparator. |
static class |
FastStream.TypeCheck<T,S> |
static class |
FastStream.Wrapped<T>
Wraps a regular
Iterable into a FastStream. |
| Modifier and Type | Field and Description |
|---|---|
static FastStream<?> |
EMPTY
Static empty instance.
|
| Modifier and Type | Method and Description |
|---|---|
default boolean |
allMatch(java.util.function.Predicate<? super T> pred)
Tests if all elements in the
FastStream match the provided Predicate. |
default boolean |
anyMatch(java.util.function.Predicate<? super T> pred)
Tests if any element in the
FastStream matches the provided Predicate. |
static <T> FastStream<T> |
concat(java.lang.Iterable<? extends T>... iterables)
Returns a concatenated
FastStream containing the elements from
the provided Iterable array. |
default FastStream<T> |
concat(java.lang.Iterable<? extends T> other)
Returns a
FastStream with the provided Iterable concatenated
after. |
static <T> FastStream<T> |
concatMany(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> iterables)
|
default int |
count()
Evaluates the stream, counting the number of elements contained within.
|
default FastStream<T> |
distinct()
Returns a
FastStream containing all elements that are unique
according to their Object.hashCode()/Object.equals(java.lang.Object) identity. |
default double |
doubleSum(java.util.function.ToDoubleFunction<? super T> func)
Sum all elements in the stream to a double, using the
provided
ToDoubleFunction to convert each element to
a double. |
static <T> FastStream<T> |
empty()
Returns an empty
FastStream singleton. |
default FastStream<T> |
filter(java.util.function.Predicate<? super T> pred)
Returns a
FastStream containing all elements that pass
the provided Predicate filter. |
default FastStream<T> |
filterNot(java.util.function.Predicate<? super T> pred)
Returns a
FastStream containing all elements that fail
the provided Predicate filter. |
default java.util.Optional<T> |
findFirst() |
default java.util.Optional<T> |
findLast() |
default T |
first() |
default T |
firstOrDefault() |
default T |
firstOrDefault(T _default) |
default <R> FastStream<R> |
flatMap(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> func)
Returns a
FastStream with each element transformed by
the provided Function concatenated together. |
default java.util.Optional<T> |
fold(java.util.function.BinaryOperator<T> accumulator)
Returns the result of a folding operation applied to the
FastStream contents. |
default <U> U |
fold(U identity,
java.util.function.BiFunction<? super U,? super T,? extends U> accumulator)
Returns the result of a folding operation applied to the
FastStream contents. |
default <K> FastStream<FastStream.Group<K,T>> |
groupBy(java.util.function.Function<? super T,? extends K> keyFunc)
Returns a
FastStream containing all elements grouped by
a key. |
default <K,V> FastStream<FastStream.Group<K,V>> |
groupBy(java.util.function.Function<? super T,? extends K> keyFunc,
java.util.function.Function<? super T,? extends V> valueFunc)
Returns a
FastStream containing all elements grouped by
a key. |
static <T> FastStream.TypeCheck<T,T> |
infer()
Used to nudge Javac to perform looser inference on return types of some
collecting functions provided in here.
|
default int |
intSum(java.util.function.ToIntFunction<? super T> func)
Sum all elements in the stream to an integer, using the
provided
ToIntFunction to convert each element to
an integer. |
default boolean |
isEmpty()
Tests if the stream is empty.
|
default java.lang.String |
join(java.lang.String sep)
Join all elements of this stream together into a
String,
separated by sep. |
default int |
knownLength()
Returns the known length for the stream.
|
default int |
knownLength(boolean consumeToCalculate)
Returns the known length for the stream.
|
default T |
last() |
default T |
lastOrDefault() |
default T |
lastOrDefault(T _default) |
default FastStream<T> |
limit(int max)
Returns a
FastStream which will let at most max elements pass. |
default long |
longSum(java.util.function.ToLongFunction<? super T> func)
Sum all elements in the stream to a long, using the
provided
ToLongFunction to convert each element to
a long. |
default <R> FastStream<R> |
map(java.util.function.Function<? super T,? extends R> func)
Returns a
FastStream with each element transformed by
the provided Function. |
default T |
maxBy(java.util.function.ToIntFunction<T> func)
Returns the element in the stream with the highest value returned by
the provided
ToIntFunction. |
default T |
maxByOrDefault(java.util.function.ToIntFunction<T> func)
Returns the element in the stream with the highest value returned by
the provided
ToIntFunction. |
default T |
maxByOrDefault(java.util.function.ToIntFunction<T> func,
T _default)
Returns the element in the stream with the highest value returned by
the provided
ToIntFunction. |
default boolean |
noneMatch(java.util.function.Predicate<? super T> pred)
Tests if no elements in the
FastStream match the provided Predicate. |
static <T> FastStream<T> |
of()
Overload of
empty() for convenience. |
static <T> FastStream<T> |
of(java.lang.Iterable<? extends T> itr)
Wraps the provided
Iterable to a FastStream |
static <T> FastStream<T> |
of(java.util.Optional<? extends T> opt)
Returns a
FastStream for an Optional. |
static <T> FastStream<T> |
of(T... things)
Returns a
FastStream for an array of objects. |
static <T> FastStream<T> |
of(T thing)
Returns a
FastStream for a single object. |
static <T> FastStream<T> |
ofNullable(T thing)
Returns a
FastStream for a single object. |
default T |
only() |
default T |
onlyOrDefault() |
default T |
onlyOrDefault(T _default) |
default FastStream<T> |
peek(java.util.function.Consumer<? super T> cons)
Returns a
FastStream which listens to all the elements which pass to the next operation. |
default FastStream<T> |
reversed()
Returns a
FastStream in reverse order. |
default FastStream<T> |
skip(int n)
Returns a
FastStream which will skip n number of elements. |
default FastStream<T> |
sorted()
Returns a
FastStream sorted based on the elements natural sort order. |
default FastStream<T> |
sorted(java.util.Comparator<? super T> comparator)
Returns a
FastStream sorted based on the provided comparator. |
default java.lang.Object[] |
toArray()
Collects this stream into an
Object[]. |
default T[] |
toArray(T[] arr)
Collects the stream into a
T[]. |
default <any> |
toImmutableList()
Collects this stream into a
ImmutableList. |
default <R> <any> |
toImmutableList(FastStream.TypeCheck<R,? super T> check)
Collects this stream into a
ImmutableList. |
default <K,V> <any> |
toImmutableMap(java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc)
Collects this stream into an
ImmutableMap. |
default <K,V> <any> |
toImmutableMap(java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Collects this stream into an
ImmutableMap. |
default <any> |
toImmutableSet()
Collects this stream into a
ImmutableSet. |
default <R> <any> |
toImmutableSet(FastStream.TypeCheck<R,? super T> check)
Collects this stream into a
ImmutableSet. |
default <K,V> java.util.LinkedHashMap<K,V> |
toLinkedHashMap(java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc)
Collects this stream into a
LinkedHashMap. |
default <K,V> java.util.LinkedHashMap<K,V> |
toLinkedHashMap(java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Collects this stream into a
LinkedHashMap. |
default java.util.LinkedHashSet<T> |
toLinkedHashSet()
Collects this stream into a
LinkedHashSet. |
default <R> java.util.LinkedHashSet<R> |
toLinkedHashSet(FastStream.TypeCheck<R,? super T> check)
Collects this stream into a
LinkedHashSet. |
default java.util.LinkedList<T> |
toLinkedList()
Collects this stream into a
LinkedList. |
default <R> java.util.LinkedList<R> |
toLinkedList(FastStream.TypeCheck<R,? super T> check)
Collects this stream into a
LinkedList. |
default java.util.ArrayList<T> |
toList()
Collects this stream into an
ArrayList. |
default <R> java.util.List<R> |
toList(FastStream.TypeCheck<R,? super T> check)
Collects this stream into an
ArrayList. |
default <K,V> java.util.HashMap<K,V> |
toMap(java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc)
Collects this stream into a
HashMap. |
default <K,V> java.util.HashMap<K,V> |
toMap(java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Collects this stream into a
HashMap. |
default <K,V,M extends java.util.Map<K,V>> |
toMap(M map,
java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc)
Collects this stream into the provided
Map. |
default <K,V,M extends java.util.Map<K,V>> |
toMap(M map,
java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Collects this stream into the provided
Map. |
default java.util.HashSet<T> |
toSet()
Collects this stream into a
HashSet. |
default <R> java.util.HashSet<R> |
toSet(FastStream.TypeCheck<R,? super T> check)
Collects this stream into a
HashSet. |
static final FastStream<?> EMPTY
empty() for return type inference.static <T> FastStream<T> empty()
FastStream singleton.FastStream.static <T> FastStream<T> of()
empty() for convenience.FastStream.static <T> FastStream<T> of(java.lang.Iterable<? extends T> itr)
Iterable to a FastStream
This method may return the same object if it's already a FastStream,
the empty singleton if it's a Collection and provably empty,
or a new Wrapped instance.
The Wrapped instance is careful to expose the underlying iterator of the provided
Iterable, as well as forward calls to Iterable.forEach(java.util.function.Consumer<? super T>)
itr - The Iterable to wrap.static <T> FastStream<T> of(T thing)
FastStream for a single object.thing - The thing.FastStream for the single object.static <T> FastStream<T> ofNullable(T thing)
FastStream for a single object.
If the provided object is null,
an empty FastStream is returned.
thing - The thing.FastStream for the single object or empty.static <T> FastStream<T> of(java.util.Optional<? extends T> opt)
FastStream for an Optional.
If the provided Optional is not Optional.isPresent(),
an empty FastStream is returned.
opt - The Optional.FastStream for the Optional or empty.@SafeVarargs static <T> FastStream<T> of(T... things)
FastStream for an array of objects.
This method is preferred over a wrapper such as Arrays.asList(T...)
due to fewer allocations and virtual/interface calls.
things - The thing.FastStream for the objects.@SafeVarargs static <T> FastStream<T> concat(java.lang.Iterable<? extends T>... iterables)
FastStream containing the elements from
the provided Iterable array.iterables - The Iterables to concatenate.FastStream.static <T> FastStream<T> concatMany(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> iterables)
iterables - The Iterables to concatenate.FastStream.default FastStream<T> concat(java.lang.Iterable<? extends T> other)
FastStream with the provided Iterable concatenated
after.other - The otherFastStream.default FastStream<T> filter(java.util.function.Predicate<? super T> pred)
FastStream containing all elements that pass
the provided Predicate filter.pred - The Predicate to apply.FastStream.default FastStream<T> filterNot(java.util.function.Predicate<? super T> pred)
FastStream containing all elements that fail
the provided Predicate filter.pred - The Predicate to apply.FastStream.default <R> FastStream<R> map(java.util.function.Function<? super T,? extends R> func)
FastStream with each element transformed by
the provided Function.func - The Function to apply.FastStream.default <R> FastStream<R> flatMap(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> func)
FastStream with each element transformed by
the provided Function concatenated together.func - The Function to apply producing the Iterable for concatenation.FastStream.default FastStream<T> distinct()
FastStream containing all elements that are unique
according to their Object.hashCode()/Object.equals(java.lang.Object) identity.FastStream.default <K> FastStream<FastStream.Group<K,T>> groupBy(java.util.function.Function<? super T,? extends K> keyFunc)
FastStream containing all elements grouped by
a key. The provided Function is used to extract the key from the element.keyFunc - The key Function.FastStreamdefault <K,V> FastStream<FastStream.Group<K,V>> groupBy(java.util.function.Function<? super T,? extends K> keyFunc, java.util.function.Function<? super T,? extends V> valueFunc)
FastStream containing all elements grouped by
a key. The provided Function is used to extract the key from the element.keyFunc - The key Function.valueFunc - The value Function.FastStreamdefault FastStream<T> sorted()
FastStream sorted based on the elements natural sort order.
This requires that T implements Comparable.
FastStream.default FastStream<T> sorted(java.util.Comparator<? super T> comparator)
FastStream sorted based on the provided comparator.comparator - The Comparator to apply.FastStream.default FastStream<T> reversed()
FastStream in reverse order.FastStream.default FastStream<T> peek(java.util.function.Consumer<? super T> cons)
FastStream which listens to all the elements which pass to the next operation.cons - The listener Consumer.FastStream.default FastStream<T> limit(int max)
FastStream which will let at most max elements pass.
A special case of -1 is provided to indicate no max limit.
max - The maximum amount of elements to pass through, or -1.FastStream.default FastStream<T> skip(int n)
FastStream which will skip n number of elements.n - The number of elements to skip.FastStream.default boolean anyMatch(java.util.function.Predicate<? super T> pred)
FastStream matches the provided Predicate.pred - The Predicate to apply.Predicate.default boolean allMatch(java.util.function.Predicate<? super T> pred)
FastStream match the provided Predicate.pred - The Predicate to apply.Predicate.default boolean noneMatch(java.util.function.Predicate<? super T> pred)
FastStream match the provided Predicate.pred - The Predicate to apply.Predicate.default boolean isEmpty()
This method is provided for convenience. Generally, using knownLength() or
a collecting operation would achieve greater performance.
default int knownLength()
This may return -1 if the stream contains operations
that can't know their length before computing (filter operations).
-1default int knownLength(boolean consumeToCalculate)
If true is specified to consumeToCalculate causes
operations which would not normally know their length prior to a
terminal operation being applied, to cache their result under the assumption
that the stream is about to be fully consumed.
consumeToCalculate - If the caller intends to consume the entire stream via Iterable.forEach(java.util.function.Consumer<? super T>) after calling.default int count()
default <U> U fold(U identity,
java.util.function.BiFunction<? super U,? super T,? extends U> accumulator)
FastStream contents.identity - The initial value.accumulator - The Function responsible for merging elements in the stream together.null if identity is null and the stream is empty.default java.util.Optional<T> fold(java.util.function.BinaryOperator<T> accumulator)
FastStream contents.accumulator - The Function responsible for merging elements in the stream together.FastStream contained no elements.default int intSum(java.util.function.ToIntFunction<? super T> func)
ToIntFunction to convert each element to
an integer.func - The ToIntFunction to apply.default long longSum(java.util.function.ToLongFunction<? super T> func)
ToLongFunction to convert each element to
a long.func - The ToLongFunction to apply.default double doubleSum(java.util.function.ToDoubleFunction<? super T> func)
ToDoubleFunction to convert each element to
a double.func - The ToDoubleFunction to apply.default java.util.Optional<T> findFirst()
default T first()
default T firstOrDefault()
null.default T firstOrDefault(T _default)
_default - The default value to return if the stream is empty._default.default java.util.Optional<T> findLast()
default T last()
default T lastOrDefault()
null.default T lastOrDefault(T _default)
_default - The default value to return if the stream is empty._default.default T only()
default T onlyOrDefault()
null if the stream is empty or contains more than one element.default T onlyOrDefault(T _default)
_default - The default value to return if the stream is empty or contains more than one element._default if the stream is empty
or contains more than one element.default T maxBy(java.util.function.ToIntFunction<T> func)
ToIntFunction.func - The ToIntFunction.default T maxByOrDefault(java.util.function.ToIntFunction<T> func)
ToIntFunction.func - The ToIntFunction.null if the stream is empty.default T maxByOrDefault(java.util.function.ToIntFunction<T> func, T _default)
ToIntFunction.func - The ToIntFunction._default - The default value to return if the stream is empty._default if the stream is empty.default java.util.ArrayList<T> toList()
ArrayList.ArrayList.default <R> java.util.List<R> toList(FastStream.TypeCheck<R,? super T> check)
ArrayList.check - Call infer() in this argument for flexible return inference.ArrayList.default java.util.LinkedList<T> toLinkedList()
LinkedList.LinkedList.default <R> java.util.LinkedList<R> toLinkedList(FastStream.TypeCheck<R,? super T> check)
LinkedList.check - Call infer() in this argument for flexible return inference.LinkedList.default <any> toImmutableList()
ImmutableList.ImmutableList.default <R> <any> toImmutableList(FastStream.TypeCheck<R,? super T> check)
ImmutableList.check - Call infer() in this argument for flexible return inference.ImmutableList.default java.util.HashSet<T> toSet()
HashSet.HashSet.default <R> java.util.HashSet<R> toSet(FastStream.TypeCheck<R,? super T> check)
HashSet.check - Call infer() in this argument for flexible return inference.HashSet.default java.util.LinkedHashSet<T> toLinkedHashSet()
LinkedHashSet.LinkedHashSet.default <R> java.util.LinkedHashSet<R> toLinkedHashSet(FastStream.TypeCheck<R,? super T> check)
LinkedHashSet.check - Call infer() in this argument for flexible return inference.LinkedHashSet.default <any> toImmutableSet()
ImmutableSet.ImmutableSet.default <R> <any> toImmutableSet(FastStream.TypeCheck<R,? super T> check)
ImmutableSet.check - Call infer() in this argument for flexible return inference.ImmutableSet.default java.lang.Object[] toArray()
Object[].Object[].default T[] toArray(T[] arr)
T[].arr - The template array to use. If the array is large enough
to fit all elements in the stream, this will be used with
a single null element after the last element.T[].default <K,V> java.util.HashMap<K,V> toMap(java.util.function.Function<? super T,? extends K> kFunc, java.util.function.Function<? super T,? extends V> vFunc)
HashMap.
In the event of a collision, the first value will be used.
kFunc - The Function to extracting the key.vFunc - The Function to extracting the value.HashMap.default <K,V> java.util.HashMap<K,V> toMap(java.util.function.Function<? super T,? extends K> kFunc, java.util.function.Function<? super T,? extends V> vFunc, java.util.function.BinaryOperator<V> mergeFunc)
HashMap.kFunc - The Function to extracting the key.vFunc - The Function to extracting the value.mergeFunc - The BinaryOperator to resolve merge conflicts.HashMap.default <K,V> java.util.LinkedHashMap<K,V> toLinkedHashMap(java.util.function.Function<? super T,? extends K> kFunc, java.util.function.Function<? super T,? extends V> vFunc)
LinkedHashMap.
In the event of a collision, the first value will be used.
kFunc - The Function to extracting the key.vFunc - The Function to extracting the value.LinkedHashMap.default <K,V> java.util.LinkedHashMap<K,V> toLinkedHashMap(java.util.function.Function<? super T,? extends K> kFunc, java.util.function.Function<? super T,? extends V> vFunc, java.util.function.BinaryOperator<V> mergeFunc)
LinkedHashMap.kFunc - The Function to extracting the key.vFunc - The Function to extracting the value.mergeFunc - The BinaryOperator to resolve merge conflicts.LinkedHashMap.default <K,V> <any> toImmutableMap(java.util.function.Function<? super T,? extends K> kFunc, java.util.function.Function<? super T,? extends V> vFunc)
ImmutableMap.
In the event of a collision, the first value will be used.
kFunc - The Function to extracting the key.vFunc - The Function to extracting the value.ImmutableMap.default <K,V> <any> toImmutableMap(java.util.function.Function<? super T,? extends K> kFunc, java.util.function.Function<? super T,? extends V> vFunc, java.util.function.BinaryOperator<V> mergeFunc)
ImmutableMap.kFunc - The Function to extracting the key.vFunc - The Function to extracting the value.mergeFunc - The BinaryOperator to resolve merge conflicts.ImmutableMap.default <K,V,M extends java.util.Map<K,V>> M toMap(M map,
java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc)
Map.
In the event of a collision, the first value will be used.
kFunc - The Function to extracting the key.vFunc - The Function to extracting the value.Map.default <K,V,M extends java.util.Map<K,V>> M toMap(M map,
java.util.function.Function<? super T,? extends K> kFunc,
java.util.function.Function<? super T,? extends V> vFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Map.kFunc - The Function to extracting the key.vFunc - The Function to extracting the value.mergeFunc - The BinaryOperator to resolve merge conflicts.Map.default java.lang.String join(java.lang.String sep)
String,
separated by sep.
This function will use the element's Object.toString().
sep - The seperator.String.static <T> FastStream.TypeCheck<T,T> infer()