Top Description Fields Constructors Methods
org.apache.commons.collections4

public Class IteratorUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Array, .InvocationTargetException, .Method, java.util.ArrayList, .Collection, .Comparator, .Dictionary, .Enumeration, .Iterator, .List, .ListIterator, .Map, org.apache.commons.collections4.functors.EqualPredicate, org.apache.commons.collections4.iterators.ArrayIterator, .ArrayListIterator, .BoundedIterator, .CollatingIterator, .EmptyIterator, .EmptyListIterator, .EmptyMapIterator, .EmptyOrderedIterator, .EmptyOrderedMapIterator, .EnumerationIterator, .FilterIterator, .FilterListIterator, .IteratorChain, .IteratorEnumeration, .IteratorIterable, .ListIteratorWrapper, .LoopingIterator, .LoopingListIterator, .NodeListIterator, .ObjectArrayIterator, .ObjectArrayListIterator, .ObjectGraphIterator, .PeekingIterator, .PushbackIterator, .SingletonIterator, .SingletonListIterator, .SkippingIterator, .TransformIterator, .UnmodifiableIterator, .UnmodifiableListIterator, .UnmodifiableMapIterator, .ZippingIterator, org.w3c.dom.Node, .NodeList

Provides static utility methods and decorators for Iterator instances. The implementations are provided in the iterators subpackage.
Since
2.1

Field Summary

Modifier and TypeField and Description
private static final String
DEFAULT_TOSTRING_DELIMITER

Default delimiter used to delimit elements while converting an Iterator to its String representation.

private static final String
DEFAULT_TOSTRING_PREFIX

Default prefix used while converting an Iterator to its String representation.

private static final String
DEFAULT_TOSTRING_SUFFIX

Default suffix used while converting an Iterator to its String representation.

public static final ResettableIterator<E>
EMPTY_ITERATOR

An iterator over no elements.

public static final ResettableListIterator<E>
EMPTY_LIST_ITERATOR

A list iterator over no elements.

public static final MapIterator<K, V>
EMPTY_MAP_ITERATOR

A map iterator over no elements.

public static final OrderedIterator<E>
EMPTY_ORDERED_ITERATOR

An ordered iterator over no elements.

public static final OrderedMapIterator<K, V>
EMPTY_ORDERED_MAP_ITERATOR

An ordered map iterator over no elements.

Constructor Summary

AccessConstructor and Description
private
IteratorUtils()

IteratorUtils is not normally instantiated.

Method Summary

Modifier and TypeMethod and Description
public static <
the element type
E
>
ResettableIterator<E>

Returns:

an iterator over the array
arrayIterator
(final E...
the array over which to iterate
array
)

Gets an iterator over an object array.

public static <
the element type
E
>
ResettableIterator<E>

Returns:

an iterator over the array
arrayIterator
(final Object
the array over which to iterate
array
)

Gets an iterator over an object or primitive array.

public static <
the element type
E
>
ResettableIterator<E>

Returns:

an iterator over part of the array
arrayIterator
(final E[]
the array over which to iterate
array
,
final int
the index to start iterating at
start
)

Gets an iterator over the end part of an object array.

public static <
the element type
E
>
ResettableIterator<E>

Returns:

an iterator over part of the array
arrayIterator
(final Object
the array over which to iterate
array
,
final int
the index to start iterating at
start
)

Gets an iterator over the end part of an object or primitive array.

public static <
the element type
E
>
ResettableIterator<E>

Returns:

an iterator over part of the array
arrayIterator
(final E[]
the array over which to iterate
array
,
final int
the index to start iterating at
start
,
final int
the index to finish iterating at
end
)

Gets an iterator over part of an object array.

public static <
the element type
E
>
ResettableIterator<E>

Returns:

an iterator over part of the array
arrayIterator
(final Object
the array over which to iterate
array
,
final int
the index to start iterating at
start
,
final int
the index to finish iterating at
end
)

Gets an iterator over part of an object or primitive array.

public static <
the element type
E
>
ResettableListIterator<E>

Returns:

