Package org.moeaframework.util
Class Iterators
java.lang.Object
org.moeaframework.util.Iterators
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Associates an index to a value. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
Counts the number of elements in the iterable.static int
Counts the number of elements in the iterator.static <T> Iterable<Iterators.IndexedValue<T>>
Returns an iterable that tracks the index of each item.static <T> Iterator<Iterators.IndexedValue<T>>
Returns an iterator that tracks the index of each item.static <T> Iterable<Iterators.IndexedValue<T>>
enumerate
(T[] array) Returns an iterable that tracks the index of each item.static <T> Iterable<T>
Returns an iterable that joins the contents of multiple iterables.static <T> Iterator<T>
Returns an iterator that joins the contents of multiple iterators.static <T> T
Returns the last element in the iterable.static <T> T
Returns the last element in the iterator.static <T,
R> Iterable<R> Returns an iterable mapping the function to the items in the iterable.static <T,
R> Iterator<R> Returns an iterator mapping the function to the items in the iterator.static <T,
R> Iterable<R> Returns an iterable mapping the function to the items in the array.static <T> List<T>
materialize
(Iterable<T> iterable) Materializes the given iterable, returning a collection of all the items.static <T> List<T>
materialize
(Iterator<T> iterator) Materializes the given iterator, returning a collection of all the items.static <V extends Comparable<? super V>>
VFinds and returns the maximum element using natural ordering as defined by theComparable
.static <V extends Comparable<? super V>>
VFinds and returns the maximum element using natural ordering as defined by theComparable
.static <V extends Comparable<? super V>>
VFinds and returns the minimum element using natural ordering as defined by theComparable
.static <K extends Comparable<? super K>,
V>
org.apache.commons.lang3.tuple.Pair<K,V> Finds and returns the minimum element based on the given key with a natural ordering.static <V extends Comparable<? super V>>
VFinds and returns the minimum element using natural ordering as defined by theComparable
.static <K extends Comparable<? super K>,
V>
org.apache.commons.lang3.tuple.Pair<K,V> Finds and returns the minimum element based on the given key with a natural ordering.static <K,
V> org.apache.commons.lang3.tuple.Pair<K, V> minimum
(Iterator<V> iterator, Function<? super V, ? extends K> keyExtractor, Comparator<? super K> keyComparator) Finds and returns the minimum element based on the given key with the ordering defined by a comparator function.static <T> Iterator<T>
of
(T... values) Returns an immutable iterator containing the given values.static <K,
T> Iterable<org.apache.commons.lang3.tuple.Pair<K, T>> Returns an iterable that returns pairs of items from two iterables.static <K,
T> Iterator<org.apache.commons.lang3.tuple.Pair<K, T>> Returns an iterator that returns pairs of items from two iterators.static <K,
T> Iterable<org.apache.commons.lang3.tuple.Pair<K, T>> zip
(K[] array1, T[] array2) Returns an iterable that returns pairs of items from two arrays.
-
Method Details
-
of
Returns an immutable iterator containing the given values.- Type Parameters:
T
- the type of each value- Parameters:
values
- the values- Returns:
- the iterator over the values
-
join
Returns an iterator that joins the contents of multiple iterators.- Type Parameters:
T
- the type of each item- Parameters:
iterators
- the iterators- Returns:
- the joined iterator
-
join
Returns an iterable that joins the contents of multiple iterables.- Type Parameters:
T
- the type of each item- Parameters:
iterables
- the iterables- Returns:
- the joined iterable
-
enumerate
Returns an iterator that tracks the index of each item.- Type Parameters:
T
- the type of each item- Parameters:
iterator
- the iterator- Returns:
- the indexed value
-
enumerate
Returns an iterable that tracks the index of each item.- Type Parameters:
T
- the type of each item- Parameters:
iterable
- the iterable- Returns:
- the indexed iterable
-
enumerate
Returns an iterable that tracks the index of each item.- Type Parameters:
T
- the type of each item- Parameters:
array
- the array of items- Returns:
- the indexed iterable
-
zip
public static <K,T> Iterator<org.apache.commons.lang3.tuple.Pair<K,T>> zip(Iterator<K> iterator1, Iterator<T> iterator2) Returns an iterator that returns pairs of items from two iterators.- Type Parameters:
K
- the type of the first iteratorT
- the type of the second iterator- Parameters:
iterator1
- the first iteratoriterator2
- the second iterator- Returns:
- an iterator over pairs
-
zip
public static <K,T> Iterable<org.apache.commons.lang3.tuple.Pair<K,T>> zip(Iterable<K> iterable1, Iterable<T> iterable2) Returns an iterable that returns pairs of items from two iterables.- Type Parameters:
K
- the type of the first iterableT
- the type of the second iterable- Parameters:
iterable1
- the first iterableiterable2
- the second iterable- Returns:
- an iterable over pairs
-
zip
Returns an iterable that returns pairs of items from two arrays.- Type Parameters:
K
- the type of the first arrayT
- the type of the second array- Parameters:
array1
- the first arrayarray2
- the second array- Returns:
- an iterable over pairs
-
map
Returns an iterator mapping the function to the items in the iterator.- Type Parameters:
T
- the source typeR
- the result type- Parameters:
iterator
- the iteratorfunction
- the mapping function- Returns:
- the result iterator
-
map
Returns an iterable mapping the function to the items in the iterable.- Type Parameters:
T
- the source typeR
- the result type- Parameters:
iterable
- the iterablefunction
- the mapping function- Returns:
- the result iterable
-
map
Returns an iterable mapping the function to the items in the array.- Type Parameters:
T
- the source typeR
- the result type- Parameters:
array
- the arrayfunction
- the mapping function- Returns:
- the result iterable
-
materialize
Materializes the given iterator, returning a collection of all the items.- Type Parameters:
T
- the type of the iterator- Parameters:
iterator
- the iterator- Returns:
- the collection of items
-
materialize
Materializes the given iterable, returning a collection of all the items.- Type Parameters:
T
- the type of the iterable- Parameters:
iterable
- the iterable- Returns:
- the collection of items
-
last
Returns the last element in the iterator.- Type Parameters:
T
- the type of the iterator- Parameters:
iterator
- the iterator- Returns:
- the last element, or
null
if the iterator was empty
-
last
Returns the last element in the iterable.- Type Parameters:
T
- the type of the iterable- Parameters:
iterable
- the iterable- Returns:
- the last element, or
null
if the iterable was empty
-
count
Counts the number of elements in the iterator.- Parameters:
iterator
- the iterator- Returns:
- the number of elements
-
count
Counts the number of elements in the iterable.- Parameters:
iterable
- the iterable- Returns:
- the number of elements
-
minimum
Finds and returns the minimum element using natural ordering as defined by theComparable
.- Type Parameters:
V
- the comparable type- Parameters:
iterator
- the iterator- Returns:
- the minimum element
- Throws:
NoSuchElementException
- if the iterator is empty
-
maximum
Finds and returns the maximum element using natural ordering as defined by theComparable
.- Type Parameters:
V
- the comparable type- Parameters:
iterator
- the iterator- Returns:
- the maximum element
- Throws:
NoSuchElementException
- if the iterator is empty
-
minimum
Finds and returns the minimum element using natural ordering as defined by theComparable
.- Type Parameters:
V
- the comparable type- Parameters:
iterable
- the iterable- Returns:
- the minimum element
- Throws:
NoSuchElementException
- if the iterator is empty
-
maximum
Finds and returns the maximum element using natural ordering as defined by theComparable
.- Type Parameters:
V
- the comparable type- Parameters:
iterable
- the iterable- Returns:
- the maximum element
- Throws:
NoSuchElementException
- if the iterator is empty
-
minimum
public static <K extends Comparable<? super K>,V> org.apache.commons.lang3.tuple.Pair<K,V> minimum(Iterator<V> iterator, Function<? super V, ? extends K> keyExtractor) Finds and returns the minimum element based on the given key with a natural ordering.- Type Parameters:
K
- the type of the key used for comparisonV
- the type of each element- Parameters:
iterator
- the iteratorkeyExtractor
- function returning the key used for comparisons- Returns:
- the minimum key and element
- Throws:
NoSuchElementException
- if the iterator is empty
-
minimum
public static <K extends Comparable<? super K>,V> org.apache.commons.lang3.tuple.Pair<K,V> minimum(Iterable<V> iterable, Function<? super V, ? extends K> keyExtractor) Finds and returns the minimum element based on the given key with a natural ordering.- Type Parameters:
K
- the type of the key used for comparisonV
- the type of each element- Parameters:
iterable
- the iterablekeyExtractor
- function returning the key used for comparisons- Returns:
- the minimum key and element
- Throws:
NoSuchElementException
- if the iterable is empty
-
minimum
public static <K,V> org.apache.commons.lang3.tuple.Pair<K,V> minimum(Iterator<V> iterator, Function<? super V, ? extends K> keyExtractor, Comparator<? super K> keyComparator) Finds and returns the minimum element based on the given key with the ordering defined by a comparator function.- Type Parameters:
K
- the type of the key used for comparisonV
- the type of each element- Parameters:
iterator
- the iteratorkeyExtractor
- function returning the key used for comparisonskeyComparator
- function for ordering the keys- Returns:
- the minimum key and element
- Throws:
NoSuchElementException
- if the iterator is empty
-