public final class Iterators
extends java.lang.Object
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.
| Modifier and Type | Method and Description |
|---|---|
static <T> boolean |
addAll(java.util.Collection<T> addTo,
java.util.Iterator<? extends T> iterator)
Adds all elements in
iterator to collection. |
static boolean |
elementsEqual(java.util.Iterator<?> iterator1,
java.util.Iterator<?> iterator2)
Determines whether two iterators contain equal elements in the same order.
|
static <T> UnmodifiableIterator<T> |
emptyIterator()
Returns the empty iterator.
|
static <T> UnmodifiableIterator<T> |
forArray(T... array)
Returns an iterator containing the elements of
array in order. |
static <T> UnmodifiableIterator<T> |
singletonIterator(T value)
Returns an iterator containing only
value. |
static int |
size(java.util.Iterator<?> iterator)
Returns the number of elements remaining in
iterator. |
static <T> UnmodifiableIterator<T> |
unmodifiableIterator(java.util.Iterator<T> iterator)
Returns an unmodifiable view of
iterator. |
public static <T> UnmodifiableIterator<T> emptyIterator()
public static <T> UnmodifiableIterator<T> unmodifiableIterator(java.util.Iterator<T> iterator)
iterator.public static int size(java.util.Iterator<?> iterator)
iterator. The iterator
will be left exhausted: its hasNext() method will return
false.public static boolean elementsEqual(java.util.Iterator<?> iterator1,
java.util.Iterator<?> iterator2)
true if iterator1
and iterator2 contain the same number of elements and every element
of iterator1 is equal to the corresponding element of
iterator2.
Note that this will modify the supplied iterators, since they will have been advanced some number of elements forward.
public static <T> UnmodifiableIterator<T> forArray(T... array)
array 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 either Arrays.asList(Object[]), ImmutableList.copyOf(Object[])},
or ImmutableList.of().
public static <T> UnmodifiableIterator<T> singletonIterator(T value)
value.
The Iterable equivalent of this method is Collections.singleton(T).
public static <T> boolean addAll(java.util.Collection<T> addTo,
java.util.Iterator<? extends T> iterator)
iterator to collection. The iterator
will be left exhausted: its hasNext() method will return
false.true if collection was modified as a result of this
operation