a list iterator over the array
arrayListIterator
(final E...
the array over which to iterate
array
)

Gets a list iterator over an object array.

public static <
the element type
E
>
ResettableListIterator<E>

Returns:

a list iterator over the array
arrayListIterator
(final Object
the array over which to iterate
array
)

Gets a list iterator over an object or primitive array.

public static <
the element type
E
>
ResettableListIterator<E>

Returns:

a list iterator over part of the array
arrayListIterator
(final E[]
the array over which to iterate
array
,
final int
the index to start iterating at
start
)

Gets a list iterator over the end part of an object array.

public static <
the element type
E
>
ResettableListIterator<E>

Returns:

a list iterator over part of the array
arrayListIterator
(final Object
the array over which to iterate
array
,
final int
the index to start iterating at
start
)

Gets a list iterator over the end part of an object or primitive array.

public static <
the element type
E
>
ResettableListIterator<E>

Returns:

a list iterator over part of the array
arrayListIterator
(final E[]
the array over which to iterate
array
,
final int
the index to start iterating at
start
,
final int
the index to finish iterating at
end
)

Gets a list iterator over part of an object array.

public static <
the element type
E
>
ResettableListIterator<E>

Returns:

a list iterator over part of the array
arrayListIterator
(final Object
the array over which to iterate
array
,
final int
the index to start iterating at
start
,
final int
the index to finish iterating at
end
)

Gets a list iterator over part of an object or primitive array.

public static <
the element type
E
>
Enumeration<E>

Returns:

a new enumeration
asEnumeration
(final Iterator<? extends E>
the iterator to use, may not be null
iterator
)

Gets an enumeration that wraps an iterator.

public static <
the element type
E
>
Iterable<E>

Returns:

a new, single use Iterable
asIterable
(final Iterator<? extends E>
the iterator to use, may not be null
iterator
)

Gets an Iterable that wraps an iterator.

public static <
the element type
E
>
Iterator<E>

Returns:

a new iterator
asIterator
(final Enumeration<? extends E>
the enumeration to use, may not be null
enumeration
)

Gets an iterator that provides an iterator view of the given enumeration.

public static <
the element type
E
>
Iterator<E>

Returns:

a new iterator
asIterator
(final Enumeration<? extends E>
the enumeration to use, may not be null
enumeration
,
final Collection<? super E>
the collection to remove elements from, may not be null
removeCollection
)

Gets an iterator that provides an iterator view of the given enumeration that will remove elements from the specified collection.

public static <
the element type
E
>
Iterable<E>

Returns:

a new, multiple use iterable
asMultipleUseIterable
(final Iterator<? extends E>
the iterator to use, may not be null
iterator
)

Gets an iterable that wraps an iterator.

public static <
the element type
E
>
BoundedIterator<E>

Returns:

a new bounded iterator
boundedIterator
(final Iterator<? extends E>
the iterator to decorate
iterator
,
final long
the maximum number of elements returned by this iterator
max
)

Decorates the specified iterator to return at most the given number of elements.

public static <
the element type
E
>
BoundedIterator<E>

Returns:

a new bounded iterator
boundedIterator
(final Iterator<? extends E>
the iterator to decorate
iterator
,
final long
the index of the first element of the decorated iterator to return
offset
,
final long
the maximum number of elements returned by this iterator
max
)

Decorates the specified iterator to return at most the given number of elements, skipping all elements until the iterator reaches the position at offset.

public static <
the element type
E
>
Iterator<E>

Returns:

a combination iterator over the iterators
chainedIterator
(final Iterator<? extends E>
the first iterator to use, not null
iterator1
,
final Iterator<? extends E>
the second iterator to use, not null
iterator2
)

Gets an iterator that iterates through two Iterators one after another.

public static <
the element type
E
>
Iterator<E>

Returns:

a combination iterator over the iterators
chainedIterator
(final Iterator<? extends E>...
the iterators to use, not null or empty or contain nulls
iterators
)

Gets an iterator that iterates through an array of Iterators one after another.

public static <
the element type
E
>
Iterator<E>

Returns:

