-
- 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 defaultIterator
that 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> U
foldLeft(U start, java.util.function.BiFunction<? super U,? super T,? extends U> combiner)
Folds the elements from the left, starting with thestart
value and combining the result by successively calling thecombiner
operation.default <U> U
foldRight(U start, java.util.function.BiFunction<? super T,? super U,? extends U> combiner)
Folds the elements from the right, starting with thestart
value and combining the result by successively calling thecombiner
.default T
get()
This operation is a convenience method for theIterator.next()
.default boolean
isSingleValued()
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 T
reduceLeft(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 thereducer
until there are no more elements.void
reset()
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:Streamable
Filter 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 returnstrue
for.- Specified by:
filter
in interfaceStreamable<T>
- Specified by:
filter
in 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:Traversable
Find the first match in the elements using the providedPredicate
. The returned Optional isnull
safe 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:
first
in 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:Foldable
Folds the elements from the left, starting with thestart
value and combining the result by successively calling thecombiner
operation.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:Foldable
Folds the elements from the right, starting with thestart
value 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:Value
Is this instance single-valued or not.- Specified by:
isSingleValued
in interfaceValue<T>
- Returns:
true
if single-valued, otherwisefalse
-
iterator
default Iterator<T> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- Specified by:
iterator
in interfaceTraversable<T>
-
last
default Optional<T> last(java.util.function.Predicate<T> predicate)
Description copied from interface:Traversable
Find the last match in the Iterator using the providedPredicate
. The returned Optional isnull
safe 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:
last
in 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:Streamable
Perform a mapping operation on the elements in the stream.This operation will loop over all elements in the stream and apply the
mapper
method. The mapped values will be returned in as a new stream of elements.- Specified by:
map
in interfaceStreamable<T>
- Specified by:
map
in 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:Foldable
Reduce the collection from the left, starting with the first element in the collection and successively calling the
reducer
until 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:
reduceLeft
in 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
-
-