-
- Type Parameters:
T- the type contained in the iterator
- All Superinterfaces:
Foldable<T>,java.lang.Iterable<T>,java.util.Iterator<T>,java.io.Serializable,Streamable<T>,Traversable<T>,Value<T>
public interface Iterator<T> extends java.util.Iterator<T>, Traversable<T>
An extension on the defaultIteratorthat adds utility operations to easily manipulate the iterator or locate elements inside it.- Since:
- 0.0.3
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <T> Iterator<T>concat(Iterator<T>... iterators)Combine multiple iterator instances into one big iterator.Iterator<T>filter(java.util.function.Predicate<T> predicate)Filter out an element if it does not match the suppliedpredicate.default Optional<T>first(java.util.function.Predicate<T> predicate)Find the first match in the elements using the providedPredicate.default <U> UfoldLeft(U start, java.util.function.BiFunction<? super U,? super T,? extends U> combiner)Folds the elements from the left, starting with thestartvalue and combining the result by successively calling thecombineroperation.default <U> UfoldRight(U start, java.util.function.BiFunction<? super T,? super U,? extends U> combiner)Folds the elements from the right, starting with thestartvalue and combining the result by successively calling thecombiner.default Tget()This operation is a convenience method for theIterator.next().default booleanisSingleValued()Is this instance single-valued or not.default Iterator<T>iterator()default Optional<T>last(java.util.function.Predicate<T> predicate)Find the last match in the Iterator using the providedPredicate.default <U> Iterator<U>map(java.util.function.Function<T,U> mapper)Perform a mapping operation on the elements in the stream.default TreduceLeft(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)Reduce the collection from the left, starting with the first element in the collection and successively calling thereduceruntil there are no more elements.voidreset()Move the iterator back to the first element in the sequence.default java.lang.Object[]toNativeArray()Create a primitive array of the elements contained within the iterator.
-
-
-
Method Detail
-
filter
Iterator<T> filter(java.util.function.Predicate<T> predicate)
Description copied from interface:StreamableFilter out an element if it does not match the suppliedpredicate. This operation will iterate over all elements and return a new set containing only the elements where the predicate returnstruefor.- Specified by:
filterin interfaceStreamable<T>- Specified by:
filterin interfaceTraversable<T>- Parameters:
predicate- the predicate to apply to the contents of this- Returns:
- the filtered value
-
first
default Optional<T> first(java.util.function.Predicate<T> predicate)
Description copied from interface:TraversableFind the first match in the elements using the providedPredicate. The returned Optional isnullsafe and will either contain the element or be an emptyOptional.Example:
// the result will be an Optional with the value 2 int firstMatch = Collection(1, 2, 3, 4) .first(i -> i % 2 == 0);- Specified by:
firstin interfaceTraversable<T>- Parameters:
predicate- the predicate to use- Returns:
- the first match found
-
foldLeft
default <U> U foldLeft(U start, java.util.function.BiFunction<? super U,? super T,? extends U> combiner)Description copied from interface:FoldableFolds the elements from the left, starting with thestartvalue and combining the result by successively calling thecombineroperation.Example:
// result "!test" List("t", "e", "s", "t").foldLeft("!", (x, y) -> x + y)
-
foldRight
default <U> U foldRight(U start, java.util.function.BiFunction<? super T,? super U,? extends U> combiner)Description copied from interface:FoldableFolds the elements from the right, starting with thestartvalue and combining the result by successively calling thecombiner.Example:
// result "tset!" List("t", "e", "s", "t").foldRight("!", (x, y) -> x + y)
-
get
default T get()
This operation is a convenience method for theIterator.next().
-
isSingleValued
default boolean isSingleValued()
Description copied from interface:ValueIs this instance single-valued or not.- Specified by:
isSingleValuedin interfaceValue<T>- Returns:
trueif single-valued, otherwisefalse
-
iterator
default Iterator<T> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<T>- Specified by:
iteratorin interfaceTraversable<T>
-
last
default Optional<T> last(java.util.function.Predicate<T> predicate)
Description copied from interface:TraversableFind the last match in the Iterator using the providedPredicate. The returned Optional isnullsafe and will either contain the element or be an emptyOptional.Example:
// the result will be an Optional with the value 4 int firstMatch = Collection(1, 2, 3, 4) .last(i -> i % 2 == 0);- Specified by:
lastin interfaceTraversable<T>- Parameters:
predicate- the predicate to use- Returns:
- the last match found
-
map
default <U> Iterator<U> map(java.util.function.Function<T,U> mapper)
Description copied from interface:StreamablePerform a mapping operation on the elements in the stream.This operation will loop over all elements in the stream and apply the
mappermethod. The mapped values will be returned in as a new stream of elements.- Specified by:
mapin interfaceStreamable<T>- Specified by:
mapin interfaceTraversable<T>- Type Parameters:
U- the type of object expected as a result- Parameters:
mapper- the mapping functionality- Returns:
- the mapped object
-
reduceLeft
default T reduceLeft(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)
Description copied from interface:FoldableReduce the collection from the left, starting with the first element in the collection and successively calling the
reduceruntil there are no more elements.Example:
// result "test" List("t", "e", "s", "t").reduceLeft((x, y) -> x + y)This operation is similar to the Foldable.foldLeft(Object, BiFunction) where the first element is the head of the elements.
- Specified by:
reduceLeftin interfaceFoldable<T>- Parameters:
reducer- the operation to use to reduce the elements to one value- Returns:
- the reduced value
- See Also:
Foldable.foldLeft(Object, BiFunction)
-
reset
void reset()
Move the iterator back to the first element in the sequence.
-
toNativeArray
default java.lang.Object[] toNativeArray()
Create a primitive array of the elements contained within the iterator.- Returns:
- the primitive array
-
concat
@SafeVarargs static <T> Iterator<T> concat(Iterator<T>... iterators)
Combine multiple iterator instances into one big iterator.- Type Parameters:
T- the type of the iterator- Parameters:
iterators- the iterators to be combined- Returns:
- the new iterator containing all elements of all iterators
-
-