java.lang.Object | |
↳ | org.apache.commons.collections.IteratorUtils |
Provides static utility methods and decorators for Iterator instances. The implementations are provided in the iterators subpackage.
WARNING: Due to human error certain binary incompatabilities were introduced between Commons Collections 2.1 and 3.0. The class remained source and test compatible, so if you can recompile all your classes and dependencies everything is OK. Those methods which are binary incompatible are marked as such, together with alternate solutions that are binary compatible against versions 2.1.1 and 3.1.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
EMPTY_ITERATOR | An iterator over no elements. | ||||||||||
EMPTY_LIST_ITERATOR | A list iterator over no elements. | ||||||||||
EMPTY_MAP_ITERATOR | A map iterator over no elements. | ||||||||||
EMPTY_ORDERED_ITERATOR | An ordered iterator over no elements. | ||||||||||
EMPTY_ORDERED_MAP_ITERATOR | An ordered map iterator over no elements. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
IteratorUtils is not normally instantiated.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Gets an iterator over part of an object or primitive array.
| |||||||||||
Gets an iterator over the end part of an object array.
| |||||||||||
Gets an iterator over part of an object array.
| |||||||||||
Gets an iterator over an object or primitive array.
| |||||||||||
Gets an iterator over an object array.
| |||||||||||
Gets an iterator over the end part of an object or primitive array.
| |||||||||||
Gets a list iterator over part of an object or primitive array.
| |||||||||||
Gets a list iterator over an object array.
| |||||||||||
Gets a list iterator over part of an object array.
| |||||||||||
Gets a list iterator over the end part of an object or primitive array.
| |||||||||||
Gets a list iterator over an object or primitive array.
| |||||||||||
Gets a list iterator over the end part of an object array.
| |||||||||||
Gets an enumeration that wraps an iterator.
| |||||||||||
Gets an iterator that provides an iterator view of the given enumeration.
| |||||||||||
Gets an iterator that provides an iterator view of the given enumeration
that will remove elements from the specified collection.
| |||||||||||
Gets an iterator that iterates through an array of
Iterator s
one after another. | |||||||||||
Gets an iterator that iterates through a collections of
Iterator s
one after another. | |||||||||||
Gets an iterator that iterates through two
Iterator s
one after another. | |||||||||||
Gets an iterator that provides an ordered iteration over the elements
contained in a collection of ordered
Iterator s. | |||||||||||
Gets an iterator that provides an ordered iteration over the elements
contained in an array of
Iterator s. | |||||||||||
Gets an iterator that provides an ordered iteration over the elements
contained in a collection of
Iterator s. | |||||||||||
Gets an empty iterator.
| |||||||||||
Gets an empty list iterator.
| |||||||||||
Gets an empty map iterator.
| |||||||||||
Gets an empty ordered iterator.
| |||||||||||
Gets an empty ordered map iterator.
| |||||||||||
Gets an iterator that filters another iterator.
| |||||||||||
Gets a list iterator that filters another list iterator.
| |||||||||||
Gets a suitable Iterator for the given object.
| |||||||||||
Gets an iterator that loops continuously over the supplied collection.
| |||||||||||
Gets an iterator that loops continuously over the supplied list.
| |||||||||||
Gets an iterator that operates over an object graph.
| |||||||||||
Gets a singleton iterator.
| |||||||||||
Gets a singleton list iterator.
| |||||||||||
Gets an array based on an iterator.
| |||||||||||
Gets an array based on an iterator.
| |||||||||||
Gets a list based on an iterator.
| |||||||||||
Gets a list based on an iterator.
| |||||||||||
Gets a list iterator based on a simple iterator.
| |||||||||||
Gets an iterator that transforms the elements of another iterator.
| |||||||||||
Gets an immutable version of an
Iterator . | |||||||||||
Gets an immutable version of a
ListIterator . | |||||||||||
Gets an immutable version of a
MapIterator . |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
An iterator over no elements.
WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1.
Use EmptyIterator.INSTANCE
for compatability with Commons Collections 2.1.1.
A list iterator over no elements.
WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1.
Use EmptyListIterator.INSTANCE
for compatability with Commons Collections 2.1.1.
An ordered map iterator over no elements.
IteratorUtils is not normally instantiated.
Gets an iterator over part of an object or primitive array.
This method will handle primitive arrays as well as object arrays. The primitives will be wrapped in the appropriate wrapper class.
array | the array over which to iterate |
---|---|
start | the index to start iterating at |
end | the index to finish iterating at |
IllegalArgumentException | if the array is not an array |
---|---|
IndexOutOfBoundsException | if array bounds are invalid |
IllegalArgumentException | if end is before start |
NullPointerException | if array is null |
Gets an iterator over the end part of an object array.
WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
Use new ArrayIterator(array,start)
for compatability.
array | the array over which to iterate |
---|---|
start | the index to start iterating at |
IndexOutOfBoundsException | if start is less than zero or greater than the length of the array |
---|---|
NullPointerException | if array is null |
Gets an iterator over part of an object array.
WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
Use new ArrayIterator(array,start,end)
for compatability.
array | the array over which to iterate |
---|---|
start | the index to start iterating at |
end | the index to finish iterating at |
IndexOutOfBoundsException | if array bounds are invalid |
---|---|
IllegalArgumentException | if end is before start |
NullPointerException | if array is null |
Gets an iterator over an object or primitive array.
This method will handle primitive arrays as well as object arrays. The primitives will be wrapped in the appropriate wrapper class.
array | the array over which to iterate |
---|
IllegalArgumentException | if the array is not an array |
---|---|
NullPointerException | if array is null |
Gets an iterator over an object array.
WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
Use new ArrayIterator(array)
for compatability.
array | the array over which to iterate |
---|
NullPointerException | if array is null |
---|
Gets an iterator over the end part of an object or primitive array.
This method will handle primitive arrays as well as object arrays. The primitives will be wrapped in the appropriate wrapper class.
array | the array over which to iterate |
---|---|
start | the index to start iterating at |
IllegalArgumentException | if the array is not an array |
---|---|
IndexOutOfBoundsException | if start is less than zero or greater than the length of the array |
NullPointerException | if array is null |
Gets a list iterator over part of an object or primitive array.
This method will handle primitive arrays as well as object arrays. The primitives will be wrapped in the appropriate wrapper class.
array | the array over which to iterate |
---|---|
start | the index to start iterating at |
end | the index to finish iterating at |
IllegalArgumentException | if the array is not an array |
---|---|
IndexOutOfBoundsException | if array bounds are invalid |
IllegalArgumentException | if end is before start |
NullPointerException | if array is null |
Gets a list iterator over an object array.
array | the array over which to iterate |
---|
NullPointerException | if array is null |
---|
Gets a list iterator over part of an object array.
array | the array over which to iterate |
---|---|
start | the index to start iterating at |
end | the index to finish iterating at |
IndexOutOfBoundsException | if array bounds are invalid |
---|---|
IllegalArgumentException | if end is before start |
NullPointerException | if array is null |
Gets a list iterator over the end part of an object or primitive array.
This method will handle primitive arrays as well as object arrays. The primitives will be wrapped in the appropriate wrapper class.
array | the array over which to iterate |
---|---|
start | the index to start iterating at |
IllegalArgumentException | if the array is not an array |
---|---|
IndexOutOfBoundsException | if start is less than zero |
NullPointerException | if array is null |
Gets a list iterator over an object or primitive array.
This method will handle primitive arrays as well as object arrays. The primitives will be wrapped in the appropriate wrapper class.
array | the array over which to iterate |
---|
IllegalArgumentException | if the array is not an array |
---|---|
NullPointerException | if array is null |
Gets a list iterator over the end part of an object array.
array | the array over which to iterate |
---|---|
start | the index to start iterating at |
IndexOutOfBoundsException | if start is less than zero |
---|---|
NullPointerException | if array is null |
Gets an enumeration that wraps an iterator.
iterator | the iterator to use, not null |
---|
NullPointerException | if iterator is null |
---|
Gets an iterator that provides an iterator view of the given enumeration.
enumeration | the enumeration to use |
---|
Gets an iterator that provides an iterator view of the given enumeration that will remove elements from the specified collection.
enumeration | the enumeration to use |
---|---|
removeCollection | the collection to remove elements from |
Gets an iterator that iterates through an array of Iterator
s
one after another.
iterators | the iterators to use, not null or empty or contain nulls |
---|
NullPointerException | if iterators array is null or contains a null |
---|
Gets an iterator that iterates through a collections of Iterator
s
one after another.
iterators | the iterators to use, not null or empty or contain nulls |
---|
NullPointerException | if iterators collection is null or contains a null |
---|---|
ClassCastException | if the iterators collection contains the wrong object type |
Gets an iterator that iterates through two Iterator
s
one after another.
iterator1 | the first iterators to use, not null |
---|---|
iterator2 | the first iterators to use, not null |
NullPointerException | if either iterator is null |
---|
Gets an iterator that provides an ordered iteration over the elements
contained in a collection of ordered Iterator
s.
Given two ordered Iterator
s A
and B
,
the next()
method will return the lesser of
A.next()
and B.next()
.
The comparator is optional. If null is specified then natural order is used.
comparator | the comparator to use, may be null for natural order |
---|---|
iterator1 | the first iterators to use, not null |
iterator2 | the first iterators to use, not null |
NullPointerException | if either iterator is null |
---|
Gets an iterator that provides an ordered iteration over the elements
contained in an array of Iterator
s.
Given two ordered Iterator
s A
and B
,
the next()
method will return the lesser of
A.next()
and B.next()
and so on.
The comparator is optional. If null is specified then natural order is used.
comparator | the comparator to use, may be null for natural order |
---|---|
iterators | the iterators to use, not null or empty or contain nulls |
NullPointerException | if iterators array is null or contains a null |
---|
Gets an iterator that provides an ordered iteration over the elements
contained in a collection of Iterator
s.
Given two ordered Iterator
s A
and B
,
the next()
method will return the lesser of
A.next()
and B.next()
and so on.
The comparator is optional. If null is specified then natural order is used.
comparator | the comparator to use, may be null for natural order |
---|---|
iterators | the iterators to use, not null or empty or contain nulls |
NullPointerException | if iterators collection is null or contains a null |
---|---|
ClassCastException | if the iterators collection contains the wrong object type |
Gets an empty iterator.
This iterator is a valid iterator object that will iterate over nothing.
WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
Use EmptyIterator.INSTANCE
for compatability with Commons Collections 2.1.1.
Gets an empty list iterator.
This iterator is a valid list iterator object that will iterate over nothing.
WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
Use EmptyListIterator.INSTANCE
for compatability with Commons Collections 2.1.1.
Gets an empty map iterator.
This iterator is a valid map iterator object that will iterate over nothing.
Gets an empty ordered iterator.
This iterator is a valid iterator object that will iterate over nothing.
Gets an empty ordered map iterator.
This iterator is a valid map iterator object that will iterate over nothing.
Gets an iterator that filters another iterator.
The returned iterator will only return objects that match the specified filtering predicate.
iterator | the iterator to use, not null |
---|---|
predicate | the predicate to use as a filter, not null |
NullPointerException | if either parameter is null |
---|
Gets a list iterator that filters another list iterator.
The returned iterator will only return objects that match the specified filtering predicate.
listIterator | the list iterator to use, not null |
---|---|
predicate | the predicate to use as a filter, not null |
NullPointerException | if either parameter is null |
---|
Gets a suitable Iterator for the given object.
This method can handles objects as follows
obj | the object to convert to an iterator |
---|
Gets an iterator that loops continuously over the supplied collection.
The iterator will only stop looping if the remove method is called enough times to empty the collection, or if the collection is empty to start with.
coll | the collection to iterate over, not null |
---|
NullPointerException | if the collection is null |
---|
Gets an iterator that loops continuously over the supplied list.
The iterator will only stop looping if the remove method is called enough times to empty the list, or if the list is empty to start with.
list | the list to iterate over, not null |
---|
NullPointerException | if the list is null |
---|
Gets an iterator that operates over an object graph.
This iterator can extract multiple objects from a complex tree-like object graph.
The iteration starts from a single root object.
It uses a Transformer
to extract the iterators and elements.
Its main benefit is that no intermediate List
is created.
For example, consider an object graph:
|- Branch -- Leaf | \- Leaf |- Tree | /- Leaf | |- Branch -- Leaf Forest | \- Leaf | |- Branch -- Leaf | | \- Leaf |- Tree | /- Leaf |- Branch -- Leaf |- Branch -- LeafThe following
Transformer
, used in this class, will extract all
the Leaf objects without creating a combined intermediate list:
public Object transform(Object input) { if (input instanceof Forest) { return ((Forest) input).treeIterator(); } if (input instanceof Tree) { return ((Tree) input).branchIterator(); } if (input instanceof Branch) { return ((Branch) input).leafIterator(); } if (input instanceof Leaf) { return input; } throw new ClassCastException(); }
Internally, iteration starts from the root object. When next is called, the transformer is called to examine the object. The transformer will return either an iterator or an object. If the object is an Iterator, the next element from that iterator is obtained and the process repeats. If the element is an object it is returned.
Under many circumstances, linking Iterators together in this manner is more efficient (and convenient) than using nested for loops to extract a list.
root | the root object to start iterating from, null results in an empty iterator |
---|---|
transformer | the transformer to use, see above, null uses no effect transformer |
Gets a singleton iterator.
This iterator is a valid iterator object that will iterate over the specified object.
WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
Use new SingletonIterator(object)
for compatability.
object | the single object over which to iterate |
---|
Gets a singleton list iterator.
This iterator is a valid list iterator object that will iterate over the specified object.
object | the single object over which to iterate |
---|
Gets an array based on an iterator.
As the wrapped Iterator is traversed, an ArrayList of its values is created. At the end, this is converted to an array.
iterator | the iterator to use, not null |
---|
NullPointerException | if iterator parameter is null |
---|
Gets an array based on an iterator.
As the wrapped Iterator is traversed, an ArrayList of its values is created. At the end, this is converted to an array.
iterator | the iterator to use, not null |
---|---|
arrayClass | the class of array to create |
NullPointerException | if iterator parameter is null |
---|---|
NullPointerException | if arrayClass is null |
ClassCastException | if the arrayClass is invalid |
Gets a list based on an iterator.
As the wrapped Iterator is traversed, an ArrayList of its values is created. At the end, the list is returned.
iterator | the iterator to use, not null |
---|
NullPointerException | if iterator parameter is null |
---|
Gets a list based on an iterator.
As the wrapped Iterator is traversed, an ArrayList of its values is created. At the end, the list is returned.
iterator | the iterator to use, not null |
---|---|
estimatedSize | the initial size of the ArrayList |
NullPointerException | if iterator parameter is null |
---|---|
IllegalArgumentException | if the size is less than 1 |
Gets a list iterator based on a simple iterator.
As the wrapped Iterator is traversed, a LinkedList of its values is cached, permitting all required operations of ListIterator.
iterator | the iterator to use, not null |
---|
NullPointerException | if iterator parameter is null |
---|
Gets an iterator that transforms the elements of another iterator.
The transformation occurs during the next() method and the underlying iterator is unaffected by the transformation.
iterator | the iterator to use, not null |
---|---|
transform | the transform to use, not null |
NullPointerException | if either parameter is null |
---|
Gets an immutable version of an Iterator
. The returned object
will always throw an UnsupportedOperationException
for
the remove()
method.
iterator | the iterator to make immutable |
---|
Gets an immutable version of a ListIterator
. The returned object
will always throw an UnsupportedOperationException
for
the remove()
, add(E)
and
set(E)
methods.
listIterator | the iterator to make immutable |
---|
Gets an immutable version of a MapIterator
. The returned object
will always throw an UnsupportedOperationException
for
the remove()
, setValue(Object)
methods.
mapIterator | the iterator to make immutable |
---|