Top Description Inners Constructors Methods
com.google.common.collect

public final Class Iterators

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkState, .Predicates.instanceOf, com.google.common.collect.CollectPreconditions.checkRemove, .NullnessCasts.uncheckedCastNullableTToT, java.util.Objects.requireNonNull

This class contains static utility methods that operate on or return objects of type Iterator. Except as noted, each method has a corresponding Iterable-based method in the Iterables class.

Performance notes: Unless otherwise noted, all of the iterators produced in this class are lazy, which means that they only advance the backing iteration when absolutely necessary.

See the Guava User Guide section on Iterators.

Authors
Kevin Bourrillion, Jared Levy
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static enum
Iterators.EmptyModifiableIterator

This is an enum singleton rather than an anonymous class so ProGuard can figure out it's only referenced by emptyModifiableIterator().

private static class
Iterators.MergingIterator<T extends @Nullable Object>

An iterator that performs a lazy N-way merge, calculating the next value each time the iterator is polled.

private static class
Iterators.PeekingImpl<E extends @Nullable Object>

Implementation of PeekingIterator that avoids peeking unless necessary.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <T extends @Nullable Object> boolean

Returns:

true if collection was modified as a result of this operation
addAll
(Collection<T> addTo, Iterator<? extends T> iterator)

Adds all elements in iterator to collection.

public static int

Returns:

the number of elements the iterator was advanced
advance
(Iterator<?> iterator, int numberToAdvance)

Calls next() on iterator, either numberToAdvance times or until hasNext() returns false, whichever comes first.

public static <T extends @Nullable Object> boolean
all(Iterator<T> iterator, Predicate<? super T> predicate)

Returns true if every element returned by iterator satisfies the given predicate.

public static <T extends @Nullable Object> boolean
any(Iterator<T> iterator, Predicate<? super T> predicate)

Returns true if one or more elements returned by iterator satisfy the given predicate.

public static <T extends @Nullable Object> Enumeration<T>
asEnumeration(Iterator<T> iterator)

Adapts an Iterator to the Enumeration interface.

pack-priv static <T extends @Nullable Object> ListIterator<T>
cast(Iterator<T> iterator)

Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557

pack-priv static void
checkNonnegative(int position)

pack-priv static void
clear(Iterator<?> iterator)

Clears the iterator using its remove method.

public static <T extends @Nullable Object> Iterator<T>
concat(Iterator<? extends T> a, Iterator<? extends T> b)

Combines two iterators into a single iterator.

public static <T extends @Nullable Object> Iterator<T>
concat(Iterator<? extends T> a, Iterator<? extends T> b, Iterator<? extends T> c)

Combines three iterators into a single iterator.

public static <T extends @Nullable Object> Iterator<T>
concat(Iterator<? extends T> a, Iterator<? extends T> b, Iterator<? extends T> c, Iterator<? extends T> d)

Combines four iterators into a single iterator.

public static <T extends @Nullable Object> Iterator<T>
concat(Iterator<? extends T>... inputs)

Combines multiple iterators into a single iterator.

public static <T extends @Nullable Object> Iterator<T>
concat(Iterator<? extends Iterator<? extends T>> inputs)

Combines multiple iterators into a single iterator.

pack-priv static <T extends @Nullable Object> Iterator<T>
concatNoDefensiveCopy(Iterator<? extends T>... inputs)

Concats a varargs array of iterators without making a defensive copy of the array.

private static <I extends Iterator<?>> Iterator<I>
consumingForArray(@Nullable I... elements)

Returns an Iterator that walks the specified array, nulling out elements behind it.

public static <T extends @Nullable Object> Iterator<T>

Returns:

an iterator that removes and returns elements from the supplied iterator
consumingIterator
(Iterator<T>
the iterator to remove and return elements from
iterator
)

Returns a view of the supplied iterator that removes each element from the supplied iterator as it is returned.

public static boolean
contains(Iterator<?> iterator, Object element)

Returns true if iterator contains element.

public static <T extends @Nullable Object> Iterator<T>
cycle(Iterable<T> iterable)

Returns an iterator that cycles indefinitely over the elements of iterable.

public static <T extends @Nullable Object> Iterator<T>
cycle(T... elements)

Returns an iterator that cycles indefinitely over the provided elements.

