Class Iterators
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
.
- Since:
- 2.0 (imported from Google Collections Library)
- Author:
- Kevin Bourrillion, Jared Levy
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> boolean
addAll
(Collection<T> addTo, Iterator<? extends T> iterator) Adds all elements initerator
tocollection
.static <T> boolean
Returnstrue
if every element returned byiterator
satisfies the given predicate.static boolean
elementsEqual
(Iterator<?> iterator1, Iterator<?> iterator2) Determines whether two iterators contain equal elements in the same order.static <T> UnmodifiableIterator<T>
Deprecated.static <T> UnmodifiableIterator<T>
forArray
(T... array) Returns an iterator containing the elements ofarray
in order.static <T> T
Returns the next element initerator
ordefaultValue
if the iterator is empty.static <T> PeekingIterator<T>
peekingIterator
(Iterator<? extends T> iterator) Returns aPeekingIterator
backed by the given iterator.static boolean
removeAll
(Iterator<?> removeFrom, Collection<?> elementsToRemove) Traverses an iterator and removes every element that belongs to the provided collection.static <T> boolean
Removes every element that satisfies the provided predicate from the iterator.static <T> UnmodifiableIterator<T>
singletonIterator
(T value) Returns an iterator containing onlyvalue
.static int
Returns the number of elements remaining initerator
.static <F,
T> Iterator<T> Returns an iterator that appliesfunction
to each element offromIterator
.static <T> UnmodifiableIterator<T>
unmodifiableIterator
(Iterator<T> iterator) Returns an unmodifiable view ofiterator
.
-
Method Details
-
emptyIterator
Deprecated.UseImmutableSet.<T>of().iterator()
instead; or for Java 7 or later,Collections.emptyIterator()
. This method is scheduled for removal in May 2016.Returns the empty iterator.The
Iterable
equivalent of this method isImmutableSet#of()
. -
unmodifiableIterator
Returns an unmodifiable view ofiterator
. -
size
Returns the number of elements remaining initerator
. The iterator will be left exhausted: itshasNext()
method will returnfalse
. -
removeAll
Traverses an iterator and removes every element that belongs to the provided collection. The iterator will be left exhausted: itshasNext()
method will returnfalse
.- Parameters:
removeFrom
- the iterator to (potentially) remove elements fromelementsToRemove
- the elements to remove- Returns:
true
if any element was removed fromiterator
-
removeIf
Removes every element that satisfies the provided predicate from the iterator. The iterator will be left exhausted: itshasNext()
method will returnfalse
.- Parameters:
removeFrom
- the iterator to (potentially) remove elements frompredicate
- a predicate that determines whether an element should be removed- Returns:
true
if any elements were removed from the iterator- Since:
- 2.0
-
elementsEqual
Determines whether two iterators contain equal elements in the same order. More specifically, this method returnstrue
ifiterator1
anditerator2
contain the same number of elements and every element ofiterator1
is equal to the corresponding element ofiterator2
.Note that this will modify the supplied iterators, since they will have been advanced some number of elements forward.
-
addAll
Adds all elements initerator
tocollection
. The iterator will be left exhausted: itshasNext()
method will returnfalse
.- Returns:
true
ifcollection
was modified as a result of this operation
-
all
Returnstrue
if every element returned byiterator
satisfies the given predicate. Ifiterator
is empty,true
is returned. -
transform
public static <F,T> Iterator<T> transform(Iterator<F> fromIterator, Function<? super F, ? extends T> function) Returns an iterator that appliesfunction
to each element offromIterator
.The returned iterator supports
remove()
if the provided iterator does. After a successfulremove()
call,fromIterator
no longer contains the corresponding element. -
getNext
Returns the next element initerator
ordefaultValue
if the iterator is empty. TheIterables
analog to this method isIterables.getFirst(java.lang.Iterable<? extends T>, T)
.- Parameters:
defaultValue
- the default value to return if the iterator is empty- Returns:
- the next element of
iterator
or the default value - Since:
- 7.0
-
forArray
Returns an iterator containing the elements ofarray
in order. The returned iterator is a view of the array; subsequent changes to the array will be reflected in the iterator.Note: It is often preferable to represent your data using a collection type, for example using
Arrays.asList(Object[])
, making this method unnecessary.The
Iterable
equivalent of this method is eitherArrays.asList(Object[])
,ImmutableList#copyOf(Object[])
}, orImmutableList#of
. -
singletonIterator
Returns an iterator containing onlyvalue
.The
Iterable
equivalent of this method isCollections.singleton(T)
. -
peekingIterator
Returns aPeekingIterator
backed by the given iterator.Calls to the
peek
method with no intervening calls tonext
do not affect the iteration, and hence return the same object each time. A subsequent call tonext
is guaranteed to return the same object again. For example:<p> PeekingIterator<String> peekingIterator = Iterators.peekingIterator(Iterators.forArray("a", "b")); String a1 = peekingIterator.peek(); // returns "a" String a2 = peekingIterator.peek(); // also returns "a" String a3 = peekingIterator.next(); // also returns "a"
Any structural changes to the underlying iteration (aside from those performed by the iterator's own
PeekingIterator.remove()
method) will leave the iterator in an undefined state.The returned iterator does not support removal after peeking, as explained by
PeekingIterator.remove()
.Note: If the given iterator is already a
PeekingIterator
, it might be returned to the caller, although this is neither guaranteed to occur nor required to be consistent. For example, this method might choose to pass through recognized implementations ofPeekingIterator
when the behavior of the implementation is known to meet the contract guaranteed by this method.There is no
Iterable
equivalent to this method, so use this method to wrap each individual iterator as it is generated.- Parameters:
iterator
- the backing iterator. ThePeekingIterator
assumes ownership of this iterator, so users should cease making direct calls to it after calling this method.- Returns:
- a peeking iterator backed by that iterator. Apart from the
additional
PeekingIterator.peek()
method, this iterator behaves exactly the same asiterator
.
-
ImmutableSet.<T>of().iterator()
instead; or for Java 7 or later,Collections.emptyIterator()
.