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 | 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. |
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() |
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(java.lang.Iterable<T> itr) |
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 <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.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 <T> StreamableIterable<T> empty()
static <T> StreamableIterable<T> of(java.lang.Iterable<T> itr)
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 java.util.ArrayList<T> toList()
StreamableIterable to an ArrayList.ArrayList.default java.util.LinkedList<T> toLinkedList()
StreamableIterable to a LinkedList.LinkedList.default java.util.HashSet<T> toSet()
StreamableIterable to a HashSet.HashSet.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,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