public static boolean
elementsEqual(Iterator<?> iterator1, Iterator<?> iterator2)

Determines whether two iterators contain equal elements in the same order.

pack-priv static <T extends @Nullable Object> UnmodifiableIterator<T>
emptyIterator()

Returns the empty iterator.

pack-priv static <T extends @Nullable Object> UnmodifiableListIterator<T>
emptyListIterator()

Returns the empty iterator.

pack-priv static <T extends @Nullable Object> Iterator<T>
emptyModifiableIterator()

Returns the empty Iterator that throws IllegalStateException instead of UnsupportedOperationException on a call to Iterator#remove().

public static <T extends @Nullable Object> UnmodifiableIterator<T>
filter(Iterator<T> unfiltered, Predicate<? super T> retainIfTrue)

Returns a view of unfiltered containing all elements that satisfy the input predicate retainIfTrue.

public static <T> UnmodifiableIterator<T>
filter(Iterator<?> unfiltered, Class<T> desiredType)

Returns a view of unfiltered containing all elements that are of the type desiredType.

public static <T extends @Nullable Object> T
find(Iterator<T> iterator, Predicate<? super T> predicate)

Returns the first element in iterator that satisfies the given predicate; use this method only when such an element is known to exist.

public static <T extends @Nullable Object> T
find(Iterator<? extends T> iterator, Predicate<? super T> predicate, T defaultValue)

Returns the first element in iterator that satisfies the given predicate.

public static <T extends @Nullable Object> UnmodifiableIterator<T>
forArray(T... array)

Returns an iterator containing the elements of array in order.

pack-priv static <T extends @Nullable Object> UnmodifiableListIterator<T>
forArray(T[] array, int offset, int length, int index)

Returns a list iterator containing the elements in the specified range of array in order, starting at the specified index.

public static <T extends @Nullable Object> UnmodifiableIterator<T>
forEnumeration(Enumeration<T> enumeration)

Adapts an Enumeration to the Iterator interface.

public static int
frequency(Iterator<?> iterator, Object element)

Returns the number of elements in the specified iterator that equal the specified object.

public static <T extends @Nullable Object> T

Returns:

the element at the specified position in iterator
get
(Iterator<T> iterator, int
position of the element to return
position
)

Advances iterator position + 1 times, returning the element at the positionth position.

public static <T extends @Nullable Object> T

Returns:

the element at the specified position in iterator or defaultValue if iterator produces fewer than position + 1 elements.
get
(Iterator<? extends T> iterator, int
position of the element to return
position
,
T
the default value to return if the iterator is empty or if position is greater than the number of elements remaining in iterator
defaultValue
)

Advances iterator position + 1 times, returning the element at the positionth position or defaultValue otherwise.

public static <T extends @Nullable Object> T

Returns:

the last element of iterator
getLast
(Iterator<T> iterator)

Advances iterator to the end, returning the last element.

public static <T extends @Nullable Object> T

Returns:

the last element of iterator
getLast
(Iterator<? extends T> iterator, T
the default value to return if the iterator is empty
defaultValue
)

Advances iterator to the end, returning the last element or defaultValue if the iterator is empty.

public static <T extends @Nullable Object> T

Returns:

the next element of iterator or the default value
getNext
(Iterator<? extends T> iterator, T
the default value to return if the iterator is empty
defaultValue
)

Returns the next element in iterator or defaultValue if the iterator is empty.

public static <T extends @Nullable Object> T
getOnlyElement(Iterator<T> iterator)

Returns the single element contained in iterator.

public static <T extends @Nullable Object> T
getOnlyElement(Iterator<? extends T> iterator, T defaultValue)

Returns the single element contained in iterator, or defaultValue if the iterator is empty.

public static <T extends @Nullable Object> int
indexOf(Iterator<T> iterator, Predicate<? super T> predicate)

Returns the index in iterator of the first element that satisfies the provided predicate, or -1 if the Iterator has no such elements.

public static <T extends @Nullable Object> Iterator<T>
limit(Iterator<T>
the iterator to limit
iterator
,
int
the maximum number of elements in the returned iterator
limitSize
)

Returns a view containing the first limitSize elements of iterator.

