|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.Iterators
public final class Iterators
This class contains static utility methods that operate on or return objects
of type Iterator. Also see the parallel implementations in Iterables.
| Method Summary | ||
|---|---|---|
static
|
addAll(java.util.Collection<T> collection,
java.util.Iterator<? extends T> iterator)
Adds all elements in iterator to collection. |
|
static
|
all(java.util.Iterator<T> iterator,
Predicate<? super T> predicate)
Returns true if every element returned by iterator
satisfies the given predicate. |
|
static
|
any(java.util.Iterator<T> iterator,
Predicate<? super T> predicate)
Returns true if one or more elements returned by iterator
satisfy the given predicate. |
|
static
|
asEnumeration(java.util.Iterator<T> iterator)
Adapts an Iterator to the Enumeration interface. |
|
static
|
concat(java.util.Iterator<? extends java.util.Iterator<? extends T>> iterators)
Combines multiple iterators into a single iterator. |
|
static
|
concat(java.util.Iterator<? extends T>... iterators)
Combines multiple iterators into a single iterator. |
|
static
|
concat(java.util.Iterator<? extends T> a,
java.util.Iterator<? extends T> b)
Combines two iterators into a single iterator. |
|
static
|
cycle(java.lang.Iterable<T> iterable)
Returns an iterator that cycles indefinitely over the elements of iterable. |
|
static
|
cycle(T... elements)
Returns an iterator that cycles indefinitely over the provided elements. |
|
static boolean |
elementsEqual(java.util.Iterator<?> iterator1,
java.util.Iterator<?> iterator2)
Determines whether two iterators contain equal elements. |
|
static
|
emptyIterator()
Returns the empty Iterator. |
|
static
|
emptyListIterator()
Returns the empty ListIterator. |
|
static
|
filter(java.util.Iterator<?> unfiltered,
java.lang.Class<T> type)
Returns all instances of class type in unfiltered. |
|
static
|
filter(java.util.Iterator<T> unfiltered,
Predicate<? super T> predicate)
Returns the elements of unfiltered that satisfy a predicate. |
|
static
|
find(java.util.Iterator<E> iterator,
Predicate<? super E> predicate)
Returns the first element in iterator that satisfies the given
predicate. |
|
static
|
forEnumeration(java.util.Enumeration<T> enumeration)
Adapts an Enumeration to the Iterator interface. |
|
static int |
frequency(java.util.Iterator<?> iterator,
java.lang.Object element)
Returns the number of elements in the specified iterator equal to the specified object. |
|
static
|
getLast(java.util.Iterator<T> iterator)
Advances iterator to the end, returning the last element. |
|
static
|
getOnlyElement(java.util.Iterator<T> iterator)
Returns the single element contained in iterator. |
|
static
|
getOnlyElement(java.util.Iterator<T> iterator,
T defaultValue)
Returns the single element contained in iterator, or defaultValue if the iterator is empty. |
|
static
|
limit(java.util.Iterator<T> iterator,
int limit)
Creates an iterator returning the first limit elements of the given
iterator. |
|
static
|
newArray(java.util.Iterator<T> iterator,
java.lang.Class<T> type)
Converts an iterator into an array. |
|
static
|
partition(java.util.Iterator<? extends T> iterator,
int partitionSize,
boolean padToSize)
Partition an iterator into sub-iterators of the given size like so: {A, B, C, D, E, F} with partition size 3 => {A, B, C} and {D, E, F}. |
|
static
|
peekingIterator(java.util.Iterator<T> iterator)
Wraps the supplied iterator in a PeekingIterator. |
|
static int |
size(java.util.Iterator<?> iterator)
Returns the number of elements remaining in iterator. |
|
static
|
skip(java.util.Iterator<T> iterator,
int numberToSkip)
Calls next() on iterator, either numberToSkip times
or until hasNext() returns false, whichever comes first. |
|
static java.lang.String |
toString(java.util.Iterator<?> iterator)
Returns a string representation of the elements in iterator, with
the format "[e1, e2, ..., en]". |
|
static
|
transform(java.util.Iterator<F> fromIterator,
Function<? super F,? extends T> function)
Returns an iterator that applies function to each element of fromIterator. |
|
static
|
unmodifiableIterator(java.util.Iterator<T> iterator)
Returns an unmodifiable view of iterator. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <T> java.util.Iterator<T> emptyIterator()
Iterator.
public static <T> java.util.ListIterator<T> emptyListIterator()
ListIterator.
public static <T> java.util.Iterator<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 java.lang.String toString(java.util.Iterator<?> iterator)
iterator, with
the format "[e1, e2, ..., en]". The iterator will be left
exhausted: its hasNext() method will return false.
public static <T> T getOnlyElement(java.util.Iterator<T> iterator)
iterator.
java.util.NoSuchElementException - if the iterator is empty
java.lang.IllegalArgumentException - if the iterator contains multiple
elements
public static <T> T getOnlyElement(java.util.Iterator<T> iterator,
@Nullable
T defaultValue)
iterator, or defaultValue if the iterator is empty.
java.lang.IllegalArgumentException - if the iterator contains multiple
elements
public static <T> T[] newArray(java.util.Iterator<T> iterator,
java.lang.Class<T> type)
hasNext() method will return false.
iterator - the iterator to converttype - the type of the elements
public static <T> boolean addAll(java.util.Collection<T> collection,
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
public static int frequency(java.util.Iterator<?> iterator,
@Nullable
java.lang.Object element)
hasNext() method will return false.
Collections.frequency(java.util.Collection>, java.lang.Object)public static <T> java.util.Iterator<T> cycle(java.lang.Iterable<T> iterable)
iterable.
The returned iterator supports remove() if the provided iterator
does. After remove() is called, subsequent cycles omit the removed
element, which is no longer in iterable. The iterator's
hasNext() method returns true until iterable is
empty.
Warning: Typical uses of the resulting iterator may produce an
infinite loop. You should use an explicit break or be certain that
you will eventually remove all the elements.
public static <T> java.util.Iterator<T> cycle(T... elements)
The returned iterator supports remove() if the provided iterator
does. After calling remove(), subsequent cycles omit the removed
element, but elements does not change. The iterator's
hasNext() method returns true until all of the original
elements have been removed.
Warning: Typical uses of the resulting iterator may produce an
infinite loop. You should use an explicit break or be certain that
you will eventually remove all the elements.
public static <T> java.util.Iterator<T> concat(java.util.Iterator<? extends T> a,
java.util.Iterator<? extends T> b)
a, followed by the elements in
b. The source iterators are not polled until necessary.
The returned iterator supports remove() when the appropriate
source iterator supports it.
public static <T> java.util.Iterator<T> concat(java.util.Iterator<? extends T>... iterators)
The returned iterator supports remove() when the appropriate
source iterator supports it.
java.lang.NullPointerException - if any of the provided iterators is nullpublic static <T> java.util.Iterator<T> concat(java.util.Iterator<? extends java.util.Iterator<? extends T>> iterators)
The returned iterator supports Iterator.remove() when the
appropriate source iterator supports it. The methods of the returned
iterator may throw NullPointerException if any of the source
iterators are null.
public static <T> java.util.Iterator<java.util.Iterator<T>> partition(java.util.Iterator<? extends T> iterator,
int partitionSize,
boolean padToSize)
remove().
After you call next() on the returned iterator, the iterators
from prior next() calls become invalid.
iterator - the iterator to partitionpartitionSize - the size of each partitionpadToSize - whether to pad the last partition to the partition size
with null
public static <T> java.util.Iterator<T> filter(java.util.Iterator<T> unfiltered,
Predicate<? super T> predicate)
unfiltered that satisfy a predicate. It may
return an empty iterator, but never null. The resulting iterator
does not support remove().
public static <T> java.util.Iterator<T> filter(java.util.Iterator<?> unfiltered,
java.lang.Class<T> type)
type in unfiltered. The
returned iterator has elements whose class is type or a subclass of
type. The returned iterator does not support remove().
unfiltered - an iterator containing objects of any typetype - the type of elements desired
public static <T> boolean any(java.util.Iterator<T> iterator,
Predicate<? super T> predicate)
true if one or more elements returned by iterator
satisfy the given predicate.
public static <T> boolean all(java.util.Iterator<T> iterator,
Predicate<? super T> predicate)
true if every element returned by iterator
satisfies the given predicate. If iterator is empty, true
is returned.
public static <E> E find(java.util.Iterator<E> iterator,
Predicate<? super E> predicate)
iterator that satisfies the given
predicate. If a matching element is found, the iterator will be left in a
state such that calling iterator.remove() will remove the found
item. If no such element is found, the iterator will be left exhausted: its
hasNext() method will return false.
iterator
java.util.NoSuchElementException - if no element in iterator matches
the given predicate
public static <F,T> java.util.Iterator<T> transform(java.util.Iterator<F> fromIterator,
Function<? super F,? extends T> function)
function to each element of fromIterator.
The returned iterator supports remove() if the provided iterator
does. After a successful remove() call, fromIterator no
longer contains the corresponding element.
public static <T> T getLast(java.util.Iterator<T> iterator)
iterator to the end, returning the last element.
iterator
java.util.NoSuchElementException - if the iterator has no remaining elements
public static <T> int skip(java.util.Iterator<T> iterator,
int numberToSkip)
next() on iterator, either numberToSkip times
or until hasNext() returns false, whichever comes first.
public static <T> java.util.Iterator<T> forEnumeration(java.util.Enumeration<T> enumeration)
Enumeration to the Iterator interface. The
returned iterator does not support remove().
public static <T> java.util.Enumeration<T> asEnumeration(java.util.Iterator<T> iterator)
Iterator to the Enumeration interface.
Collections.enumeration(Collection)
public static <T> java.util.Iterator<T> limit(java.util.Iterator<T> iterator,
int limit)
limit elements of the given
iterator. If the original iterator does not contain that many elements, the
returned iterator will have the same behavior as the original iterator. The
returned iterator supports remove() if the original iterator
supports it.
iterator - the iterator to limitlimit - the maximum number of elements in the returned iterator
java.lang.IllegalArgumentException - if limit is negativepublic static <T> PeekingIterator<T> peekingIterator(java.util.Iterator<T> iterator)
PeekingIterator. The
PeekingIterator assumes ownership of the supplied iterator, so
users should cease making direct calls to it after calling this method.
If the PeekingIterator.peek() method of the constructed
PeekingIterator is never called, the returned iterator will
behave exactly the same as the supplied iterator.
Subsequent calls to peek() with no intervening calls to
next() do not affect the iteration, and hence return the same
object each time. After a call to peek(), the next call to
next() is guaranteed to return the same object that the
peek() call returned. For example:
PeekingIterator<E> peekingIterator = ...; // Either the next three calls will each throw // NoSuchElementExceptions, or... E e1 = peekingIterator.peek(); E e2 = peekingIterator.peek(); // e2 is the same as e1 E e3 = peekingIterator.next(); // e3 is the same as e1/e2
Calling Iterator.remove() after PeekingIterator.peek()
is unsupported by the returned iterator and will throw an
IllegalStateException.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||