a combination iterator over the iterators
chainedIterator
(final Collection<Iterator<? extends E>>
the iterators to use, not null or empty or contain nulls
iterators
)

Gets an iterator that iterates through a collections of Iterators one after another.

public static <
the element type
E
>
Iterator<E>

Returns:

a combination iterator over the iterators
collatedIterator
(final Comparator<? super E>
the comparator to use, may be null for natural order
comparator
,
final Iterator<? extends E>
the first iterators to use, not null
iterator1
,
final Iterator<? extends E>
the first iterators to use, not null
iterator2
)

Gets an iterator that provides an ordered iteration over the elements contained in a collection of ordered Iterators.

public static <
the element type
E
>
Iterator<E>

Returns:

a combination iterator over the iterators
collatedIterator
(final Comparator<? super E>
the comparator to use, may be null for natural order
comparator
,
final Iterator<? extends E>...
the iterators to use, not null or empty or contain nulls
iterators
)

Gets an iterator that provides an ordered iteration over the elements contained in an array of Iterators.

public static <
the element type
E
>
Iterator<E>

Returns:

a combination iterator over the iterators
collatedIterator
(final Comparator<? super E>
the comparator to use, may be null for natural order
comparator
,
final Collection<Iterator<? extends E>>
the iterators to use, not null or empty or contain nulls
iterators
)

Gets an iterator that provides an ordered iteration over the elements contained in a collection of Iterators.

public static <
the type of object the Iterator contains
E
>
boolean

Returns:

true if the object is contained in the iterator, false otherwise
contains
(final Iterator<E>
the iterator to check, may be null
iterator
,
final Object
the object to check
object
)

Checks if the object is contained in the given iterator.

public static <
the element type
E
>
ResettableIterator<E>

Returns:

an iterator over nothing
emptyIterator
()

Gets an empty iterator.

public static <
the element type
E
>
ResettableListIterator<E>

Returns:

a list iterator over nothing
emptyListIterator
()

Gets an empty list iterator.

public static <
the key type
K
,
the value type
V
>
MapIterator<K, V>

Returns:

a map iterator over nothing
emptyMapIterator
()

Gets an empty map iterator.

public static <
the element type
E
>
OrderedIterator<E>

Returns:

an ordered iterator over nothing
emptyOrderedIterator
()

Gets an empty ordered iterator.

public static <
the key type
K
,
the value type
V
>
OrderedMapIterator<K, V>

Returns:

a map iterator over nothing
emptyOrderedMapIterator
()

Gets an empty ordered map iterator.

public static <
the element type
E
>
Iterator<E>

Returns:

a new filtered iterator
filteredIterator
(final Iterator<? extends E>
the iterator to use, not null
iterator
,
final Predicate<? super E>
the predicate to use as a filter, not null
predicate
)

Gets an iterator that filters another iterator.

public static <
the element type
E
>
ListIterator<E>

Returns:

a new filtered iterator
filteredListIterator
(final ListIterator<? extends E>
the list iterator to use, not null
listIterator
,
final Predicate<? super E>
the predicate to use as a filter, not null
predicate
)

Gets a list iterator that filters another list iterator.

public static <
the element type
E
>
E

Returns:

