Iterable
and other
Supplier<Iterator>
compatible iterator sources.
This utility is useful for low-level optimization routines. Note that:
for (var reduced : IuIterable.map(myList, a::reducer)) { // do something with reduced } for (var filtered : IuIterable.filter(myList, a::matches)) { // do something with reduced }
...are equivalent to:
myList.stream().map(a::reducerMethod)...// do something myList.stream().filter(a::isMatching)...// do something
...but with minimal object creation (w/o Stream overhead), and in a form that
may be used directly in a for
loop.
IuIterable factory instances are recommended for
all potentially temporary or synchronized complex constantly
repeatable iteration scenarios, as in the examples above. This
consideration improves reachability, i.e., by unit tests and debug
breakpoints, compared to Iterable.forEach(Consumer)
.
Factory iterables are also preferred over Stream
forms.
In particular, cat(Iterable...)
is useful for union joins of like
iterations.:
for (var item : IuIterable.cat(myCollection, iter(myArray), myMap.values())) { // do something with items }
A common case for constantly repeatable iteration is returning a value from or passing an argument to a method on a business interface.
Factory iterators implement Object.hashCode()
by
returning the number of items already removed from the head of the iterator,
and Object.equals(Object)
for non-destructively comparing the tail
end to that of another factory iterator. These semantics
may be used reliably for short-term set operations between
concurrent iterators.
API Note: Constantly repeatable refers to an immutable
Iterator
source factory backed by a fixed number of data elements
strictly composed of primitive values and constable instances (i.e.,
java.time
, Collection
and Map
with strictly constable
values, etc). Source must be immutable—Iterator.remove()
will not be invoked and should not be implemented. These conditions
are not verifiable, so results are undefined if not met by the application.
All factory Iterable
instances should be considered
disposable and used only in local variable scope. Take care not to pass
factory instances to lambdas, inline, or anonymous classes. Iterable
s
backing these instances need only remain constantly
repeatable while in use (i.e. backing a for
loop). Once out
of scope, the backing sources may change. Therefore, consumers
should hold source references, not factory instances.
- Since:
- 5.4
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Iterable
<T> Concatenates one or more iterables.static <T> Iterable
<T> empty()
static <T> Iterable
<T> Filters anIterable
using predicate.static <T> Iterable
<T> iter
(T... a) Wraps an array.static <T> Iterable
<T> iter
(T[] a, int from) Wraps an array.static <T,
U> Iterable <U> Maps an iterable using a transform function.static <T> Iterable
<T> Creates anIterable
instance from a constantly repeatable supplier.static String
Returns a string representation of anIterable
.static String
Returns a string representation of all remaining elements of anIterator
.static String
Returns a string representation of remaining elements of anIterator
, after skipping a specified number of elements.static boolean
remaindersAreEqual
(Iterator<?> i1, Iterator<?> i2) Steps through two iterators, comparing all remaining items on each until either both are exhausted or items mismatch between the two.static <T> T
static <T> Stream
<T>
-
Method Details
-
of
Creates anIterable
instance from a constantly repeatable supplier. -
empty
- Type Parameters:
T
- item type- Returns:
- empty
Iterable
-
print
Returns a string representation of anIterable
.- Parameters:
iterable
-Iterable
- Returns:
Object.toString()
ifiterable
is aCollection
, otherwise returns.toString(iterable.iterator())
-
print
Returns a string representation of all remaining elements of anIterator
.- Parameters:
iterator
-Iterator
, will be exhausted- Returns:
- string representation
-
print
public static String print(Iterator<?> iterator, int skip) throws NoSuchElementException, IllegalArgumentException Returns a string representation of remaining elements of anIterator
, after skipping a specified number of elements.- Parameters:
iterator
-Iterator
, will be exhaustedskip
- number of steps to skip before recording; may be 0 to skip no steps. Skipped steps are printed as "..."- Returns:
- string representation
- Throws:
NoSuchElementException
- if skip requests skipping elements no present on the source iterable.IllegalArgumentException
- if skip < 0
-
remaindersAreEqual
Steps through two iterators, comparing all remaining items on each until either both are exhausted or items mismatch between the two.This is a destructive operation that renders both arguments unusable.
- Parameters:
i1
-Iterator
, will either be exhausted or left in an unknown state.i2
-Iterator
, will either be exhausted or left in an unknown state.- Returns:
- true if both iterators contained the same number of items, and all
items in both iterators were
Object.equals(Object)
in the order iterated.
-
iter
Wraps an array.- Type Parameters:
T
- item type- Parameters:
a
- array- Returns:
- An iterable over the entire array.
-
iter
Wraps an array.- Type Parameters:
T
- item type- Parameters:
a
- arrayfrom
- starting point- Returns:
- An iterable over the array starting from the point indicated.
-
cat
Concatenates one or more iterables.- Type Parameters:
T
- item type- Parameters:
iterables
- iterables- Returns:
- A single iterable over all iterables in sequence.
-
map
Maps an iterable using a transform function.- Type Parameters:
T
- item typeU
- transformed item type- Parameters:
i
- iterablef
- transform function- Returns:
- An iterable over the results of applying the transform function to the items available from the iterable.
-
select
- Type Parameters:
T
- item type- Parameters:
i
- iterablep
- predicate- Returns:
- The first element that meets the condition.
-
filter
Filters anIterable
using predicate.- Type Parameters:
T
- item type- Parameters:
i
- iterablep
- predicate- Returns:
- An iterable over the items for which the predicate returns true.
-
stream
-