public static <T extends @Nullable Object> UnmodifiableIterator<T>
mergeSorted(Iterable<? extends Iterator<? extends T>> iterators, Comparator<? super T> comparator)

Returns an iterator over the merged contents of all given iterators, traversing every element of the input iterators.

public static <T extends @Nullable Object> UnmodifiableIterator<List<@Nullable T>>

Returns:

an iterator of immutable lists containing the elements of iterator divided into partitions (the final iterable may have trailing null elements)
paddedPartition
(Iterator<T>
the iterator to return a partitioned view of
iterator
,
int
the desired size of each partition
size
)

Divides an iterator into unmodifiable sublists of the given size, padding the final iterator with null values if necessary.

public static <T extends @Nullable Object> UnmodifiableIterator<List<T>>

Returns:

an iterator of immutable lists containing the elements of iterator divided into partitions
partition
(Iterator<T>
the iterator to return a partitioned view of
iterator
,
int
the desired size of each partition (the last may be smaller)
size
)

Divides an iterator into unmodifiable sublists of the given size (the final list may be smaller).

private static <T extends @Nullable Object> UnmodifiableIterator<List<@Nullable T>>
partitionImpl(Iterator<T> iterator, int size, boolean pad)

public static <T extends @Nullable Object> PeekingIterator<T>

Returns:

a peeking iterator backed by that iterator. Apart from the additional PeekingIterator#peek() method, this iterator behaves exactly the same as iterator.
peekingIterator
(Iterator<? extends T>
the backing iterator. The PeekingIterator assumes ownership of this iterator, so users should cease making direct calls to it after calling this method.
iterator
)

Returns a PeekingIterator backed by the given iterator.

public static <T extends @Nullable Object> PeekingIterator<T>
peekingIterator(PeekingIterator<T> iterator)

Deprecated no need to use this
Simply returns its argument.
pack-priv static <T extends @Nullable Object> T
pollNext(Iterator<T> iterator)

Deletes and returns the next value from the iterator, or returns null if there is no such value.

public static boolean

Returns:

true if any element was removed from iterator
removeAll
(Iterator<?>
the iterator to (potentially) remove elements from
removeFrom
,
Collection<?>
the elements to remove
elementsToRemove
)

Traverses an iterator and removes every element that belongs to the provided collection.

public static <T extends @Nullable Object> boolean

Returns:

true if any elements were removed from the iterator
removeIf
(Iterator<T>
the iterator to (potentially) remove elements from
removeFrom
,
Predicate<? super T>
a predicate that determines whether an element should be removed
predicate
)

Removes every element that satisfies the provided predicate from the iterator.

public static boolean

Returns:

true if any element was removed from iterator
retainAll
(Iterator<?>
the iterator to (potentially) remove elements from
removeFrom
,
Collection<?>
the elements to retain
elementsToRetain
)

Traverses an iterator and removes every element that does not belong to the provided collection.

public static <T extends @Nullable Object> UnmodifiableIterator<T>
singletonIterator(T value)

Returns an iterator containing only value.

public static int
size(Iterator<?> iterator)

Returns the number of elements remaining in iterator.

public static <T extends @Nullable Object> T[]

Returns:

a newly-allocated array into which all the elements of the iterator have been copied
toArray
(Iterator<? extends T>
the iterator to copy
iterator
,
Class<@NonNull T>
the type of the elements
type
)

Copies an iterator's elements into an array.

public static String
toString(Iterator<?> iterator)

Returns a string representation of iterator, with the format [e1, e2, ..., en].

public static <F extends @Nullable Object, T extends @Nullable Object> Iterator<T>
transform(Iterator<F> fromIterator, Function<? super F, ? extends T> function)

Returns a view containing the result of applying function to each element of fromIterator.

public static <T> Optional<T>
tryFind(Iterator<T> iterator, Predicate<? super T> predicate)

Returns an Optional containing the first element in iterator that satisfies the given predicate, if such an element exists.

public static <T extends @Nullable Object> UnmodifiableIterator<T>
unmodifiableIterator(Iterator<? extends T> iterator)

Returns an unmodifiable view of iterator.

public static <T extends @Nullable Object> UnmodifiableIterator<T>
unmodifiableIterator(UnmodifiableIterator<T> iterator)

Deprecated no need to use this
Simply returns its argument.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait