public interface StreamableIterable<T>
extends java.lang.Iterable<T>
Whilst the Stream API is nice for some operations. There
are some use cases where a simpler wrapped Iterable approach would
be faster and more memory efficient, that's what this class aims to solve.
Created by covers1624 on 1/10/21.
| Modifier and Type | Field and Description |
|---|---|
static StreamableIterable<?> |
EMPTY
Static empty instance.
|
| Modifier and Type | Method and Description |
|---|---|
default boolean |
allMatch(java.util.function.Predicate<? super T> test)
Check if all elements in this
StreamableIterable match the given predicate. |
default boolean |
anyMatch(java.util.function.Predicate<? super T> test)
Check if any elements in this
StreamableIterable match the given predicate. |
static <T> StreamableIterable<T> |
concat(java.lang.Iterable<StreamableIterable<T>> iterables)
Returns an
StreamableIterable containing the elements
from all the provided iterables concatenated together. |
static <T> StreamableIterable<T> |
concat(StreamableIterable<T>... iterables)
Returns an
StreamableIterable containing the elements
from all the provided iterables concatenated together. |
default StreamableIterable<T> |
concat(StreamableIterable<T> other)
Concatenates another
StreamableIterable onto this one. |
default int |
count()
Count the number of elements in this
StreamableIterable. |
default StreamableIterable<T> |
distinct()
Filter the elements in this
StreamableIterable by their hashcode/equals. |
static <T> StreamableIterable<T> |
empty()
Returns an empty
StreamableIterable. |
default StreamableIterable<T> |
filter(java.util.function.Predicate<? super T> pred)
Filter the elements in this
StreamableIterable by matching a Predicate. |
default StreamableIterable<T> |
filterNot(java.util.function.Predicate<? super T> pred)
Filter the elements in this
StreamableIterable by matching a Predicate. |
default java.util.Optional<T> |
findFirst()
Optionally get the first element within this
StreamableIterable. |
default java.util.Optional<T> |
findLast()
Optionally get the first element within this
StreamableIterable. |
default <R> StreamableIterable<R> |
flatMap(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> func)
Flat map the elements in this
StreamableIterable. |
default java.util.Optional<T> |
fold(java.util.function.BinaryOperator<T> accumulator)
Folds each element of this
StreamableIterable into the previous. |
default T |
fold(T identity,
java.util.function.BinaryOperator<T> accumulator)
Folds each element of this
StreamableIterable into the previous. |
default StreamableIterable<T> |
limit(int max)
Limit the number of elements returned by this
StreamableIterable to the given value. |
default <R> StreamableIterable<R> |
map(java.util.function.Function<? super T,? extends R> func)
Transform the elements in this
StreamableIterable. |
default boolean |
noneMatch(java.util.function.Predicate<? super T> test)
Check if no elements in this
StreamableIterable match the given predicate. |
static <T> StreamableIterable<T> |
of()
Returns an empty
StreamableIterable. |
static <T> StreamableIterable<T> |
of(java.lang.Iterable<T> itr)
Constructs a
StreamableIterable wrapper from the
given Iterable. |
static <T> StreamableIterable<T> |
of(java.util.Optional<T> optional)
Creates an
StreamableIterable for an Optional value. |
static <T> StreamableIterable<T> |
of(T... things)
Creates an
StreamableIterable from a variable list of inputs. |
static <T> StreamableIterable<T> |
of(T thing)
Creates a
StreamableIterable for single input. |
static <T> StreamableIterable<T> |
ofNullable(T thing)
Creates an
StreamableIterable for a Nullable value. |
default T |
only()
Returns the only element in this
StreamableIterable. |
default T |
onlyOrDefault()
Returns the first element found in the
StreamableIterable if it is the only element,
otherwise null is returned. |
default T |
onlyOrDefault(T _default)
Returns the first element found in the
StreamableIterable if it is the only element,
otherwise the default value is returned. |
default java.util.stream.Stream<T> |
parallelStream()
Convert this
StreamableIterable to a parallel Stream. |
default StreamableIterable<T> |
peek(java.util.function.Consumer<T> action)
Peek the elements of this
StreamableIterable as they are consumed. |
default StreamableIterable<T> |
skip(int n)
Skip n number of elements in this
StreamableIterable. |
default java.util.stream.Stream<T> |
stream()
Convert this
StreamableIterable to a Stream. |
default java.lang.Object[] |
toArray()
Collect this
StreamableIterable to an array. |
default T[] |
toArray(T[] arr)
Collect this
StreamableIterable to an array. |
default <any> |
toImmutableList()
Collect this
StreamableIterable to an ImmutableList. |
default <K,V> <any> |
toImmutableMap(java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc)
Collect this
StreamableIterable to am ImmutableMap. |
default <K,V> <any> |
toImmutableMap(java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Collect this
StreamableIterable to am ImmutableMap. |
default <any> |
toImmutableSet()
Collect this
StreamableIterable to a LinkedHashSet. |
default <K,V> java.util.HashMap<K,V> |
toLinkedHashMap(java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc)
Collect this
StreamableIterable to a LinkedHashMap. |
default <K,V> java.util.HashMap<K,V> |
toLinkedHashMap(java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Collect this
StreamableIterable to a LinkedHashMap. |
default java.util.LinkedHashSet<T> |
toLinkedHashSet()
Collect this
StreamableIterable to a LinkedHashSet. |
default java.util.LinkedList<T> |
toLinkedList()
Collect this
StreamableIterable to a LinkedList. |
default java.util.ArrayList<T> |
toList()
Collect this
StreamableIterable to an ArrayList. |
default <K,V> java.util.HashMap<K,V> |
toMap(java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc)
Collect this
StreamableIterable to a HashMap. |
default <K,V> java.util.HashMap<K,V> |
toMap(java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Collect this
StreamableIterable to a HashMap. |
default <K,V,M extends java.util.Map<K,V>> |
toMap(M map,
java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc)
Collect this
StreamableIterable to a Map. |
default <K,V,M extends java.util.Map<K,V>> |
toMap(M map,
java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc,
java.util.function.BinaryOperator<V> mergeFunc)
Collect this
StreamableIterable to a Map. |
default java.util.HashSet<T> |
toSet()
Collect this
StreamableIterable to a HashSet. |
static final StreamableIterable<?> EMPTY
empty().static <T> StreamableIterable<T> empty()
StreamableIterable.StreamableIterablestatic <T> StreamableIterable<T> of(java.lang.Iterable<T> itr)
StreamableIterable wrapper from the
given Iterable.itr - The Iterable to wrap.StreamableIterablestatic <T> StreamableIterable<T> of()
StreamableIterable.StreamableIterablestatic <T> StreamableIterable<T> of(T thing)
StreamableIterable for single input.thing - The thing.StreamableIterable.static <T> StreamableIterable<T> ofNullable(T thing)
StreamableIterable for a Nullable value.
If the value provided is null, this method will
return empty(), otherwise it will return a new StreamableIterable
containing only the value provided.
thing - The thing.StreamableIterable.static <T> StreamableIterable<T> of(java.util.Optional<T> optional)
StreamableIterable for an Optional value.
If the value provided is not present, this method will
return empty(), otherwise it will return a new StreamableIterable
containing only the value provided.
optional - The thing.StreamableIterable.@SafeVarargs static <T> StreamableIterable<T> of(T... things)
StreamableIterable from a variable list of inputs.things - The things.StreamableIterable.@SafeVarargs static <T> StreamableIterable<T> concat(StreamableIterable<T>... iterables)
StreamableIterable containing the elements
from all the provided iterables concatenated together.iterables - The StreamableIterables to concatenate.StreamableIterable.static <T> StreamableIterable<T> concat(java.lang.Iterable<StreamableIterable<T>> iterables)
StreamableIterable containing the elements
from all the provided iterables concatenated together.iterables - The StreamableIterables to concatenate.StreamableIterable.default StreamableIterable<T> concat(StreamableIterable<T> other)
StreamableIterable onto this one.other - The other StreamableIterable to append.StreamableIterable.default StreamableIterable<T> filter(java.util.function.Predicate<? super T> pred)
StreamableIterable by matching a Predicate.
All elements which match this Predicate will be in the resulting StreamableIterable.
pred - The predicate.StreamableIterable with the filter applied.default StreamableIterable<T> filterNot(java.util.function.Predicate<? super T> pred)
StreamableIterable by matching a Predicate.
All elements which do not match this Predicate will be in the resulting StreamableIterable.
pred - The predicate.StreamableIterable with the filter applied.default <R> StreamableIterable<R> map(java.util.function.Function<? super T,? extends R> func)
StreamableIterable.func - The Function to transform with.StreamableIterable with the mapped elements.default <R> StreamableIterable<R> flatMap(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> func)
StreamableIterable.func - The function to get/create the iterable for a given element.StreamableIterable with the flat mapped elements.default StreamableIterable<T> distinct()
StreamableIterable by their hashcode/equals.StreamableIterable with the filtered elements.default StreamableIterable<T> peek(java.util.function.Consumer<T> action)
StreamableIterable as they are consumed.action - The consumer.StreamableIterable with the peek function applied.default StreamableIterable<T> limit(int max)
StreamableIterable to the given value.max - The limit. -1 for infinite.StreamableIterable with the max filter applied.
In the event -1< is supplied, the same StreamableIterable will be provided.
In the event 0 is supplied, an empty StreamableIterable will be provided.default StreamableIterable<T> skip(int n)
StreamableIterable.n - The number of elements to skip.StreamableIterable with the max filter applied.
In the event 0 is supplied, the same StreamableIterable will be provided.default java.lang.Object[] toArray()
StreamableIterable to an array.default T[] toArray(T[] arr)
StreamableIterable to an array.arr - The array to add the elements to.
If all elements to not fit in this array a new array
will be returned of appropriate size.default T fold(T identity, java.util.function.BinaryOperator<T> accumulator)
StreamableIterable into the previous.identity - The starting element.accumulator - The function to fold elements with.default java.util.Optional<T> fold(java.util.function.BinaryOperator<T> accumulator)
StreamableIterable into the previous.accumulator - The function to fold elements with.default int count()
StreamableIterable.default boolean anyMatch(java.util.function.Predicate<? super T> test)
StreamableIterable match the given predicate.test - The predicate.default boolean allMatch(java.util.function.Predicate<? super T> test)
StreamableIterable match the given predicate.test - The predicate.default boolean noneMatch(java.util.function.Predicate<? super T> test)
StreamableIterable match the given predicate.test - The predicate.default java.util.Optional<T> findFirst()
StreamableIterable.default java.util.Optional<T> findLast()
StreamableIterable.default T only()
StreamableIterable.java.lang.IllegalArgumentException - If there are none, or more than one element.default T onlyOrDefault()
StreamableIterable if it is the only element,
otherwise null is returned.null.default T onlyOrDefault(T _default)
StreamableIterable if it is the only element,
otherwise the default value is returned._default - The default value, in the event the StreamableIterable is empty, or has more than one element.default java.util.ArrayList<T> toList()
StreamableIterable to an ArrayList.ArrayList.default java.util.LinkedList<T> toLinkedList()
StreamableIterable to a LinkedList.LinkedList.default <any> toImmutableList()
StreamableIterable to an ImmutableList.ImmutableList.default java.util.HashSet<T> toSet()
StreamableIterable to a HashSet.HashSet.default java.util.LinkedHashSet<T> toLinkedHashSet()
StreamableIterable to a LinkedHashSet.LinkedHashSet.default <any> toImmutableSet()
StreamableIterable to a LinkedHashSet.LinkedHashSet.default <K,V> java.util.HashMap<K,V> toMap(java.util.function.Function<T,K> keyFunc, java.util.function.Function<T,V> valueFunc)
StreamableIterable to a HashMap.
This method will always resolve the existing element on collision.keyFunc - The function for extracting the key.valueFunc - The function for extracting the value.Map.default <K,V> java.util.HashMap<K,V> toMap(java.util.function.Function<T,K> keyFunc, java.util.function.Function<T,V> valueFunc, java.util.function.BinaryOperator<V> mergeFunc)
StreamableIterable to a HashMap.keyFunc - The function for extracting the key.valueFunc - The function for extracting the value.mergeFunc - The function for merging 2 values on collision. (Left existing, Right toAdd)Map.default <K,V> java.util.HashMap<K,V> toLinkedHashMap(java.util.function.Function<T,K> keyFunc, java.util.function.Function<T,V> valueFunc)
StreamableIterable to a LinkedHashMap.
This method will always resolve the existing element on collision.keyFunc - The function for extracting the key.valueFunc - The function for extracting the value.Map.default <K,V> java.util.HashMap<K,V> toLinkedHashMap(java.util.function.Function<T,K> keyFunc, java.util.function.Function<T,V> valueFunc, java.util.function.BinaryOperator<V> mergeFunc)
StreamableIterable to a LinkedHashMap.keyFunc - The function for extracting the key.valueFunc - The function for extracting the value.mergeFunc - The function for merging 2 values on collision. (Left existing, Right toAdd)Map.default <K,V> <any> toImmutableMap(java.util.function.Function<T,K> keyFunc, java.util.function.Function<T,V> valueFunc)
StreamableIterable to am ImmutableMap.
This function collects to an intermediate LinkedHashMap internally,
iteration order will be preserved.
keyFunc - The function for extracting the key.valueFunc - The function for extracting the value.ImmutableMap.default <K,V> <any> toImmutableMap(java.util.function.Function<T,K> keyFunc, java.util.function.Function<T,V> valueFunc, java.util.function.BinaryOperator<V> mergeFunc)
StreamableIterable to am ImmutableMap.
This function collects to an intermediate LinkedHashMap internally,
iteration order will be preserved.
keyFunc - The function for extracting the key.valueFunc - The function for extracting the value.mergeFunc - The function for merging 2 values on collision. (Left existing, Right toAdd)ImmutableMap.default <K,V,M extends java.util.Map<K,V>> M toMap(M map,
java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc)
StreamableIterable to a Map.
This method will always resolve the existing element on collision.map - The map to add the elements to.keyFunc - The function for extracting the key.valueFunc - The function for extracting the value.default <K,V,M extends java.util.Map<K,V>> M toMap(M map,
java.util.function.Function<T,K> keyFunc,
java.util.function.Function<T,V> valueFunc,
java.util.function.BinaryOperator<V> mergeFunc)
StreamableIterable to a Map.map - The map to add the elements to.keyFunc - The function for extracting the key.valueFunc - The function for extracting the value.mergeFunc - The function for merging 2 values on collision. (Left existing, Right toAdd)default java.util.stream.Stream<T> stream()
StreamableIterable to a Stream.Streamdefault java.util.stream.Stream<T> parallelStream()
StreamableIterable to a parallel Stream.Stream