Class Iterables
The stream framework covers most needs for high-level operations on sequences of values. The methods of this class may be a useful light-weight alternative where streams would introduce too much notational or computational complexity.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA list that is populated lazily on demand. -
Method Summary
Modifier and TypeMethodDescriptionstatic <A,B> boolean allZip(BiPredicate<? super A, ? super B> rel, Iterable<? extends A> left, Iterable<? extends B> right) Checks whether all corresponding pairs of elements are in a given relation.static <E> Optional<E> Returns an arbitrary element, if available.static <A,B> boolean anyZip(BiPredicate<? super A, ? super B> rel, Iterable<? extends A> left, Iterable<? extends B> right) Checks whether any corresponding pairs of elements are in a given relation.static <A> Iterables.Cached<A> Returns a view on an iterable that stores iterated values in a list.static <A> Iterable<A> Returns a view on two iterables that contains both their elements in order.static <A> Iterable<A> Returns a view on an iterable that contains one more element at the start.static <A> Iterable<A> Returns a view on an iterable that has the same elements, except the first ones.static <A> Iterable<A> Combines the effects ofdropandtake.static <A> Iterable<A> empty()Returns an empty iterable.static <A> Iterable<A> Returns a view on an iterable that contains only those elements that match a given predicate.static <A,B> Iterable <A> filterWithConstraint(BiPredicate<? super A, ? super B> rel, Iterable<A> things, Iterable<B> constraints) Returns a view on an iterable that contains only those elements that match with a corresponding constraint.static <E> Optional<E> Returns the first element, if available.static <A,B> Iterable <B> Returns a concatenation of subsequences computed with a function.static <A> Iterable<A> Returns a view on an iterable of iterables that contains all of their elements.static <T> voidforEachPair(Iterable<T> data, @Opt Consumer<T> first, BiConsumer<T, T> pairwise, @Opt Consumer<T> last) Performs the given action for all remaining pairs of adjacent elements delivered by the iterator, as well as for the first and last singleton, in order.static <T> voidforEachPair(Iterable<T> data, BiConsumer<T, T> pairwise) Performs the given action for all remaining pairs of adjacent elements delivered by the iterator, in order.static <T> voidforEachTriple(Iterable<T> data, TriConsumer<@Opt T, @Opt T, @Opt T> consumer) Performs the given action for all remaining triples of adjacent elements delivered by the iterator, including partial ones, in order.static <A,B> Iterable <B> Returns a view on an iterable that has all elements transformed by a given function.static <A> Iterable<A> merge(Comparator<? super A> order, boolean removeDuplicates, Iterable<? extends A> left, Iterable<? extends A> right) Returns the merged combination of two ordered sequences.static <A> Iterable<A> Returns a view on an iterable that contains the same elements, except the first one.static <E> Iterable<E> of(E... things) Returns an iterable view containing exactly the given elements.static <A> Iterable<A> singleton(A item) Returns an iterable that contains exactly one element.static <A> Iterable<A> Returns a view on an iterable that has the same elements, up to a given length.
-
Method Details
-
empty
Returns an empty iterable.- Type Parameters:
A- the element type- Returns:
- an iterable that has no elements
-
map
public static <A,B> Iterable<B> map(Function<? super A, ? extends B> fun, Iterable<? extends A> things) Returns a view on an iterable that has all elements transformed by a given function.Changes to the underlying iterable are reflected in the result.
- Type Parameters:
A- the source element typeB- the target element type- Parameters:
fun- a transformer functionthings- some elements- Returns:
- an iterable where each element is computed on the fly by applying
funto the corresponding element ofthings - Throws:
NullPointerException- iffunorthingsis null
-
singleton
Returns an iterable that contains exactly one element.- Type Parameters:
A- the element type- Parameters:
item- the element- Returns:
- an iterable that contains just the given element
-
of
Returns an iterable view containing exactly the given elements.Changes to the underlying array are reflected in the result.
This method differs from
Arrays.asList(T...)by not disclosing the number of available elements.- Type Parameters:
E- the element type- Parameters:
things- an array of elements- Returns:
- an iterable containing exactly the given elements in order
- See Also:
-
flatten
Returns a view on an iterable of iterables that contains all of their elements.Changes to the underlying iterable(s) are reflected by the resulting view.
- Type Parameters:
A- the type of elements- Parameters:
items- an iterable of iterables of elements- Returns:
- an unmodifiable iterable that contains all elements that are
contained in an iterable contained in
items
-
flatMap
public static <A,B> Iterable<B> flatMap(Function<? super A, ? extends Iterable<? extends B>> fun, Iterable<A> items) Returns a concatenation of subsequences computed with a function.- Type Parameters:
A- the source element typeB- the target element type- Parameters:
fun- a function that computes a subsequence of target elements from a single source elementitems- the source elements- Returns:
- the concatenation of the subsequences obtained by applying
funto all source elements in order
-
cons
Returns a view on an iterable that contains one more element at the start.Changes to the underlying iterable are reflected in the result.
- Type Parameters:
A- the element type- Parameters:
first- the new first elementrest- the other elements- Returns:
- an iterable that, upon iteration, produces the first followed by the other elements
- Throws:
NullPointerException- ifrestis null
-
concat
Returns a view on two iterables that contains both their elements in order.Changes to the underlying iterables are reflected in the result.
- Type Parameters:
A- the element type- Parameters:
first- the first part of elementssecond- the second part of elements- Returns:
- an iterables that contains all elements of
firstfollowed by all elements ofsecond - Throws:
NullPointerException- iffirstorsecondis null
-
filter
Returns a view on an iterable that contains only those elements that match a given predicate.Changes to the underlying iterable are reflected in the result.
- Type Parameters:
A- the element type- Parameters:
pred- a predicate specifying which elements to retainthings- some elements- Returns:
- an iterable that contains all elements of
thingsfor whichpredreturnstrue, in order - Throws:
NullPointerException- ifpredorthingsis null
-
cache
Returns a view on an iterable that stores iterated values in a list.This is useful if the underlying iterable computes elements expensively on the demand.
Behavior is unspecified if the underlying iterable is mutable.
The resulting list call for elements on demand. Iteration over the list requires elements one at a time. Some other list operations, such as calculating the size or comparison with another list, require all elements at once.
- Type Parameters:
A- the element type- Parameters:
things- an iterable (that computes elements on demand)- Returns:
- a list that is populated with elements on demand
-
next
Returns a view on an iterable that contains the same elements, except the first one.Changes to the underlying iterable are reflected in the result.
When
Iterable.iterator()is called on the result and the underlying iterable is empty, aNoSuchElementExceptionis thrown.- Type Parameters:
A- the element type- Parameters:
things- some elements- Returns:
- an iterable that contains all given elements except the first one
-
allZip
public static <A,B> boolean allZip(BiPredicate<? super A, ? super B> rel, Iterable<? extends A> left, Iterable<? extends B> right) Checks whether all corresponding pairs of elements are in a given relation.There must be as many left elements as right elements to relate.
- Type Parameters:
A- the left element typeB- the right element type- Parameters:
rel- the relation on pairs of a left and a right elementleft- the left elementsright- the right elements- Returns:
trueifrelreturnstruefor all pairs of the i-th left and the i-th right element- Throws:
IllegalArgumentException- if there are differently many left and right elements, and the result has not been determined up to that pointNullPointerException- ifrel,leftorrightis null
-
anyZip
public static <A,B> boolean anyZip(BiPredicate<? super A, ? super B> rel, Iterable<? extends A> left, Iterable<? extends B> right) Checks whether any corresponding pairs of elements are in a given relation.There must be as many left elements as right elements to relate.
- Type Parameters:
A- the left element typeB- the right element type- Parameters:
rel- the relation on pairs of a left and a right elementleft- the left elementsright- the right elements- Returns:
trueifrelreturnstruefor at least one pair of the i-th left and the i-th right element- Throws:
IllegalArgumentException- if there are differently many left and right elements, and the result has not been determined up to that pointNullPointerException- ifrel,leftorrightis null
-
merge
public static <A> Iterable<A> merge(Comparator<? super A> order, boolean removeDuplicates, Iterable<? extends A> left, Iterable<? extends A> right) Returns the merged combination of two ordered sequences.Each input sequence is iterated over exactly once.
- Type Parameters:
A- the element type- Parameters:
order- the ordering of both inputs and the resultremoveDuplicates- duplicates are removed iftrue, or retained iffalseleft- the first ordered sequence of elementsright- the second ordered sequence of elements- Returns:
- an ordered sequence that contains the elements of both inputs, possibly without duplicates
- Throws:
NullPointerException- iforder,leftorrightis null
-
filterWithConstraint
public static <A,B> Iterable<A> filterWithConstraint(BiPredicate<? super A, ? super B> rel, Iterable<A> things, Iterable<B> constraints) Returns a view on an iterable that contains only those elements that match with a corresponding constraint.Changes to the underlying iterables are reflected in the result.
- Type Parameters:
A- the element typeB- the constraint type- Parameters:
rel- a binary predicate on elements and constraints specifying which elements to retainthings- some elementsconstraints- some constraints- Returns:
- an iterable that contains, for all i, the i-th
element of
thingsfor whichrelreturnstruewhen paired with the i-th constraint, in order - Throws:
IllegalArgumentException- if there are fewer constraints than elementsNullPointerException- ifpredorthingsis null
-
drop
Returns a view on an iterable that has the same elements, except the first ones.Changes to the underlying iterable are reflected in the result.
If more elements were to be dropped than available, the result is simply empty.
- Type Parameters:
A- the element type- Parameters:
offset- the number of elements to discard from the start of each iterationthings- some elements- Returns:
- an iterable that has the same elements as
things, except foroffsetmany that are discarded at the start of iteration - Throws:
IllegalArgumentException- ifoffsetis negativeNullPointerException- ifthingsis null- See Also:
-
take
Returns a view on an iterable that has the same elements, up to a given length.Changes to the underlying iterable are reflected in the result.
If more elements were to be retained than available, the result is simply equivalent to the input.
- Type Parameters:
A- the element type- Parameters:
length- the number of elements to retain from each iterationthings- some elements- Returns:
- an iterable that has the same first elements as
things, but ends afterlength - Throws:
IllegalArgumentException- iflengthis negativeNullPointerException- ifthingsis null- See Also:
-
dropTake
Combines the effects ofdropandtake.- Type Parameters:
A- the element type- Parameters:
offset- the number of elements to discard from the start of each iterationlength- the number of elements to retain from each iterationthings- some elements- Returns:
- an iterable that has the same elements as
things, except foroffsetmany that are discarded at the start of iteration, but ends afterlength - Throws:
IllegalArgumentException- ifoffsetorlengthis negativeNullPointerException- ifthingsis null- See Also:
-
anyOf
Returns an arbitrary element, if available. If the iterables is meta-stable, then the returned element will be the same until a change of content.- Type Parameters:
E- the element type- Parameters:
things- some elements- Returns:
- an optional value that contains an arbitrarily chosen element if there is one, or the empty optional value if there are no elements
-
firstOf
Returns the first element, if available.- Type Parameters:
E- the element type- Parameters:
things- some elements- Returns:
- an optional value that contains the first (by iteration order) element if there is one, or the empty optional value if there are no elements
-
forEachPair
Performs the given action for all remaining pairs of adjacent elements delivered by the iterator, in order.- Type Parameters:
T- the element type- Parameters:
data- some elementspairwise- the action to perform- See Also:
-
forEachPair
public static <T> void forEachPair(Iterable<T> data, @Opt @Opt Consumer<T> first, BiConsumer<T, T> pairwise, @Opt @Opt Consumer<T> last) Performs the given action for all remaining pairs of adjacent elements delivered by the iterator, as well as for the first and last singleton, in order.- Type Parameters:
T- the element type- Parameters:
data- some elementsfirst- the action to perform for the first singleton, ornullfor no actionpairwise- the action to perform for pairslast- the action to perform for the last singleton, ornullfor no action- See Also:
-
forEachTriple
Performs the given action for all remaining triples of adjacent elements delivered by the iterator, including partial ones, in order.If the data is completely empty, no action is performed. Otherwise the schema for binding the parameters of the code e.g. from a four element list is as follows:
null null #0 null #0 #1 #0 #1 #2 #1 #2 #3 #2 #3 null #3 null null- Type Parameters:
T- the element type- Parameters:
data- some elementsconsumer- the action to perform
-