Class ExtendedIterator<T>
- java.lang.Object
-
- org.apache.commons.collections4.iterators.ExtendedIterator<T>
-
- Type Parameters:
T- The type of object returned from the iterator.
- All Implemented Interfaces:
java.util.Iterator<T>,IteratorOperations<T>
public final class ExtendedIterator<T> extends java.lang.Object implements IteratorOperations<T>
Extends Iterator functionality to include operations commonly found on streams (for example filtering, concatenating, mapping). It also provides convenience methods for common operations.- Since:
- 4.5.0-M3
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <X extends T>
ExtendedIterator<T>andThen(java.util.Iterator<X> other)Chains theotheriterator to the end of this one.static <T> ExtendedIterator<T>create(java.util.Iterator<T> it)Create an ExtendedIterator returning the elements ofit.static <T> ExtendedIterator<T>create(java.util.stream.Stream<T> stream)Creates an ExtendedIterator wrapped round aStream.static <T> ExtendedIterator<T>createNoRemove(java.util.Iterator<T> it)Creates an ExtendedIterator wrapped roundit, which does not permit.remove()even ifitdoes.static ExtendedIterator<?>emptyIterator()Creates an empty Extended iterator.ExtendedIterator<T>filter(java.util.function.Predicate<T> predicate)Filter this iterator using a predicate.static <T> ExtendedIterator<T>flatten(java.util.Iterator<java.util.Iterator<T>> iterators)Flattens an iterator of iterators into an Iterator over the next level values.voidforEachRemaining(java.util.function.Consumer<? super T> action)booleanhasNext()<U> ExtendedIterator<U>map(java.util.function.Function<T,U> function)Map the elements of the iterator to a now type.Tnext()voidremove()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.collections4.iterators.IteratorOperations
addTo, removeNext, toCollection, toList, toSet
-
-
-
-
Method Detail
-
create
public static <T> ExtendedIterator<T> create(java.util.Iterator<T> it)
Create an ExtendedIterator returning the elements ofit. Ifitis itself an ExtendedIterator, return that; otherwise wrapit.- Type Parameters:
T- The type of object returned from the iterator.- Parameters:
it- The iterator to wrap.- Returns:
- An Extended iterator wrapping
it
-
create
public static <T> ExtendedIterator<T> create(java.util.stream.Stream<T> stream)
Creates an ExtendedIterator wrapped round aStream. The extended iterator does not permit.remove().The stream should not be used directly. The effect of doing so is undefined.
- Type Parameters:
T- The type of object returned from the iterator.- Parameters:
stream- the Stream to create an iterator from.- Returns:
- an Extended iterator on the
streamiterator.
-
createNoRemove
public static <T> ExtendedIterator<T> createNoRemove(java.util.Iterator<T> it)
Creates an ExtendedIterator wrapped roundit, which does not permit.remove()even ifitdoes.- Type Parameters:
T- The type of object returned from the iterator.- Parameters:
it- The Iterator to wrap.- Returns:
- an Extended iterator on
it - Throws:
java.lang.UnsupportedOperationException- if remove() is called on the resulting iterator.
-
emptyIterator
public static ExtendedIterator<?> emptyIterator()
Creates an empty Extended iterator.- Returns:
- An empty Extended iterator.
-
flatten
public static <T> ExtendedIterator<T> flatten(java.util.Iterator<java.util.Iterator<T>> iterators)
Flattens an iterator of iterators into an Iterator over the next level values. Similar to list splicing in lisp.- Type Parameters:
T- The type of object returned from the iterator.- Parameters:
iterators- An iterator of iterators.- Returns:
- An iterator over the logical concatenation of the inner iterators.
-
andThen
public <X extends T> ExtendedIterator<T> andThen(java.util.Iterator<X> other)
Chains theotheriterator to the end of this one.- Type Parameters:
X- The type of object returned from the other iterator.- Parameters:
other- the other iterator to extend this iterator with.- Returns:
- A new iterator returning the contents of
thisiterator followed by the contents ofotheriterator.
-
filter
public ExtendedIterator<T> filter(java.util.function.Predicate<T> predicate)
Filter this iterator using a predicate. Only items for which the predicate returnstruewill be included in the result.- Parameters:
predicate- The predicate to filter the items with.- Returns:
- An iterator filtered by the predicate.
-
forEachRemaining
public void forEachRemaining(java.util.function.Consumer<? super T> action)
- Specified by:
forEachRemainingin interfacejava.util.Iterator<T>
-
map
public <U> ExtendedIterator<U> map(java.util.function.Function<T,U> function)
Map the elements of the iterator to a now type.- Type Parameters:
U- The object type to return.- Parameters:
function- The function to map elements of<T>to type<U>.- Returns:
- An Extended iterator that returns a
<U>for very<T>in the original iterator.
-
-