iterators.- Author:
- Sven Efftinge - Initial contribution and API, Sebastian Zarnekow
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanReturns true if this Iterator contains the specified element.static <T> Iterator<T>Returns a view on this iterator that provides all elements except the firstcountentries.static <T> Iterator<T>dropWhile(Iterator<? extends T> iterator, Functions.Function1<? super T, Boolean> predicate) Returns an Iterator containing all elements starting from the first element for which the drop-predicate returned false.static booleanelementsEqual(Iterator<?> iterator, Iterable<?> iterable) Determines whether two iterators contain equal elements in the same order.static booleanelementsEqual(Iterator<?> iterator, Iterator<?> other) Determines whether two iterators contain equal elements in the same order.static <T> booleanexists(Iterator<T> iterator, Functions.Function1<? super T, Boolean> predicate) Returnstrueif one or more elements initeratorsatisfy the predicate.static <T> Iterator<T>Returns all instances of classtypeinunfiltered.static <T> Iterator<T>filter(Iterator<T> unfiltered, Functions.Function1<? super T, Boolean> predicate) Returns the elements ofunfilteredthat satisfy a predicate.static <T> Iterator<T>filterNull(Iterator<T> unfiltered) Returns a new iterator filtering any null references.static <T> TfindFirst(Iterator<T> iterator, Functions.Function1<? super T, Boolean> predicate) Finds the first element in the given iterator that fulfills the predicate.static <T> TfindLast(Iterator<T> iterator, Functions.Function1<? super T, Boolean> predicate) Finds the last element in the given iterator that fulfills the predicate.static <T,R> Iterator<R> flatMap(Iterator<T> original, Functions.Function1<? super T, ? extends Iterator<R>> transformation) Returns an iterable that performs the giventransformationfor each element oforiginalwhen requested.static <T> Iterator<T>Combines multiple iterators into a single iterator.static <T,R> R fold(Iterator<T> iterator, R seed, Functions.Function2<? super R, ? super T, ? extends R> function) Applies the combinatorfunctionto all elements of the iterator in turn and usesseedas the start value.static <T> booleanforall(Iterator<T> iterator, Functions.Function1<? super T, Boolean> predicate) Returnstrueif every element initeratorsatisfies the predicate.static <T> voidforEach(Iterator<T> iterator, Procedures.Procedure1<? super T> procedure) Appliesprocedurefor each element of the given iterator.static <T> voidforEach(Iterator<T> iterator, Procedures.Procedure2<? super T, ? super Integer> procedure) Appliesprocedurefor each element of the given iterator.groupBy(Iterator<? extends V> values, Functions.Function1<? super V, ? extends K> computeKeys) Returns a map for which theMap.values()is a collection of lists, where the elements in the list will appear in the order as they appeared in the iterator.static <T> TReturns the first element in the given iterator ornullif empty.Returns an Iterator of Pairs where the nth pair is created by taking the nth element of the source as the value and its 0-based index as the key.static booleanDetermines if the given iterator contains no elements.static booleanisNullOrEmpty(Iterator<?> iterator) Determines if the given iterator isnullor contains no elements.static StringReturns the concatenated string representation of the elements in the given iterator.static Stringjoin(Iterator<?> iterator, CharSequence separator) Returns the concatenated string representation of the elements in the given iterator.static <T> Stringjoin(Iterator<T> iterator, CharSequence before, CharSequence separator, CharSequence after, Functions.Function1<? super T, ? extends CharSequence> function) Returns the concatenated string representation of the elements in the given iterator.static <T> Stringjoin(Iterator<T> iterator, CharSequence separator, Functions.Function1<? super T, ? extends CharSequence> function) Returns the concatenated string representation of the elements in the given iterator.static <T> TDeprecated.static <T> TlastOrNull(Iterator<T> iterator) Returns the last element in the given iterator ornullif empty.static <T,R> Iterator<R> map(Iterator<T> original, Functions.Function1<? super T, ? extends R> transformation) Returns an iterator that performs the giventransformationfor each element oforiginalwhen requested.static <T extends Comparable<? super T>>
TFinds the maximum of the elements according to their natural ordering.static <T> Tmax(Iterator<T> iterator, Comparator<? super T> comparator) Finds the maximum element according tocomparator.static <T,C extends Comparable<? super C>>
TmaxBy(Iterator<T> iterator, Functions.Function1<? super T, C> compareBy) Finds the element that yields the maximum value when passed tocompareByIf there are several maxima, the first one will be returned.static <T extends Comparable<? super T>>
TFinds the minimum of the given elements according to their natural ordering.static <T> Tmin(Iterator<T> iterator, Comparator<? super T> comparator) Finds the mininmum element according tocomparator.static <T,C extends Comparable<? super C>>
TminBy(Iterator<T> iterator, Functions.Function1<? super T, C> compareBy) Finds the element that yields the minimum value when passed tocompareBy.static <T> Iterator<T>operator_plus(Iterator<? extends T> a, Iterator<? extends T> b) Concatenates two iterators into a single iterator.static <T> Treduce(Iterator<? extends T> iterator, Functions.Function2<? super T, ? super T, ? extends T> function) Applies the combinatorfunctionto all elements of the iterator in turn.static <T> Iterator<T>reject(Iterator<T> unfiltered, Functions.Function1<? super T, Boolean> predicate) Returns the elements ofunfilteredthat do not satisfy a predicate.static intReturns the number of elements initerator.static <T> Iterator<T>Returns a view on this iterator that contains all the elements except the first.static <T> Iterator<T>Returns a view on this iterator that provides at most the firstcountentries.static <T> Iterator<T>takeWhile(Iterator<? extends T> iterator, Functions.Function1<? super T, Boolean> predicate) Returns an Iterator containing all elements starting from the head of the source up to and excluding the first element that violates the predicate.static <K,V> Map<K, V> toInvertedMap(Iterator<? extends K> keys, Functions.Function1<? super K, V> computeValues) Returns a map for which theMap.values()are computed by the given function, and each key is an element in the givenkeys.static <T> Iterable<T>toIterable(Iterator<T> iterator) static <T> List<T>Returns a list that contains all the entries of the given iterator in the same order.static <T,K, V> Map<K, V> toMap(Iterator<? extends T> inputs, Functions.Function1<? super T, K> computeKeys, Functions.Function1<? super T, V> computeValues) Returns a map for which theMap.values()are the product of invoking supplied functioncomputeValueson input iterable elements, and each key is the product of invoking a supplied functioncomputeKeyson same elements.static <K,V> Map<K, V> toMap(Iterator<? extends V> values, Functions.Function1<? super V, K> computeKeys) Returns a map for which theMap.values()are the given elements in the given order, and each key is the product of invoking a supplied functioncomputeKeyson its corresponding value.static <T> Set<T>Returns a set that contains all the unique entries of the given iterator in the order of their appearance.
-
Constructor Details
-
IteratorExtensions
public IteratorExtensions()
-
-
Method Details
-
toIterable
Wraps anIteratorin anIterable. WARNING: The resultingIterablemay be used (e.g. callingIterableExtensions.isEmpty(Iterable)) / iterated (e.g. in a for loop) only once. If you want to create a reusableIterablecallIterableExtensions.toList(Iterable)orIterableExtensions.toSet(Iterable)on the result and reuse the resultingList/Set -
operator_plus
Concatenates two iterators into a single iterator. The returned iterator traverses the elements in
a, followed by the elements inb. The resulting iterator is effectivly a view on the source iterators. That is, the source iterators are not polled until necessary and the result will reflect changes in the sources.The returned iterator supports
remove()when the corresponding input iterator supports it.- Parameters:
a- the first iterator. May not benull.b- the second iterator. May not benull.- Returns:
- a combined iterator. Never
null.
-
findFirst
public static <T> T findFirst(Iterator<T> iterator, Functions.Function1<? super T, Boolean> predicate) Finds the first element in the given iterator that fulfills the predicate. If none is found or the iterator is empty,nullis returned.- Parameters:
iterator- the iterator. May not benull.predicate- the predicate. May not benull.- Returns:
- the first element in the iterator for which the given predicate returns
true, returnsnullif no element matches the predicate or the iterator is empty.
-
findLast
public static <T> T findLast(Iterator<T> iterator, Functions.Function1<? super T, Boolean> predicate) Finds the last element in the given iterator that fulfills the predicate. If none is found or the iterator is empty,nullis returned.- Parameters:
iterator- the iterator. May not benull.predicate- the predicate. May not benull.- Returns:
- the last element in the iterator for which the given predicate returns
true, returnsnullif no element matches the predicate or the iterator is empty.
-
head
Returns the first element in the given iterator ornullif empty.- Parameters:
iterator- the iterator. May not benull.- Returns:
- the first element in the iterator or
null.
-
tail
Returns a view on this iterator that contains all the elements except the first.- Parameters:
iterator- the iterator. May not benull.- Returns:
- an iterator with all elements except the first. Never
null. - See Also:
-
last
Deprecated.Use#lastOrNull(Iterator<T>)instead. To be aligned withIterableExtensions.lastOrNull(Iterable)(see the deprecation reason ofIterableExtensions.last(Iterable)).Returns the last element in the given iterator ornullif empty.- Parameters:
iterator- the iterator. May not benull.- Returns:
- the last element in the iterator or
null.
-
lastOrNull
Returns the last element in the given iterator ornullif empty.- Parameters:
iterator- the iterator. May not benull.- Returns:
- the last element in the iterator or
null. - Since:
- 2.35
-
take
Returns a view on this iterator that provides at most the firstcountentries.- Parameters:
iterator- the iterator. May not benull.count- the number of elements that should be returned at most.- Returns:
- an iterator with
countelements. Nevernull. - Throws:
IllegalArgumentException- ifcountis negative.
-
drop
Returns a view on this iterator that provides all elements except the firstcountentries.- Parameters:
iterator- the iterator. May not benull.count- the number of elements that should be dropped.- Returns:
- an iterator without the first
countelements. Nevernull. - Throws:
IllegalArgumentException- ifcountis negative.
-
exists
public static <T> boolean exists(Iterator<T> iterator, Functions.Function1<? super T, Boolean> predicate) Returnstrueif one or more elements initeratorsatisfy the predicate.Note that this will advance or even exhaust the given iterator.
- Parameters:
iterator- the iterator. May not benull.predicate- the predicate. May not benull.- Returns:
trueif one or more elements initeratorsatisfy the predicate.
-
forall
public static <T> boolean forall(Iterator<T> iterator, Functions.Function1<? super T, Boolean> predicate) Returnstrueif every element initeratorsatisfies the predicate. Ifiteratoris empty,trueis returned. In other words,falseis returned if at least one element fails to fulfill the predicate.- Parameters:
iterator- the iterator. May not benull.predicate- the predicate. May not benull.- Returns:
trueif every element initeratorsatisfies the predicate and also if there is no element.
-
filter
@Pure public static <T> Iterator<T> filter(Iterator<T> unfiltered, Functions.Function1<? super T, Boolean> predicate) Returns the elements ofunfilteredthat satisfy a predicate. The resulting iterator does not supportremove(). The returned iterator is a view on the original elements. Changes in the unfiltered original are reflected in the view.- Parameters:
unfiltered- the unfiltered iterator. May not benull.predicate- the predicate. May not benull.- Returns:
- an iterator that contains only the elements that fulfill the predicate. Never
null.
-
reject
@Pure public static <T> Iterator<T> reject(Iterator<T> unfiltered, Functions.Function1<? super T, Boolean> predicate) Returns the elements ofunfilteredthat do not satisfy a predicate. The resulting iterator does not supportremove(). The returned iterator is a view on the original elements. Changes in the unfiltered original are reflected in the view.- Parameters:
unfiltered- the unfiltered iterator. May not benull.predicate- the predicate. May not benull.- Returns:
- an iterator that contains only the elements that do not fulfill the predicate. Never
null.
-
filter
@Pure @GwtIncompatible("Class.isInstance") public static <T> Iterator<T> filter(Iterator<?> unfiltered, Class<T> type) Returns all instances of classtypeinunfiltered. The returned iterator has elements whose class istypeor a subclass oftype. The returned iterator does not supportremove(). The returned iterator is a view on the original elements. Changes in the unfiltered original are reflected in the view.- Parameters:
unfiltered- the unfiltered iterator. May not benull.type- the type of elements desired- Returns:
- an unmodifiable iterator containing all elements of the original iterator that were of the requested
type. Never
null.
-
filterNull
Returns a new iterator filtering any null references.- Parameters:
unfiltered- the unfiltered iterator. May not benull.- Returns:
- an unmodifiable iterator containing all elements of the original iterator without any
nullreferences. Nevernull.
-
map
@Pure public static <T,R> Iterator<R> map(Iterator<T> original, Functions.Function1<? super T, ? extends R> transformation) Returns an iterator that performs the giventransformationfor each element oforiginalwhen requested. The mapping is done lazily. The returned iterator's iterator supportsremove()if the provided iterator does. After a successfulremove()call,originalno longer contains the corresponding element.- Parameters:
original- the original iterator. May not benull.transformation- the transformation. May not benull.- Returns:
- an iterator that provides the result of the transformation. Never
null.
-
flatMap
@Pure public static <T,R> Iterator<R> flatMap(Iterator<T> original, Functions.Function1<? super T, ? extends Iterator<R>> transformation) Returns an iterable that performs the giventransformationfor each element oforiginalwhen requested. The mapping is done lazily. That is, subsequent iterations of the elements in the iterable will repeatedly apply the transformation.The transformation maps each element to an iterable, and all resulting iterables are combined to a single iterable. Effectively a combination of
map(Iterator, Functions.Function1)andflatten(Iterator)is performed.The returned iterable's iterator does not support
remove()in contrast tomap(Iterator, Functions.Function1).- Parameters:
original- the original iterable. May not benull.transformation- the transformation. May not benulland must not yieldnull.- Returns:
- an iterable that provides the result of the transformation. Never
null. - Since:
- 2.13
-
flatten
Combines multiple iterators into a single iterator. The returned iterator traverses the elements of each iterator ininputs. The input iterators are not polled until necessary.- Parameters:
inputs- the to be flattened iterators. May not benull.- Returns:
- an iterator that provides the concatenated values of the input elements. Never
null. - Since:
- 2.13
-
forEach
Appliesprocedurefor each element of the given iterator.- Parameters:
iterator- the iterator. May not benull.procedure- the procedure. May not benull.
-
forEach
public static <T> void forEach(Iterator<T> iterator, Procedures.Procedure2<? super T, ? super Integer> procedure) Appliesprocedurefor each element of the given iterator. The procedure takes the element and a loop counter. If the counter would overflow,Integer.MAX_VALUEis returned for all subsequent elements. The first element is at index zero.- Parameters:
iterator- the iterator. May not benull.procedure- the procedure. May not benull.
-
join
Returns the concatenated string representation of the elements in the given iterator. No delimiter is used. The given iterator is left exhausted.- Parameters:
iterator- the iterator. May not benull.- Returns:
- the string representation of the iterator's elements. Never
null. - See Also:
-
join
Returns the concatenated string representation of the elements in the given iterator. Theseparatoris used to between each pair of entries in the input. The stringnullis used fornullentries in the input. The given iterator is left exhausted.- Parameters:
iterator- the iterator. May not benull.separator- the separator. May not benull.- Returns:
- the string representation of the iterator's elements. Never
null. - See Also:
-
join
public static <T> String join(Iterator<T> iterator, CharSequence separator, Functions.Function1<? super T, ? extends CharSequence> function) Returns the concatenated string representation of the elements in the given iterator. Thefunctionis used to compute the string for each element. Theseparatoris used to between each pair of entries in the input. The stringnullis used if the function yieldsnullas the string representation for an entry. The given iterator is left exhausted.- Parameters:
iterator- the iterator. May not benull.separator- the separator. May not benull.function- the function that is used to compute the string representation of a single element. May not benull.- Returns:
- the string representation of the iterator's elements. Never
null.
-
join
public static <T> String join(Iterator<T> iterator, CharSequence before, CharSequence separator, CharSequence after, Functions.Function1<? super T, ? extends CharSequence> function) Returns the concatenated string representation of the elements in the given iterator. Thefunctionis used to compute the string for each element. Theseparatoris used to between each pair of entries in the input. The stringnullis used if the function yieldsnullas the string representation for an entry. The given iterator is left exhausted.- Parameters:
iterator- the iterator. May not benull.before- prepends the resulting string if the iterator contains at least one element. May benullwhich is equivalent to passing an empty string.separator- the separator. May benullwhich is equivalent to passing an empty string.after- appended to the resulting string if the iterator contain at least one element. May benullwhich is equivalent to passing an empty string.function- the function that is used to compute the string representation of a single element. May not benull.- Returns:
- the string representation of the iterator's elements. Never
null.
-
elementsEqual
Determines whether two iterators contain equal elements in the same order. More specifically, this method returnstrueifiteratorandothercontain the same number of elements and every element ofiteratoris equal to the corresponding element ofother.Note that this will advance or even exhaust the given iterators.
- Parameters:
iterator- an iterator. May not benull.other- an iterator. May not benull.- Returns:
trueif the two iterators contain equal elements in the same order.
-
elementsEqual
Determines whether two iterators contain equal elements in the same order. More specifically, this method returnstrueifiteratoranditerablecontain the same number of elements and every element ofiteratoris equal to the corresponding element ofiterable.Note that this will advance or even exhaust the given iterators.
- Parameters:
iterator- an iterator. May not benull.iterable- an iterable. May not benull.- Returns:
trueif the two iterators contain equal elements in the same order.
-
isNullOrEmpty
Determines if the given iterator isnullor contains no elements.- Parameters:
iterator- the to-be-queried iterator. May benull.- Returns:
trueif the iterator isnullor contains no elements
-
isEmpty
Determines if the given iterator contains no elements.- Parameters:
iterator- the to-be-queried iterator. May not benull.- Returns:
trueif the iterator contains no elements- See Also:
-
size
Returns the number of elements initerator. The given iterator is left exhausted.- Parameters:
iterator- the iterator. May not benull.- Returns:
- the number of elements in
iterator.
-
reduce
public static <T> T reduce(Iterator<? extends T> iterator, Functions.Function2<? super T, ? super T, ? extends T> function) Applies the combinator
functionto all elements of the iterator in turn.One of the function parameters is an element of the iterator, and the other is the result of previous application of the function. The seed of the operation is the first element in the iterator. The second value is computed by applying the function to the seed together with the second element of the iterator. The third value is computed from the previous result together with the third element and so on. In other words, the previous result of each step is taken and passed together with the next element to the combinator function.
If the iterator is empty,
nullis returned.More formally, given an iterator
[a, b, c, d]and a functionf, the result ofreduceisf(f(f(a, b), c), d)- Parameters:
iterator- the to-be-reduced iterator. May not benull.function- the combinator function. May not benull.- Returns:
- the last result of the applied combinator function or
nullfor the empty input.
-
fold
public static <T,R> R fold(Iterator<T> iterator, R seed, Functions.Function2<? super R, ? super T, ? extends R> function) Applies the combinator
functionto all elements of the iterator in turn and usesseedas the start value.One of the function parameters is an element of the iterator, and the other is the result of previous application of the function. The seed of the operation is explicitly passed to
fold. The first computed value is the result of the applied function forseedand the first element of the iterator. This intermediate result together with the second element of the iterator produced the next result and so on.foldis similar toreducebut allows aseedvalue and the combinatorfunctionmay be asymmetric. It takesT and Rand returnsR.If the iterator is empty,
seedis returned.More formally, given an iterator
[a, b, c, d], a seedinitialand a functionf, the result offoldisf(f(f(f(initial, a), b), c), d)- Parameters:
iterator- the to-be-folded iterator. May not benull.seed- the initial value. May benull.function- the combinator function. May not benull.- Returns:
- the last result of the applied combinator function or
seedfor the empty input.
-
toList
Returns a list that contains all the entries of the given iterator in the same order.- Parameters:
iterator- the iterator. May not benull.- Returns:
- a list with the same entries as the given iterator. Never
null.
-
toSet
Returns a set that contains all the unique entries of the given iterator in the order of their appearance. The result set is a copy of the iterator with stable order.- Parameters:
iterator- the iterator. May not benull.- Returns:
- a set with the unique entries of the given iterator. Never
null.
-
toInvertedMap
public static <K,V> Map<K,V> toInvertedMap(Iterator<? extends K> keys, Functions.Function1<? super K, V> computeValues) Returns a map for which theMap.values()are computed by the given function, and each key is an element in the givenkeys. If the iterator contains equal keys more than once, the last one will be contained in the map. The map is computed eagerly. That is, subsequent changes in the keys are not reflected by the map. The key iterator is left exhausted.- Parameters:
keys- the keys to use when constructing theMap. May not benull.computeValues- the function used to produce the values for each key. May not benull.- Returns:
- a map mapping each entry in the given iterator to the corresponding result when evaluating the function
computeValues.
-
toMap
public static <K,V> Map<K,V> toMap(Iterator<? extends V> values, Functions.Function1<? super V, K> computeKeys) Returns a map for which theMap.values()are the given elements in the given order, and each key is the product of invoking a supplied functioncomputeKeyson its corresponding value. If the function produces the same key for different values, the last one will be contained in the map. The value iterator is left exhausted.- Parameters:
values- the values to use when constructing theMap. May not benull.computeKeys- the function used to produce the key for each value. May not benull.- Returns:
- a map mapping the result of evaluating the function
keyFunctionon each value in the input iterator to that value
-
toMap
public static <T,K, Map<K,V> V> toMap(Iterator<? extends T> inputs, Functions.Function1<? super T, K> computeKeys, Functions.Function1<? super T, V> computeValues) Returns a map for which theMap.values()are the product of invoking supplied functioncomputeValueson input iterable elements, and each key is the product of invoking a supplied functioncomputeKeyson same elements. If the function produces the same key for different values, the last one will be contained in the map. The input iterator is left exhausted.- Parameters:
inputs- the elements to use when constructing theMap. May not benull.computeKeys- the function used to produce the key for each value. May not benull.computeValues- the function used to produce the values for each key. May not benull.- Returns:
- a map mapping the result of evaluating the functions
keyFunctionandcomputeValueson each value in the input iterator to that value
-
groupBy
public static <K,V> Map<K,List<V>> groupBy(Iterator<? extends V> values, Functions.Function1<? super V, ? extends K> computeKeys) Returns a map for which theMap.values()is a collection of lists, where the elements in the list will appear in the order as they appeared in the iterator. Each key is the product of invoking the supplied functioncomputeKeyson its corresponding value. So a key of that map groups a list of values for which the function produced exactly that key. The value iterator is left exhausted.- Parameters:
values- the values to use when constructing theMap. May not benull.computeKeys- the function used to produce the key for each value. May not benull.- Returns:
- a map mapping the result of evaluating the function
keyFunctionon each value in the input iterator to that value. As there can be more than one value mapped by a key, the mapping result is is a list of values. - Since:
- 2.7
-
takeWhile
public static <T> Iterator<T> takeWhile(Iterator<? extends T> iterator, Functions.Function1<? super T, Boolean> predicate) Returns an Iterator containing all elements starting from the head of the source up to and excluding the first element that violates the predicate. The resulting Iterator is a lazily computed view, so any modifications to the underlying Iterators will be reflected on iteration. The result does not supportIterator.remove()- Parameters:
iterator- the elements from which to take. May not benull.predicate- the predicate which decides whether to keep taking elements. May not benull.- Returns:
- the taken elements
- Since:
- 2.7
-
dropWhile
public static <T> Iterator<T> dropWhile(Iterator<? extends T> iterator, Functions.Function1<? super T, Boolean> predicate) Returns an Iterator containing all elements starting from the first element for which the drop-predicate returned false. The resulting Iterator is a lazily computed view, so any modifications to the underlying Iterators will be reflected on iteration. The result does not supportIterator.remove()- Parameters:
iterator- the elements from which to drop. May not benull.predicate- the predicate which decides whether to keep dropping elements. May not benull.- Returns:
- the remaining elements after dropping
- Since:
- 2.7
-
indexed
Returns an Iterator of Pairs where the nth pair is created by taking the nth element of the source as the value and its 0-based index as the key. E.g.zipWitIndex(#["a", "b", "c"]) == #[(0, "a"), (1, "b"), (2, "c")]If the index would overflow,Integer.MAX_VALUEis returned for all subsequent elements. The resulting Iterator is a lazily computed view, so any modifications to the underlying Iterator will be reflected on iteration. The result does not supportIterator.remove()- Parameters:
iterator- the elements. May not benull.- Returns:
- the zipped result
- Since:
- 2.7
-
min
Finds the minimum of the given elements according to their natural ordering. If there are several mimina, the first one will be returned.Note that this will advance or even exhaust the given iterator.
- Parameters:
iterator- the mutually comparable elements. May not benull.- Returns:
- the minimum
- Throws:
NoSuchElementException- if the iterator is empty- Since:
- 2.7
-
minBy
public static <T,C extends Comparable<? super C>> T minBy(Iterator<T> iterator, Functions.Function1<? super T, C> compareBy) Finds the element that yields the minimum value when passed tocompareBy. If there are several maxima, the first one will be returned.Note that this will advance or even exhaust the given iterator.
- Parameters:
iterator- the elements to find the minimum of. May not benull.compareBy- a function that returns a comparable characteristic to compare the elements by. May not benull.- Returns:
- the minimum
- Throws:
NoSuchElementException- if the iterator is empty- Since:
- 2.7
-
min
Finds the mininmum element according tocomparator. If there are several minima, the first one will be returned.Note that this will advance or even exhaust the given iterator.
- Parameters:
iterator- the elements to find the minimum of. May not benull.comparator- the comparison function. May not benull.- Returns:
- the minimum
- Throws:
NoSuchElementException- if the iterator is empty- Since:
- 2.7
-
max
Finds the maximum of the elements according to their natural ordering. If there are several maxima, the first one will be returned.Note that this will advance or even exhaust the given iterator.
- Parameters:
iterator- the mutually comparable elements. May not benull.- Returns:
- the maximum
- Throws:
NoSuchElementException- if the iterator is empty- Since:
- 2.7
-
maxBy
public static <T,C extends Comparable<? super C>> T maxBy(Iterator<T> iterator, Functions.Function1<? super T, C> compareBy) Finds the element that yields the maximum value when passed tocompareByIf there are several maxima, the first one will be returned.Note that this will advance or even exhaust the given iterator.
- Parameters:
iterator- the elements to find the maximum of. May not benull.compareBy- a function that returns a comparable characteristic to compare the elements by. May not benull.- Returns:
- the maximum
- Throws:
NoSuchElementException- if the iterator is empty- Since:
- 2.7
-
max
Finds the maximum element according tocomparator. If there are several maxima, the first one will be returned.Note that this will advance or even exhaust the given iterator.
- Parameters:
iterator- the elements to find the maximum of. May not benull.comparator- the comparison function. May not benull.- Returns:
- the maximum
- Throws:
NoSuchElementException- if the iterator is empty- Since:
- 2.7
-
contains
Returns true if this Iterator contains the specified element. More formally, returns true if and only if this Iterator contains at least one element e such that (o==null ? e==null : o.equals(e)).Note that this will advance or even exhaust the given iterator.
- Parameters:
iterator- the elements to testo- element whose presence in this Iterator is to be tested- Returns:
- true if this Iterator contains the specified element
-
#lastOrNull(Iterator<T>)instead.