the first element of the iterator which matches the predicate or null if none could be found
find
(final Iterator<E>
the iterator to search, may be null
iterator
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Finds the first element in the given iterator which matches the given predicate.

public static <
the type of object in the Iterator
E
>
E

Returns:

the first object
first
(final Iterator<E>
the iterator to get a value from
iterator
)

Shortcut for get(iterator, 0).

public static <
the element type
E
>
void
forEach(final Iterator<E>
the iterator to use, may be null
iterator
,
final Closure<? super E>
the closure to apply to each element, may not be null
closure
)

Applies the closure to each element of the provided iterator.

public static <
the type of object the Iterator contains
E
>
E

Returns:

the last element in the iterator, or null if iterator is null or empty
forEachButLast
(final Iterator<E>
the iterator to get the input from, may be null
iterator
,
final Closure<? super E>
the closure to perform, may not be null
closure
)

Executes the given closure on each but the last element in the iterator.

public static <
the type of object in the Iterator
E
>
E

Returns:

the object at the specified index
get
(final Iterator<E>
the iterator to get a value from
iterator
,
final int
the index to get
index
)

Returns the index-th value in Iterator, throwing IndexOutOfBoundsException if there is no such element.

public static Iterator<?>

Returns:

a suitable iterator, never null
getIterator
(final Object
the object to convert to an iterator
obj
)

Gets a suitable Iterator for the given object.

public static <
the element type
E
>
int

Returns:

the index of the first element which matches the predicate or -1 if none matches
indexOf
(final Iterator<E>
the iterator to search, may be null
iterator
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Returns the index of the first element in the specified iterator that matches the given predicate.

public static boolean

Returns:

true if the iterator is exhausted or null, false otherwise
isEmpty
(final Iterator<?>
the Iterator to use, may be null
iterator
)

Checks if the given iterator is empty.

public static <
the element type
E
>
ResettableIterator<E>

Returns:

a new looping iterator
loopingIterator
(final Collection<? extends E>
the collection to iterate over, not null
coll
)

Gets an iterator that loops continuously over the supplied collection.

public static <
the element type
E
>
ResettableListIterator<E>

Returns:

a new looping iterator
loopingListIterator
(final List<E>
the list to iterate over, not null
list
)

Gets an iterator that loops continuously over the supplied list.

public static <
the type of object the Iterator contains
E
>
boolean

Returns:

true if every element of the collection matches the predicate or if the collection is empty, false otherwise
matchesAll
(final Iterator<E>
the Iterator to use, may be null
iterator
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Answers true if a predicate is true for every element of an iterator.

public static <
the type of object the Iterator contains
E
>
boolean

Returns:

true if any element of the collection matches the predicate, false otherwise
matchesAny
(final Iterator<E>
the Iterator to use, may be null
iterator
,
final Predicate<? super E>
the predicate to use, may not be null
predicate
)

Answers true if a predicate is true for any element of the iterator.

public static NodeListIterator

Returns:

a new, single use Iterator
nodeListIterator
(final NodeList
the node list to use, may not be null
nodeList
)

Gets an Iterator that wraps the specified NodeList.

public static NodeListIterator

Returns:

a new, single use Iterator
nodeListIterator
(final Node
the node to use, may not be null
node
)

Gets an Iterator that wraps the specified node's childNodes.

public static <
the element type
E
>
Iterator<E>

Returns:

a new object graph iterator
objectGraphIterator
(final E
the root object to start iterating from, null results in an empty iterator
root
,
final Transformer<? super E, ? extends E>
the transformer to use, see above, null uses no effect transformer
transformer
)

Gets an iterator that operates over an object graph.

public static <
the element type
E
>
Iterator<E>

Returns:

a peeking iterator
peekingIterator
(final Iterator<? extends E>
the iterator to decorate, not null
iterator
)

Gets an iterator that supports one-element lookahead.

public static <
the element type
E
>
Iterator<E>

Returns:

a pushback iterator
pushbackIterator
(final Iterator<? extends E>
the iterator to decorate, not null
iterator
)

Gets an iterator that supports pushback of elements.

public static <
the element type
E
>
ResettableIterator<E>

Returns:

a singleton iterator over the object
singletonIterator
(final E
the single object over which to iterate
object
)

Gets a singleton iterator.

public static <
the element type
E
>
ListIterator<E>

Returns:

a singleton list iterator over the object
singletonListIterator
(final E
the single object over which to iterate
object
)

Gets a singleton list iterator.

public static int

Returns:

the number of elements contained in the iterator
size
(final Iterator<?>
the iterator to check, may be null
iterator
)

Returns the number of elements contained in the given iterator.

public static <
the element type
E
>
SkippingIterator<E>

Returns:

a new skipping iterator
skippingIterator
(final Iterator<E>
the iterator to decorate
iterator
,
final long
the first number of elements to skip
offset
)

Decorates the specified iterator to skip the first N elements.

public static Object[]

Returns:

an array of the iterator contents
toArray
(final Iterator<?>
the iterator to use, not null
iterator
)

Gets an array based on an iterator.

public static <
the element type
E
>
E[]

Returns:

an array of the iterator contents
toArray
(final Iterator<? extends E>
the iterator to use, not null
iterator
,
final Class<E>
the class of array to create
arrayClass
)

Gets an array based on an iterator.

public static <
the element type
E
>
List<E>

Returns:

a list of the iterator contents
toList
(final Iterator<? extends E>
the iterator to use, not null
iterator
)

Gets a list based on an iterator.

public static <
the element type
E
>
List<E>

Returns:

a list of the iterator contents
toList
(final Iterator<? extends E>
the iterator to use, not null
iterator
,
final int
the initial size of the ArrayList
estimatedSize
)

Gets a list based on an iterator.

public static <
the element type
E
>
ListIterator<E>

Returns:

a new iterator
toListIterator
(final Iterator<? extends E>
the iterator to use, may not be null
iterator
)

Gets a list iterator based on a simple iterator.

public static <
the element type
E
>
String

Returns:

a string representation of iterator
toString
(final Iterator<E>
the iterator to convert to a string, may be null
iterator
)

Returns a string representation of the elements of the specified iterator.

public static <
the element type
E
>
String

Returns:

a string representation of iterator
toString
(final Iterator<E>
the iterator to convert to a string, may be null
iterator
,
final Transformer<? super E, String>
the transformer used to get a string representation of an element
transformer
)

Returns a string representation of the elements of the specified iterator.

public static <
the element type
E
>
String

Returns:

a string representation of iterator
toString
(final Iterator<E>
the iterator to convert to a string, may be null
iterator
,
final Transformer<? super E, String>
the transformer used to get a string representation of an element
transformer
,
final String
the string to delimit elements
delimiter
,
final String
the prefix, prepended to the string representation
prefix
,
final String
the suffix, appended to the string representation
suffix
)

Returns a string representation of the elements of the specified iterator.

public static <
the input type
I
,
the output type
O
>
Iterator<O>

Returns:

a new transforming iterator
transformedIterator
(final Iterator<? extends I>
the iterator to use, not null
iterator
,
final Transformer<? super I, ? extends O>
the transform to use, not null
transform
)

Gets an iterator that transforms the elements of another iterator.

public static <
the element type
E
>
Iterator<E>

Returns:

an immutable version of the iterator
unmodifiableIterator
(final Iterator<E>
the iterator to make immutable
iterator
)

Gets an immutable version of an Iterator.

public static <
the element type
E
>
ListIterator<E>

Returns:

an immutable version of the iterator
unmodifiableListIterator
(final ListIterator<E>
the iterator to make immutable
listIterator
)

Gets an immutable version of a ListIterator.

public static <
the key type
K
,
the value type
V
>
MapIterator<K, V>

Returns:

an immutable version of the iterator
unmodifiableMapIterator
(final MapIterator<K, V>
the iterator to make immutable
mapIterator
)

Gets an immutable version of a MapIterator.

public static <
the element type
E
>
ZippingIterator<E>

Returns:

an iterator, interleaving the decorated iterators
zippingIterator
(final Iterator<? extends E>
the first iterator to interleave
a
,
final Iterator<? extends E>
the second iterator to interleave
b
)

Returns an iterator that interleaves elements from the decorated iterators.

public static <
the element type
E
>
ZippingIterator<E>

Returns:

an iterator, interleaving the decorated iterators
zippingIterator
(final Iterator<? extends E>
the first iterator to interleave
a
,
final Iterator<? extends E>
the second iterator to interleave
b
,
final Iterator<? extends E>
the third iterator to interleave
c
)

Returns an iterator that interleaves elements from the decorated iterators.

public static <
the element type
E
>
ZippingIterator<E>

Returns:

an iterator, interleaving the decorated iterators
zippingIterator
(final Iterator<? extends E>...
the array of iterators to interleave
iterators
)

Returns an iterator that interleaves elements from the decorated iterators.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait