Interface Iterator<T>
-
- Type Parameters:
T- the element type
- All Superinterfaces:
Foldable<T>,java.lang.Iterable<T>,java.util.Iterator<T>,Traversable<T>,Value<T>
- All Known Implementing Classes:
AbstractIterator,BitSetModule.BitSetIterator,HashArrayMappedTrieModule.LeafNodeIterator,IteratorModule.CachedIterator,IteratorModule.ConcatIterator,IteratorModule.DistinctIterator,IteratorModule.EmptyIterator,IteratorModule.GroupedIterator,StreamModule.FlatMapIterator,StreamModule.StreamIterator
public interface Iterator<T> extends java.util.Iterator<T>, Traversable<T>
A compositional alternative tojava.util.Iteratordesigned for single-pass traversal of a sequence.Note: Iterators maintain an internal mutable state. They are not thread-safe and must not be reused or shared across operations (for example, after passing them to List.ofAll(Iterable)).
The abstraction defines two fundamental operations:
hasNext(), which checks whether another element is available, andnext(), which consumes and returns that element. IfhasNext()returnsfalse,next()will throwNoSuchElementException.Caution: Methods other than
hasNext()andnext()are single-use. Once such a method has been invoked, further method calls on the same iterator are not guaranteed to succeed.In essence, an Iterator represents a traversal cursor over a collection rather than the collection itself, and can therefore be consumed only once.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description default <R> Iterator<R>collect(@NonNull PartialFunction<? super T,? extends R> partialFunction)Applies aPartialFunctionto all elements that are defined for it and collects the results.static <T> Iterator<T>concat(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> iterables)Creates anIteratorthat iterates over all elements of the supplied sequence of iterables, in order.static <T> Iterator<T>concat(java.lang.Iterable<? extends T>... iterables)Creates anIteratorthat traverses the elements of the provided iterables in sequence, as if they were concatenated.default Iterator<T>concat(java.util.Iterator<? extends T> that)Returns a newIteratorthat yields the elements of this iterator followed by all elements of the specified iterator.static <T> Iterator<T>continually(java.util.function.Supplier<? extends T> supplier)Returns an infiniteIteratorthat repeatedly generates values using the providedSupplier.static <T> Iterator<T>continually(T t)Returns an infiniteIteratorthat endlessly yields the given element.default Iterator<T>distinct()Returns a newTraversablecontaining the elements of this instance with all duplicates removed.default Iterator<T>distinctBy(@NonNull java.util.Comparator<? super T> comparator)Returns a newTraversablecontaining the elements of this instance without duplicates, as determined by the givencomparator.default <U> Iterator<T>distinctBy(@NonNull java.util.function.Function<? super T,? extends U> keyExtractor)Returns a newTraversablecontaining the elements of this instance without duplicates, based on keys extracted from elements usingkeyExtractor.default Iterator<T>distinctByKeepLast(java.util.Comparator<? super T> comparator)Returns a newIteratorcontaining the elements of this instance without duplicates, keeping the last occurrence of each duplicate element, as determined by the givencomparator.default <U> Iterator<T>distinctByKeepLast(java.util.function.Function<? super T,? extends U> keyExtractor)Returns a newIteratorcontaining the elements of this instance without duplicates, keeping the last occurrence of each duplicate element, based on keys extracted from elements usingkeyExtractor.default Iterator<T>drop(int n)Removes up to n elements from this iterator.default Iterator<T>dropRight(int n)Returns a newTraversablewithout the lastnelements, or an empty instance if this contains fewer thannelements.default Iterator<T>dropUntil(@NonNull java.util.function.Predicate<? super T> predicate)Returns a newTraversablestarting from the first element that satisfies the givenpredicate, dropping all preceding elements.default Iterator<T>dropWhile(@NonNull java.util.function.Predicate<? super T> predicate)Returns a newTraversablestarting from the first element that does not satisfy the givenpredicate, dropping all preceding elements.static <T> Iterator<T>empty()Returns an emptyIterator.static <T> Iterator<T>fill(int n, java.util.function.Supplier<? extends T> s)Returns anIteratorover a sequence ofnelements supplied by the givenSupplier.static <T> Iterator<T>fill(int n, T element)Returns anIteratorcontaining the givenelementrepeatedntimes.default Iterator<T>filter(@NonNull java.util.function.Predicate<? super T> predicate)Returns an Iterator that contains elements that satisfy the givenpredicate.default Option<T>findLast(@NonNull java.util.function.Predicate<? super T> predicate)Returns the last element that satisfies the given predicate.default <U> Iterator<U>flatMap(@NonNull java.util.function.Function<? super T,? extends java.lang.Iterable<? extends U>> mapper)FlatMaps the elements of this Iterator to Iterables, which are iterated in the order of occurrence.default <U> UfoldRight(U zero, @NonNull java.util.function.BiFunction<? super T,? super U,? extends U> f)Folds the elements of this structure from the right, starting with the givenzerovalue and successively applying thecombinefunction to each element.static Iterator<java.lang.Integer>from(int value)Returns an infiniteIteratorof int values starting fromvalue.static Iterator<java.lang.Integer>from(int value, int step)Returns an infiniteIteratorof int values starting fromvalueand advancing by the specifiedstep.static Iterator<java.lang.Long>from(long value)Returns an infiniteIteratorof long values starting fromvalue.static Iterator<java.lang.Long>from(long value, long step)Returns an infiniteIteratorof long values starting fromvalueand advancing by the specifiedstep.default Tget()Returns the first element of thisTraversablein iteration order.default <C> Map<C,Iterator<T>>groupBy(@NonNull java.util.function.Function<? super T,? extends C> classifier)Groups elements of thisTraversablebased on a classifier function.default Iterator<Seq<T>>grouped(int size)Splits thisTraversableinto consecutive blocks of the given size.default booleanhasDefiniteSize()Indicates whether thisTraversablehas a known finite size.default Thead()Returns the first element of this non-emptyTraversable.default Iterator<T>init()Returns all elements of this Traversable except the last one.default Option<Iterator<T>>initOption()Returns all elements of this Traversable except the last one, wrapped in anOption.default Iterator<T>intersperse(T element)Returns a newIteratorwhere the specifiedelementis inserted between each element of this iterator.default booleanisAsync()AnIteratoris computed synchronously.default booleanisEmpty()Checks if this Traversable contains no elements.default booleanisLazy()AnIteratoris computed lazily.default booleanisSequential()Indicates whether the elements of this Traversable appear in encounter (insertion) order.default booleanisTraversableAgain()Checks if this Traversable can be traversed multiple times without side effects.static <T> Iterator<T>iterate(java.util.function.Supplier<? extends Option<? extends T>> supplier)Creates anIteratorthat repeatedly invokes the givenSupplieras long as it returns aSomevalue, terminating when it returnsNone.static <T> Iterator<T>iterate(T seed, java.util.function.Function<? super T,? extends T> f)Returns an infiniteIteratorthat generates values by repeatedly applying the given function to the previous value, starting withseed.default @NonNull Iterator<T>iterator()Returns an iterator over the elements of this Traversable, implemented viaTraversable.head()andTraversable.tail().default Tlast()Returns the last element of this Traversable.default intlength()Returns the number of elements in this Traversable.default <U> Iterator<U>map(@NonNull java.util.function.Function<? super T,? extends U> mapper)Maps the elements of this Iterator lazily using the givenmapper.default <U> Iterator<U>mapTo(U value)Maps the underlying value to another fixed value.default Iterator<java.lang.Void>mapToVoid()Maps the underlying value to Voidstatic <T> Iterator<T>narrow(Iterator<? extends T> iterator)Narrows anIterator<? extends T>toIterator<T>using a type-safe cast.static <T> Iterator<T>of(T element)Creates anIteratorthat yields exactly one element.static <T> Iterator<T>of(T... elements)Creates anIteratorthat iterates over the provided elements.static Iterator<java.lang.Boolean>ofAll(boolean... elements)Creates anIteratorover the given boolean values.static Iterator<java.lang.Byte>ofAll(byte... elements)Creates anIteratorover the given byte values.static Iterator<java.lang.Character>ofAll(char... elements)Creates anIteratorover the given char values.static Iterator<java.lang.Double>ofAll(double... elements)Creates anIteratorover the given double values.static Iterator<java.lang.Float>ofAll(float... elements)Creates anIteratorover the given float values.static Iterator<java.lang.Integer>ofAll(int... elements)Creates anIteratorover the given int values.static Iterator<java.lang.Long>ofAll(long... elements)Creates anIteratorover the given long values.static Iterator<java.lang.Short>ofAll(short... elements)Creates anIteratorover the given short values.static <T> Iterator<T>ofAll(java.lang.Iterable<? extends T> iterable)Creates anIteratorfrom the providedIterable.static <T> Iterator<T>ofAll(java.util.Iterator<? extends T> iterator)Creates anIteratorthat delegateshasNext()andnext()to the givenIterator.default Iterator<T>orElse(@NonNull java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)Returns thisTraversableif it is non-empty; otherwise, returns the result of evaluating the given supplier.default Iterator<T>orElse(java.lang.Iterable<? extends T> other)Returns thisTraversableif it is non-empty; otherwise, returns the given alternative.default Tuple2<Iterator<T>,Iterator<T>>partition(@NonNull java.util.function.Predicate<? super T> predicate)Splits thisTraversableinto two partitions according to a predicate.default Iterator<T>peek(@NonNull java.util.function.Consumer<? super T> action)Performs the givenactionon the first element if this is an eager implementation.static Iterator<java.lang.Character>range(char from, char toExclusive)Creates anIteratorof characters starting fromfrom(inclusive) up totoExclusive(exclusive).static Iterator<java.lang.Integer>range(int from, int toExclusive)Creates anIteratorof int values starting fromfrom(inclusive) up totoExclusive(exclusive).static Iterator<java.lang.Long>range(long from, long toExclusive)Creates anIteratorof long values starting fromfrom(inclusive) up totoExclusive(exclusive).static Iterator<java.lang.Character>rangeBy(char from, char toExclusive, int step)Creates anIteratorof characters starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.static Iterator<java.lang.Double>rangeBy(double from, double toExclusive, double step)Creates anIteratorof double values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.static Iterator<java.lang.Integer>rangeBy(int from, int toExclusive, int step)Creates anIteratorof int values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.static Iterator<java.lang.Long>rangeBy(long from, long toExclusive, long step)Creates anIteratorof long values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.static Iterator<java.math.BigDecimal>rangeBy(java.math.BigDecimal from, java.math.BigDecimal toExclusive, java.math.BigDecimal step)Creates anIteratorofBigDecimalvalues starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.static Iterator<java.lang.Character>rangeClosed(char from, char toInclusive)Creates anIteratorof characters starting fromfrom(inclusive) up totoInclusive(inclusive).static Iterator<java.lang.Integer>rangeClosed(int from, int toInclusive)Creates anIteratorof int values starting fromfrom(inclusive) up totoInclusive(inclusive).static Iterator<java.lang.Long>rangeClosed(long from, long toInclusive)Creates anIteratorof long values starting fromfrom(inclusive) up totoInclusive(inclusive).static Iterator<java.lang.Character>rangeClosedBy(char from, char toInclusive, int step)Creates anIteratorof characters starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.static Iterator<java.lang.Double>rangeClosedBy(double from, double toInclusive, double step)Creates anIteratorof double values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.static Iterator<java.lang.Integer>rangeClosedBy(int from, int toInclusive, int step)Creates anIteratorof int values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.static Iterator<java.lang.Long>rangeClosedBy(long from, long toInclusive, long step)Creates anIteratorof long values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.default TreduceLeft(@NonNull java.util.function.BiFunction<? super T,? super T,? extends T> op)Reduces the elements of this Traversable from the left using the given binary operation.default TreduceRight(@NonNull java.util.function.BiFunction<? super T,? super T,? extends T> op)Reduces the elements of this Traversable from the right using the given binary operation.default Iterator<T>reject(@NonNull java.util.function.Predicate<? super T> predicate)Returns a new traversable containing only the elements that do not satisfy the given predicate.default Iterator<T>replace(T currentElement, T newElement)Replaces the first occurrence ofcurrentElementwithnewElement, if it exists.default Iterator<T>replaceAll(T currentElement, T newElement)Replaces all occurrences ofcurrentElementwithnewElement.default Iterator<T>retainAll(@NonNull java.lang.Iterable<? extends T> elements)Retains only the elements from this Traversable that are contained in the givenelements.default Traversable<T>scan(T zero, @NonNull java.util.function.BiFunction<? super T,? super T,? extends T> operation)Computes a prefix scan of the elements of this Traversable.default <U> Iterator<U>scanLeft(U zero, @NonNull java.util.function.BiFunction<? super U,? super T,? extends U> operation)Produces a collection containing cumulative results of applying the operator from left to right.default <U> Iterator<U>scanRight(U zero, @NonNull java.util.function.BiFunction<? super T,? super U,? extends U> operation)Produces a collection containing cumulative results of applying the operator from right to left.default Iterator<Seq<T>>slideBy(@NonNull java.util.function.Function<? super T,?> classifier)Partitions thisTraversableinto consecutive non-overlapping windows according to a classification function.default Iterator<Seq<T>>sliding(int size)Slides a window of a givensizeover thisTraversablewith a step size of 1.default Iterator<Seq<T>>sliding(int size, int step)Slides a window of a specificsizewith a givenstepover thisTraversable.default Tuple2<Iterator<T>,Iterator<T>>span(@NonNull java.util.function.Predicate<? super T> predicate)Splits thisTraversableinto a prefix and remainder according to the givenpredicate.default java.lang.StringstringPrefix()Returns the name of this Value type, which is used by toString().static <T> Iterator<T>tabulate(int n, java.util.function.Function<? super java.lang.Integer,? extends T> f)Returns anIteratorover a sequence ofnelements, where each element is computed by the given functionfapplied to its index.default Iterator<T>tail()Returns a newTraversablewithout its first element.default Option<Iterator<T>>tailOption()Returns a newTraversablewithout its first element as anOption.default Iterator<T>take(int n)Take the first n elements from this iterator.default Iterator<T>takeRight(int n)Returns the lastnelements of thisTraversable, or all elements ifnexceeds the length.default Iterator<T>takeUntil(@NonNull java.util.function.Predicate<? super T> predicate)Takes elements from thisTraversableuntil the given predicate holds for an element.default Iterator<T>takeWhile(@NonNull java.util.function.Predicate<? super T> predicate)Takes elements from thisTraversablewhile the given predicate holds.default <U> Utransform(java.util.function.Function<? super Iterator<T>,? extends U> f)Applies a transformation function to thisIteratorand returns the result.static <T> Iterator<T>unfold(T seed, java.util.function.Function<? super T,Option<Tuple2<? extends T,? extends T>>> f)Creates anIteratorby repeatedly applying a function to a seed value.static <T,U>
Iterator<U>unfoldLeft(T seed, java.util.function.Function<? super T,Option<Tuple2<? extends T,? extends U>>> f)Creates anIteratorby repeatedly applying a function to a seed value, generating elements in a left-to-right order.static <T,U>
Iterator<U>unfoldRight(T seed, java.util.function.Function<? super T,Option<Tuple2<? extends U,? extends T>>> f)Creates anIteratorby repeatedly applying a function to a seed value, generating elements in a right-to-left order.default <T1,T2>
Tuple2<Iterator<T1>,Iterator<T2>>unzip(@NonNull java.util.function.Function<? super T,Tuple2<? extends T1,? extends T2>> unzipper)Unzips the elements of thisTraversableby mapping each element to a pair and splitting them into two separateTraversablecollections.default <T1,T2,T3>
Tuple3<Iterator<T1>,Iterator<T2>,Iterator<T3>>unzip3(@NonNull java.util.function.Function<? super T,Tuple3<? extends T1,? extends T2,? extends T3>> unzipper)Unzips the elements of thisTraversableby mapping each element to a triple and splitting them into three separateTraversablecollections.default <U> Iterator<Tuple2<T,U>>zip(@NonNull java.lang.Iterable<? extends U> that)Returns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable.default <U> Iterator<Tuple2<T,U>>zipAll(@NonNull java.lang.Iterable<? extends U> that, T thisElem, U thatElem)Returns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable, filling in placeholder elements when one collection is shorter than the other.default <U,R>
Iterator<R>zipWith(@NonNull java.lang.Iterable<? extends U> that, java.util.function.BiFunction<? super T,? super U,? extends R> mapper)Returns aTraversableby combining elements of thisTraversablewith elements of anotherIterableusing a mapping function.default Iterator<Tuple2<T,java.lang.Integer>>zipWithIndex()Zips thisTraversablewith its indices, starting at 0.default <U> Iterator<U>zipWithIndex(@NonNull java.util.function.BiFunction<? super T,? super java.lang.Integer,? extends U> mapper)Zips thisTraversablewith its indices and maps the resulting pairs using the provided mapper.-
Methods inherited from interface io.vavr.collection.Foldable
fold, reduce, reduceOption
-
Methods inherited from interface io.vavr.collection.Traversable
arrangeBy, average, containsAll, count, equals, existsUnique, find, foldLeft, forEachWithIndex, hashCode, headOption, isDistinct, isOrdered, isSingleValued, lastOption, max, maxBy, maxBy, min, minBy, minBy, mkCharSeq, mkCharSeq, mkCharSeq, mkString, mkString, mkString, nonEmpty, product, reduceLeftOption, reduceRightOption, single, singleOption, size, spliterator, sum
-
Methods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, out, out, stderr, stdout, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
-
-
-
Method Detail
-
concat
@SafeVarargs static <T> Iterator<T> concat(java.lang.Iterable<? extends T>... iterables)
Creates anIteratorthat traverses the elements of the provided iterables in sequence, as if they were concatenated.- Type Parameters:
T- the element type- Parameters:
iterables- the source iterables- Returns:
- an iterator yielding the elements of each iterable in order
- Throws:
java.lang.NullPointerException- ifiterablesisnull
-
concat
static <T> Iterator<T> concat(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> iterables)
Creates anIteratorthat iterates over all elements of the supplied sequence of iterables, in order.- Type Parameters:
T- the element type- Parameters:
iterables- an iterable whose elements provide the individual iterables to traverse- Returns:
- an iterator yielding the concatenated contents of the nested iterables
- Throws:
java.lang.NullPointerException- ifiterablesisnull
-
empty
static <T> Iterator<T> empty()
Returns an emptyIterator.- Type Parameters:
T- the element type- Returns:
- an iterator with no elements
-
narrow
static <T> Iterator<T> narrow(Iterator<? extends T> iterator)
Narrows anIterator<? extends T>toIterator<T>using a type-safe cast. This is valid because the iterator is read-only with respect to element types.- Type Parameters:
T- the element type- Parameters:
iterator- the iterator to narrow- Returns:
- the same iterator, viewed as
Iterator<T> - Throws:
java.lang.NullPointerException- ifiteratorisnull
-
of
static <T> Iterator<T> of(T element)
Creates anIteratorthat yields exactly one element.- Type Parameters:
T- the element type- Parameters:
element- the single element- Returns:
- an iterator containing only
element
-
of
@SafeVarargs static <T> Iterator<T> of(T... elements)
Creates anIteratorthat iterates over the provided elements.- Type Parameters:
T- the element type- Parameters:
elements- zero or more elements- Returns:
- an iterator over the supplied elements
-
ofAll
static <T> Iterator<T> ofAll(java.lang.Iterable<? extends T> iterable)
Creates anIteratorfrom the providedIterable. This is a convenience method equivalent to callingIterator.ofAll(iterable.iterator()).- Type Parameters:
T- the element type- Parameters:
iterable- the source iterable- Returns:
- an iterator over the iterable's elements
- Throws:
java.lang.NullPointerException- ifiterableisnull
-
ofAll
static <T> Iterator<T> ofAll(java.util.Iterator<? extends T> iterator)
Creates anIteratorthat delegateshasNext()andnext()to the givenIterator.- Type Parameters:
T- the element type- Parameters:
iterator- the underlying iterator- Returns:
- an iterator that forwards calls to
iterator - Throws:
java.lang.NullPointerException- ifiteratorisnull
-
ofAll
static Iterator<java.lang.Boolean> ofAll(boolean... elements)
Creates anIteratorover the given boolean values.- Parameters:
elements- the boolean values- Returns:
- an iterator yielding the boxed values
- Throws:
java.lang.NullPointerException- ifelementsisnull
-
ofAll
static Iterator<java.lang.Byte> ofAll(byte... elements)
Creates anIteratorover the given byte values.- Parameters:
elements- the byte values- Returns:
- an iterator yielding the boxed
Bytevalues - Throws:
java.lang.NullPointerException- ifelementsisnull
-
ofAll
static Iterator<java.lang.Character> ofAll(char... elements)
Creates anIteratorover the given char values.- Parameters:
elements- the char values- Returns:
- an iterator yielding the boxed
Charactervalues - Throws:
java.lang.NullPointerException- ifelementsisnull
-
ofAll
static Iterator<java.lang.Double> ofAll(double... elements)
Creates anIteratorover the given double values.- Parameters:
elements- the double values- Returns:
- an iterator yielding the boxed
Doublevalues - Throws:
java.lang.NullPointerException- ifelementsisnull
-
ofAll
static Iterator<java.lang.Float> ofAll(float... elements)
Creates anIteratorover the given float values.- Parameters:
elements- the float values- Returns:
- an iterator yielding the boxed
Floatvalues - Throws:
java.lang.NullPointerException- ifelementsisnull
-
ofAll
static Iterator<java.lang.Integer> ofAll(int... elements)
Creates anIteratorover the given int values.- Parameters:
elements- the int values- Returns:
- an iterator yielding the boxed
Integervalues - Throws:
java.lang.NullPointerException- ifelementsisnull
-
ofAll
static Iterator<java.lang.Long> ofAll(long... elements)
Creates anIteratorover the given long values.- Parameters:
elements- the long values- Returns:
- an iterator yielding the boxed
Longvalues - Throws:
java.lang.NullPointerException- ifelementsisnull
-
ofAll
static Iterator<java.lang.Short> ofAll(short... elements)
Creates anIteratorover the given short values.- Parameters:
elements- the short values- Returns:
- an iterator yielding the boxed
Shortvalues - Throws:
java.lang.NullPointerException- ifelementsisnull
-
tabulate
static <T> Iterator<T> tabulate(int n, java.util.function.Function<? super java.lang.Integer,? extends T> f)
Returns anIteratorover a sequence ofnelements, where each element is computed by the given functionfapplied to its index.The resulting sequence is
f(0), f(1), ..., f(n - 1).- Type Parameters:
T- the element type- Parameters:
n- the number of elementsf- the function computing element values- Returns:
- an iterator over the computed elements
- Throws:
java.lang.NullPointerException- iffisnull
-
fill
static <T> Iterator<T> fill(int n, java.util.function.Supplier<? extends T> s)
Returns anIteratorover a sequence ofnelements supplied by the givenSupplier.Each element is obtained by invoking
s.get().- Type Parameters:
T- the element type- Parameters:
n- the number of elementss- the supplier providing element values- Returns:
- an iterator over the supplied elements
- Throws:
java.lang.NullPointerException- ifsisnull
-
fill
static <T> Iterator<T> fill(int n, T element)
Returns anIteratorcontaining the givenelementrepeatedntimes.- Type Parameters:
T- the element type- Parameters:
n- the number of repetitionselement- the element to repeat- Returns:
- an iterator over
noccurrences ofelement
-
range
static Iterator<java.lang.Character> range(char from, char toExclusive)
Creates anIteratorof characters starting fromfrom(inclusive) up totoExclusive(exclusive).Examples:
Iterator.range('a', 'c') // yields 'a', 'b' Iterator.range('c', 'a') // yields no elements- Parameters:
from- the first character (inclusive)toExclusive- the end character (exclusive)- Returns:
- an iterator over the specified character range, or empty if
from >= toExclusive
-
rangeBy
static Iterator<java.lang.Character> rangeBy(char from, char toExclusive, int step)
Creates anIteratorof characters starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.Examples:
Iterator.rangeBy('a', 'c', 1) // yields 'a', 'b' Iterator.rangeBy('a', 'd', 2) // yields 'a', 'c' Iterator.rangeBy('d', 'a', -2) // yields 'd', 'b' Iterator.rangeBy('d', 'a', 2) // yields no elements- Parameters:
from- the first character (inclusive)toExclusive- the end character (exclusive) - successor of the last character ifstep > 0, or predecessor ifstep < 0step- the increment between characters; must not be zero- Returns:
- an iterator over the specified character range, or empty if the step direction
does not match the direction from
fromtotoExclusive - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeBy
@GwtIncompatible("BigDecimalHelper is GwtIncompatible") static Iterator<java.lang.Double> rangeBy(double from, double toExclusive, double step)
Creates anIteratorof double values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.Examples:
Iterator.rangeBy(1.0, 3.0, 1.0) // yields 1.0, 2.0 Iterator.rangeBy(1.0, 4.0, 2.0) // yields 1.0, 3.0 Iterator.rangeBy(4.0, 1.0, -2.0) // yields 4.0, 2.0 Iterator.rangeBy(4.0, 1.0, 2.0) // yields no elements
- Parameters:
from- the first number (inclusive)toExclusive- the end number (exclusive)step- the increment; must not be zero- Returns:
- an iterator over the specified range, or empty if the step direction does not match the
direction from
fromtotoExclusive, or iffrom == toExclusive - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeBy
static Iterator<java.math.BigDecimal> rangeBy(java.math.BigDecimal from, java.math.BigDecimal toExclusive, java.math.BigDecimal step)
Creates anIteratorofBigDecimalvalues starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.This method provides precise decimal arithmetic suitable for financial calculations and other scenarios where exact decimal representation is required.
Examples:
Iterator.rangeBy(new BigDecimal("1.0"), new BigDecimal("3.0"), new BigDecimal("1.0")) // yields 1.0, 2.0 Iterator.rangeBy(new BigDecimal("1.0"), new BigDecimal("4.0"), new BigDecimal("2.0")) // yields 1.0, 3.0 Iterator.rangeBy(new BigDecimal("4.0"), new BigDecimal("1.0"), new BigDecimal("-2.0")) // yields 4.0, 2.0 Iterator.rangeBy(new BigDecimal("4.0"), new BigDecimal("1.0"), new BigDecimal("2.0")) // yields no elements- Parameters:
from- the first number (inclusive)toExclusive- the end number (exclusive)step- the increment; must not be zero- Returns:
- an iterator over the specified range, or empty if the step direction does not match the
direction from
fromtotoExclusive, or iffrom == toExclusive - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
range
static Iterator<java.lang.Integer> range(int from, int toExclusive)
Creates anIteratorof int values starting fromfrom(inclusive) up totoExclusive(exclusive).Examples:
Iterator.range(0, 0) // yields no elements Iterator.range(2, 0) // yields no elements Iterator.range(-2, 2) // yields -2, -1, 0, 1
- Parameters:
from- the first number (inclusive)toExclusive- the end number (exclusive)- Returns:
- an iterator over the specified range, or empty if
from >= toExclusive
-
rangeBy
static Iterator<java.lang.Integer> rangeBy(int from, int toExclusive, int step)
Creates anIteratorof int values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.Examples:
Iterator.rangeBy(1, 3, 1) // yields 1, 2 Iterator.rangeBy(1, 4, 2) // yields 1, 3 Iterator.rangeBy(4, 1, -2) // yields 4, 2 Iterator.rangeBy(4, 1, 2) // yields no elements
- Parameters:
from- the first number (inclusive)toExclusive- the end number (exclusive) — last number + 1 ifstep > 0, or last number - 1 ifstep < 0step- the increment; must not be zero- Returns:
- an iterator over the specified range, or empty if the step direction does not match the
direction from
fromtotoExclusive, or iffrom == toExclusive - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
range
static Iterator<java.lang.Long> range(long from, long toExclusive)
Creates anIteratorof long values starting fromfrom(inclusive) up totoExclusive(exclusive).Examples:
Iterator.range(0L, 0L) // yields no elements Iterator.range(2L, 0L) // yields no elements Iterator.range(-2L, 2L) // yields -2L, -1L, 0L, 1L
- Parameters:
from- the first number (inclusive)toExclusive- the end number (exclusive)- Returns:
- an iterator over the specified range, or empty if
from >= toExclusive
-
rangeBy
static Iterator<java.lang.Long> rangeBy(long from, long toExclusive, long step)
Creates anIteratorof long values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.Examples:
Iterator.rangeBy(1L, 3L, 1L) // yields 1L, 2L Iterator.rangeBy(1L, 4L, 2L) // yields 1L, 3L Iterator.rangeBy(4L, 1L, -2L) // yields 4L, 2L Iterator.rangeBy(4L, 1L, 2L) // yields no elements
- Parameters:
from- the first number (inclusive)toExclusive- the end number (exclusive) — last number + 1 ifstep > 0, or last number - 1 ifstep < 0step- the increment; must not be zero- Returns:
- an iterator over the specified range, or empty if the step direction does not match
the direction from
fromtotoExclusive, or iffrom == toExclusive - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeClosed
static Iterator<java.lang.Character> rangeClosed(char from, char toInclusive)
Creates anIteratorof characters starting fromfrom(inclusive) up totoInclusive(inclusive).Examples:
Iterator.rangeClosed('a', 'c') // yields 'a', 'b', 'c' Iterator.rangeClosed('c', 'a') // yields no elements- Parameters:
from- the first character (inclusive)toInclusive- the last character (inclusive)- Returns:
- an iterator over the specified character range, or empty if
from > toInclusive
-
rangeClosedBy
static Iterator<java.lang.Character> rangeClosedBy(char from, char toInclusive, int step)
Creates anIteratorof characters starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.Examples:
Iterator.rangeClosedBy('a', 'c', 1) // yields 'a', 'b', 'c' Iterator.rangeClosedBy('a', 'd', 2) // yields 'a', 'c' Iterator.rangeClosedBy('d', 'a', -2) // yields 'd', 'b' Iterator.rangeClosedBy('d', 'a', 2) // yields no elements- Parameters:
from- the first character (inclusive)toInclusive- the last character (inclusive)step- the increment; must not be zero- Returns:
- an iterator over the specified character range, or empty if the step
direction does not match the direction from
fromtotoInclusive - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeClosedBy
@GwtIncompatible static Iterator<java.lang.Double> rangeClosedBy(double from, double toInclusive, double step)
Creates anIteratorof double values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.Examples:
Iterator.rangeClosedBy(1.0, 3.0, 1.0) // yields 1.0, 2.0, 3.0 Iterator.rangeClosedBy(1.0, 4.0, 2.0) // yields 1.0, 3.0 Iterator.rangeClosedBy(4.0, 1.0, -2.0) // yields 4.0, 2.0 Iterator.rangeClosedBy(4.0, 1.0, 2.0) // yields no elements
- Parameters:
from- the first number (inclusive)toInclusive- the last number (inclusive)step- the increment; must not be zero- Returns:
- an iterator over the specified range, or empty if the step
direction does not match the direction from
fromtotoInclusive, or iffrom == toInclusiveit returns a singleton iterator - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeClosed
static Iterator<java.lang.Integer> rangeClosed(int from, int toInclusive)
Creates anIteratorof int values starting fromfrom(inclusive) up totoInclusive(inclusive).Examples:
Iterator.rangeClosed(0, 0) // yields 0 Iterator.rangeClosed(2, 0) // yields no elements Iterator.rangeClosed(-2, 2) // yields -2, -1, 0, 1, 2
- Parameters:
from- the first number (inclusive)toInclusive- the last number (inclusive)- Returns:
- an iterator over the specified range, or empty if
from > toInclusive
-
rangeClosedBy
static Iterator<java.lang.Integer> rangeClosedBy(int from, int toInclusive, int step)
Creates anIteratorof int values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.Examples:
Iterator.rangeClosedBy(1, 3, 1) // yields 1, 2, 3 Iterator.rangeClosedBy(1, 4, 2) // yields 1, 3 Iterator.rangeClosedBy(4, 1, -2) // yields 4, 2 Iterator.rangeClosedBy(4, 1, 2) // yields no elements
- Parameters:
from- the first number (inclusive)toInclusive- the last number (inclusive)step- the increment; must not be zero- Returns:
- an iterator over the specified range, or empty if the step
direction does not match the direction from
fromtotoInclusive, or iffrom == toInclusiveit returns a singleton iterator - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeClosed
static Iterator<java.lang.Long> rangeClosed(long from, long toInclusive)
Creates anIteratorof long values starting fromfrom(inclusive) up totoInclusive(inclusive).Examples:
Iterator.rangeClosed(0L, 0L) // yields 0L Iterator.rangeClosed(2L, 0L) // yields no elements Iterator.rangeClosed(-2L, 2L) // yields -2L, -1L, 0L, 1L, 2L
- Parameters:
from- the first number (inclusive)toInclusive- the last number (inclusive)- Returns:
- an iterator over the specified range, or empty if
from > toInclusive
-
rangeClosedBy
static Iterator<java.lang.Long> rangeClosedBy(long from, long toInclusive, long step)
Creates anIteratorof long values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.Examples:
Iterator.rangeClosedBy(1L, 3L, 1L) // yields 1L, 2L, 3L Iterator.rangeClosedBy(1L, 4L, 2L) // yields 1L, 3L Iterator.rangeClosedBy(4L, 1L, -2L) // yields 4L, 2L Iterator.rangeClosedBy(4L, 1L, 2L) // yields no elements
- Parameters:
from- the first number (inclusive)toInclusive- the last number (inclusive)step- the increment; must not be zero- Returns:
- an iterator over the specified range, or empty if the step
direction does not match the direction from
fromtotoInclusive, or iffrom == toInclusiveit returns a singleton iterator - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
from
static Iterator<java.lang.Integer> from(int value)
Returns an infiniteIteratorof int values starting fromvalue.The iterator wraps from
Integer.MAX_VALUEtoInteger.MIN_VALUE.- Parameters:
value- the starting int value- Returns:
- an iterator that endlessly yields consecutive int values starting from
value
-
from
static Iterator<java.lang.Integer> from(int value, int step)
Returns an infiniteIteratorof int values starting fromvalueand advancing by the specifiedstep.The iterator wraps from
Integer.MAX_VALUEtoInteger.MIN_VALUEif overflow occurs.- Parameters:
value- the starting int valuestep- the increment for each iteration- Returns:
- an iterator that endlessly yields consecutive int values starting from
value, spaced bystep
-
from
static Iterator<java.lang.Long> from(long value)
Returns an infiniteIteratorof long values starting fromvalue.The iterator wraps from
Long.MAX_VALUEtoLong.MIN_VALUEif overflow occurs.- Parameters:
value- the starting long value- Returns:
- an iterator that endlessly yields consecutive long values starting from
value
-
from
static Iterator<java.lang.Long> from(long value, long step)
Returns an infiniteIteratorof long values starting fromvalueand advancing by the specifiedstep.The iterator wraps from
Long.MAX_VALUEtoLong.MIN_VALUEif overflow occurs.- Parameters:
value- the starting long valuestep- the increment for each iteration- Returns:
- an iterator that endlessly yields consecutive long values starting from
value, spaced bystep
-
continually
static <T> Iterator<T> continually(java.util.function.Supplier<? extends T> supplier)
Returns an infiniteIteratorthat repeatedly generates values using the providedSupplier.- Type Parameters:
T- the type of values produced- Parameters:
supplier- the supplier providing iterator values; must not benull- Returns:
- an iterator that endlessly yields values from the supplier
- Throws:
java.lang.NullPointerException- ifsupplierisnull
-
iterate
static <T> Iterator<T> iterate(java.util.function.Supplier<? extends Option<? extends T>> supplier)
Creates anIteratorthat repeatedly invokes the givenSupplieras long as it returns aSomevalue, terminating when it returnsNone.- Type Parameters:
T- the type of values produced- Parameters:
supplier- the supplier providingOptionvalues; must not benull- Returns:
- an iterator yielding the values wrapped in
Some, stopping at the firstNone - Throws:
java.lang.NullPointerException- if the supplier produces anullvalue
-
iterate
static <T> Iterator<T> iterate(T seed, java.util.function.Function<? super T,? extends T> f)
Returns an infiniteIteratorthat generates values by repeatedly applying the given function to the previous value, starting withseed.Each call to
getNext()produces the next element by applyingfto the previous element.- Type Parameters:
T- the type of values produced- Parameters:
seed- the initial valuef- the function to compute the next value from the previous; must not benull- Returns:
- an iterator that endlessly yields values generated from
seedusingf - Throws:
java.lang.NullPointerException- iffisnull
-
continually
static <T> Iterator<T> continually(T t)
Returns an infiniteIteratorthat endlessly yields the given element.- Type Parameters:
T- the type of the element- Parameters:
t- the element to repeat- Returns:
- an iterator that repeatedly returns
t
-
collect
default <R> Iterator<R> collect(@NonNull PartialFunction<? super T,? extends R> partialFunction)
Description copied from interface:TraversableApplies aPartialFunctionto all elements that are defined for it and collects the results.For each element in iteration order, the function is first tested:
IfpartialFunction.isDefinedAt(element)true, the element is mapped to typeR:R newElement = partialFunction.apply(element)Note: If this
Traversableis ordered (i.e., extendsOrdered), the caller must ensure that the resulting elements are comparable (i.e., implementComparable).- Specified by:
collectin interfaceTraversable<T>- Type Parameters:
R- the type of elements in the resultingTraversable- Parameters:
partialFunction- a function that may not be defined for all elements of this traversable- Returns:
- a new
Traversablecontaining the results of applying the partial function
-
concat
default Iterator<T> concat(java.util.Iterator<? extends T> that)
Returns a newIteratorthat yields the elements of this iterator followed by all elements of the specified iterator.This method appends the elements from
thatto the end of this iterator, creating a concatenated sequence.Examples:
Iterator.of(1, 2).concat(Iterator.of(3, 4)) // yields 1, 2, 3, 4 Iterator.empty().concat(Iterator.of(1, 2)) // yields 1, 2 Iterator.of(1, 2).concat(Iterator.empty()) // yields 1, 2
- Parameters:
that- the iterator whose elements should be appended- Returns:
- a new iterator containing elements from both iterators
- Throws:
java.lang.NullPointerException- ifthatisnull
-
intersperse
default Iterator<T> intersperse(T element)
Returns a newIteratorwhere the specifiedelementis inserted between each element of this iterator.- Parameters:
element- the element to intersperse- Returns:
- an iterator with
elementinterleaved between the original elements
-
transform
default <U> U transform(java.util.function.Function<? super Iterator<T>,? extends U> f)
Applies a transformation function to thisIteratorand returns the result.- Type Parameters:
U- the type of the result- Parameters:
f- the function to transform this iterator; must not benull- Returns:
- the result of applying
fto this iterator - Throws:
java.lang.NullPointerException- iffisnull
-
zip
default <U> Iterator<Tuple2<T,U>> zip(@NonNull java.lang.Iterable<? extends U> that)
Description copied from interface:TraversableReturns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable. Pairing stops when either collection runs out of elements; any remaining elements in the longer collection are ignored.The length of the resulting
Traversableis the minimum of the lengths of thisTraversableandthat.- Specified by:
zipin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the second half of each pair- Parameters:
that- anIterableproviding the second element of each pair- Returns:
- a new
Traversablecontaining pairs of corresponding elements
-
zipWith
default <U,R> Iterator<R> zipWith(@NonNull java.lang.Iterable<? extends U> that, java.util.function.BiFunction<? super T,? super U,? extends R> mapper)
Description copied from interface:TraversableReturns aTraversableby combining elements of thisTraversablewith elements of anotherIterableusing a mapping function. Pairing stops when either collection runs out of elements.The length of the resulting
Traversableis the minimum of the lengths of thisTraversableandthat.- Specified by:
zipWithin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the second parameter of the mapperR- the type of elements in the resultingTraversable- Parameters:
that- anIterableproviding the second parameter of the mappermapper- a function that combines elements from this andthatinto a new element- Returns:
- a new
Traversablecontaining mapped elements
-
zipAll
default <U> Iterator<Tuple2<T,U>> zipAll(@NonNull java.lang.Iterable<? extends U> that, T thisElem, U thatElem)
Description copied from interface:TraversableReturns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable, filling in placeholder elements when one collection is shorter than the other.The length of the resulting
Traversableis the maximum of the lengths of thisTraversableandthat.If this
Traversableis shorter thanthat,thisElemis used as a filler. Conversely, ifthatis shorter,thatElemis used.- Specified by:
zipAllin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the second half of each pair- Parameters:
that- anIterableproviding the second element of each pairthisElem- the element used to fill missing values if thisTraversableis shorter thanthatthatElem- the element used to fill missing values ifthatis shorter than thisTraversable- Returns:
- a new
Traversablecontaining pairs of elements, including fillers as needed
-
zipWithIndex
default Iterator<Tuple2<T,java.lang.Integer>> zipWithIndex()
Description copied from interface:TraversableZips thisTraversablewith its indices, starting at 0.- Specified by:
zipWithIndexin interfaceTraversable<T>- Returns:
- a new
Traversablecontaining each element paired with its index
-
zipWithIndex
default <U> Iterator<U> zipWithIndex(@NonNull java.util.function.BiFunction<? super T,? super java.lang.Integer,? extends U> mapper)
Description copied from interface:TraversableZips thisTraversablewith its indices and maps the resulting pairs using the provided mapper.- Specified by:
zipWithIndexin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the resultingTraversable- Parameters:
mapper- a function mapping an element and its index to a new element- Returns:
- a new
Traversablecontaining the mapped elements
-
unzip
default <T1,T2> Tuple2<Iterator<T1>,Iterator<T2>> unzip(@NonNull java.util.function.Function<? super T,Tuple2<? extends T1,? extends T2>> unzipper)
Description copied from interface:TraversableUnzips the elements of thisTraversableby mapping each element to a pair and splitting them into two separateTraversablecollections.- Specified by:
unzipin interfaceTraversable<T>- Type Parameters:
T1- type of the first element in the resulting pairsT2- type of the second element in the resulting pairs- Parameters:
unzipper- a function that maps elements of thisTraversableto pairs- Returns:
- a
Tuple2containing twoTraversablecollections with the split elements
-
unzip3
default <T1,T2,T3> Tuple3<Iterator<T1>,Iterator<T2>,Iterator<T3>> unzip3(@NonNull java.util.function.Function<? super T,Tuple3<? extends T1,? extends T2,? extends T3>> unzipper)
Description copied from interface:TraversableUnzips the elements of thisTraversableby mapping each element to a triple and splitting them into three separateTraversablecollections.- Specified by:
unzip3in interfaceTraversable<T>- Type Parameters:
T1- type of the first element in the resulting triplesT2- type of the second element in the resulting triplesT3- type of the third element in the resulting triples- Parameters:
unzipper- a function that maps elements of thisTraversableto triples- Returns:
- a
Tuple3containing threeTraversablecollections with the split elements
-
unfold
static <T> Iterator<T> unfold(T seed, java.util.function.Function<? super T,Option<Tuple2<? extends T,? extends T>>> f)
Creates anIteratorby repeatedly applying a function to a seed value.The function takes the current seed and returns
Noneto signal the end of iteration, orSome<Tuple2>containing the next element to yield and the seed for the next step.Example:
Iterator.unfold(10, x -> x == 0 ? Option.none() : Option.of(new Tuple2<>(x-1, x))); // yields 1, 2, 3, 4, 5, 6, 7, 8, 9, 10- Type Parameters:
T- the type of the seed and produced elements- Parameters:
seed- the initial seed valuef- the function to produce the next element and seed; must not benull- Returns:
- an iterator producing the elements generated by repeatedly applying
f - Throws:
java.lang.NullPointerException- iffisnull
-
unfoldLeft
static <T,U> Iterator<U> unfoldLeft(T seed, java.util.function.Function<? super T,Option<Tuple2<? extends T,? extends U>>> f)
Creates anIteratorby repeatedly applying a function to a seed value, generating elements in a left-to-right order.The function receives the current seed and returns
Noneto signal the end of iteration, orSome<Tuple2>containing the next seed and the element to include in the iterator.Example:
Iterator.unfoldLeft(10, x -> x == 0 ? Option.none() : Option.of(new Tuple2<>(x-1, x))); // yields 1, 2, 3, 4, 5, 6, 7, 8, 9, 10- Type Parameters:
T- the type of the seedU- the type of the produced elements- Parameters:
seed- the initial seed valuef- the function to produce the next element and seed; must not benull- Returns:
- an iterator producing elements generated from the seed using
f - Throws:
java.lang.NullPointerException- iffisnull
-
unfoldRight
static <T,U> Iterator<U> unfoldRight(T seed, java.util.function.Function<? super T,Option<Tuple2<? extends U,? extends T>>> f)
Creates anIteratorby repeatedly applying a function to a seed value, generating elements in a right-to-left order.The function receives the current seed and returns
Noneto signal the end of iteration, orSome<Tuple2>containing the element to yield and the next seed for subsequent calls.Example:
Iterator.unfoldRight(10, x -> x == 0 ? Option.none() : Option.of(new Tuple2<>(x, x-1))); // yields 10, 9, 8, 7, 6, 5, 4, 3, 2, 1- Type Parameters:
T- the type of the seedU- the type of the produced elements- Parameters:
seed- the initial seed valuef- the function to produce the next element and seed; must not benull- Returns:
- an iterator producing elements generated from the seed using
f - Throws:
java.lang.NullPointerException- iffisnull
-
distinct
default Iterator<T> distinct()
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance with all duplicates removed. Element equality is determined usingequals.- Specified by:
distinctin interfaceTraversable<T>- Returns:
- a new
Traversablewithout duplicate elements
-
distinctBy
default Iterator<T> distinctBy(@NonNull java.util.Comparator<? super T> comparator)
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance without duplicates, as determined by the givencomparator.- Specified by:
distinctByin interfaceTraversable<T>- Parameters:
comparator- a comparator used to determine equality of elements- Returns:
- a new
Traversablewith duplicates removed
-
distinctBy
default <U> Iterator<T> distinctBy(@NonNull java.util.function.Function<? super T,? extends U> keyExtractor)
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance without duplicates, based on keys extracted from elements usingkeyExtractor.The first occurrence of each key is retained in the resulting sequence.
- Specified by:
distinctByin interfaceTraversable<T>- Type Parameters:
U- the type of key- Parameters:
keyExtractor- a function to extract keys for determining uniqueness- Returns:
- a new
Traversablewith duplicates removed based on keys
-
distinctByKeepLast
default Iterator<T> distinctByKeepLast(java.util.Comparator<? super T> comparator)
Returns a newIteratorcontaining the elements of this instance without duplicates, keeping the last occurrence of each duplicate element, as determined by the givencomparator.When multiple elements are considered equal according to the comparator, only the last occurrence in the sequence is retained.
Examples:
Iterator.of(1, 2, 2, 3, 1).distinctByKeepLast(Comparator.naturalOrder()) // yields 2, 3, 1
- Parameters:
comparator- the comparator used to determine equality- Returns:
- a new iterator containing distinct elements, keeping the last occurrence of duplicates
- Throws:
java.lang.NullPointerException- ifcomparatorisnull
-
distinctByKeepLast
default <U> Iterator<T> distinctByKeepLast(java.util.function.Function<? super T,? extends U> keyExtractor)
Returns a newIteratorcontaining the elements of this instance without duplicates, keeping the last occurrence of each duplicate element, based on keys extracted from elements usingkeyExtractor.When multiple elements have the same extracted key, only the last occurrence in the sequence is retained.
Examples:
Iterator.of("a", "ab", "abc", "b").distinctByKeepLast(String::length) // yields "abc", "b"- Type Parameters:
U- the type of the extracted key- Parameters:
keyExtractor- function used to extract the key from elements- Returns:
- a new iterator containing distinct elements, keeping the last occurrence of duplicates
- Throws:
java.lang.NullPointerException- ifkeyExtractorisnull
-
drop
default Iterator<T> drop(int n)
Removes up to n elements from this iterator.- Specified by:
dropin interfaceTraversable<T>- Parameters:
n- A number- Returns:
- The empty iterator, if
n <= 0or this is empty, otherwise a new iterator without the first n elements.
-
dropRight
default Iterator<T> dropRight(int n)
Description copied from interface:TraversableReturns a newTraversablewithout the lastnelements, or an empty instance if this contains fewer thannelements.- Specified by:
dropRightin interfaceTraversable<T>- Parameters:
n- the number of elements to drop from the end- Returns:
- a new instance excluding the last
nelements
-
dropUntil
default Iterator<T> dropUntil(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableReturns a newTraversablestarting from the first element that satisfies the givenpredicate, dropping all preceding elements.- Specified by:
dropUntilin interfaceTraversable<T>- Parameters:
predicate- a condition tested on each element- Returns:
- a new instance starting from the first element matching the predicate
-
dropWhile
default Iterator<T> dropWhile(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableReturns a newTraversablestarting from the first element that does not satisfy the givenpredicate, dropping all preceding elements.This is equivalent to
dropUntil(predicate.negate()), which is useful for method references that cannot be negated directly.- Specified by:
dropWhilein interfaceTraversable<T>- Parameters:
predicate- a condition tested on each element- Returns:
- a new instance starting from the first element not matching the predicate
-
filter
default Iterator<T> filter(@NonNull java.util.function.Predicate<? super T> predicate)
Returns an Iterator that contains elements that satisfy the givenpredicate.- Specified by:
filterin interfaceTraversable<T>- Parameters:
predicate- A predicate- Returns:
- A new Iterator
-
reject
default Iterator<T> reject(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableReturns a new traversable containing only the elements that do not satisfy the given predicate.This is equivalent to
filter(predicate.negate()).- Specified by:
rejectin interfaceTraversable<T>- Parameters:
predicate- the condition to test elements- Returns:
- a traversable with elements not matching the predicate
-
findLast
default Option<T> findLast(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableReturns the last element that satisfies the given predicate.Equivalent to
reverse().find(predicate)but potentially more efficient.- Specified by:
findLastin interfaceTraversable<T>- Parameters:
predicate- the condition to test elements- Returns:
Some(element)if a matching element is found, otherwiseNone; the element may benull
-
flatMap
default <U> Iterator<U> flatMap(@NonNull java.util.function.Function<? super T,? extends java.lang.Iterable<? extends U>> mapper)
FlatMaps the elements of this Iterator to Iterables, which are iterated in the order of occurrence.- Specified by:
flatMapin interfaceTraversable<T>- Type Parameters:
U- Component type- Parameters:
mapper- A mapper- Returns:
- A new Iterable
-
foldRight
default <U> U foldRight(U zero, @NonNull java.util.function.BiFunction<? super T,? super U,? extends U> f)Description copied from interface:FoldableFolds the elements of this structure from the right, starting with the givenzerovalue and successively applying thecombinefunction to each element.Folding from the right means that elements are combined starting from the last element and associating each step with the accumulated result so far.
Example:
// Result: "!cba" List.of("a", "b", "c").foldRight("!", (x, acc) -> acc + x);- Specified by:
foldRightin interfaceFoldable<T>- Specified by:
foldRightin interfaceTraversable<T>- Type Parameters:
U- the type of the accumulated result- Parameters:
zero- the initial value to start folding withf- a function that combines the next element and the accumulated value- Returns:
- the folded result
-
get
default T get()
Description copied from interface:TraversableReturns the first element of thisTraversablein iteration order.
-
groupBy
default <C> Map<C,Iterator<T>> groupBy(@NonNull java.util.function.Function<? super T,? extends C> classifier)
Description copied from interface:TraversableGroups elements of thisTraversablebased on a classifier function.- Specified by:
groupByin interfaceTraversable<T>- Type Parameters:
C- The type of the group keys- Parameters:
classifier- A function that assigns each element to a group- Returns:
- A map where each key corresponds to a group of elements
- See Also:
Traversable.arrangeBy(Function)
-
grouped
default Iterator<Seq<T>> grouped(int size)
Description copied from interface:TraversableSplits thisTraversableinto consecutive blocks of the given size.Let
lengthbe the number of elements in thisTraversable:- If empty, the resulting
Iteratoris empty. - If
size <= length, the resultingIteratorcontainslength / sizeblocks of sizesizeand possibly a final smaller block of sizelength % size. - If
size > length, the resultingIteratorcontains a single block of sizelength.
Examples:
[].grouped(1) = [] [].grouped(0) throws [].grouped(-1) throws [1,2,3,4].grouped(2) = [[1,2],[3,4]] [1,2,3,4,5].grouped(2) = [[1,2],[3,4],[5]] [1,2,3,4].grouped(5) = [[1,2,3,4]]Note:
grouped(size)is equivalent tosliding(size, size).- Specified by:
groupedin interfaceTraversable<T>- Parameters:
size- the block size; must be positive- Returns:
- an
Iteratorover blocks of elements
- If empty, the resulting
-
hasDefiniteSize
default boolean hasDefiniteSize()
Description copied from interface:TraversableIndicates whether thisTraversablehas a known finite size.This should typically be implemented by concrete classes, not interfaces.
- Specified by:
hasDefiniteSizein interfaceTraversable<T>- Returns:
trueif the number of elements is finite and known,falseotherwise.
-
head
default T head()
Description copied from interface:TraversableReturns the first element of this non-emptyTraversable.- Specified by:
headin interfaceTraversable<T>- Returns:
- the first element
-
init
default Iterator<T> init()
Description copied from interface:TraversableReturns all elements of this Traversable except the last one.This is the dual of
Traversable.tail().- Specified by:
initin interfaceTraversable<T>- Returns:
- a new instance containing all elements except the last
-
initOption
default Option<Iterator<T>> initOption()
Description copied from interface:TraversableReturns all elements of this Traversable except the last one, wrapped in anOption.This is the dual of
Traversable.tailOption().- Specified by:
initOptionin interfaceTraversable<T>- Returns:
Some(traversable)if non-empty, orNoneif this Traversable is empty
-
isAsync
default boolean isAsync()
AnIteratoris computed synchronously.
-
isEmpty
default boolean isEmpty()
Description copied from interface:TraversableChecks if this Traversable contains no elements.
-
isLazy
default boolean isLazy()
AnIteratoris computed lazily.
-
isTraversableAgain
default boolean isTraversableAgain()
Description copied from interface:TraversableChecks if this Traversable can be traversed multiple times without side effects.Implementations should provide the correct behavior; this is not meant for interfaces alone.
- Specified by:
isTraversableAgainin interfaceTraversable<T>- Returns:
trueif this Traversable is guaranteed to be repeatably traversable,falseotherwise
-
isSequential
default boolean isSequential()
Description copied from interface:TraversableIndicates whether the elements of this Traversable appear in encounter (insertion) order.- Specified by:
isSequentialin interfaceTraversable<T>- Returns:
trueif insertion order is preserved,falseotherwise
-
iterator
default @NonNull Iterator<T> iterator()
Description copied from interface:TraversableReturns an iterator over the elements of this Traversable, implemented viaTraversable.head()andTraversable.tail(). Subclasses may override for a more efficient implementation.
-
last
default T last()
Description copied from interface:TraversableReturns the last element of this Traversable.- Specified by:
lastin interfaceTraversable<T>- Returns:
- the last element
-
length
default int length()
Description copied from interface:TraversableReturns the number of elements in this Traversable.Equivalent to
Traversable.size().- Specified by:
lengthin interfaceTraversable<T>- Returns:
- the number of elements
-
map
default <U> Iterator<U> map(@NonNull java.util.function.Function<? super T,? extends U> mapper)
Maps the elements of this Iterator lazily using the givenmapper.
-
mapTo
default <U> Iterator<U> mapTo(U value)
Description copied from interface:ValueMaps the underlying value to another fixed value.
-
mapToVoid
default Iterator<java.lang.Void> mapToVoid()
Description copied from interface:ValueMaps the underlying value to Void
-
orElse
default Iterator<T> orElse(java.lang.Iterable<? extends T> other)
Description copied from interface:TraversableReturns thisTraversableif it is non-empty; otherwise, returns the given alternative.- Specified by:
orElsein interfaceTraversable<T>- Parameters:
other- an alternativeTraversableto return if this is empty- Returns:
- this
Traversableif non-empty, otherwiseother
-
orElse
default Iterator<T> orElse(@NonNull java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Description copied from interface:TraversableReturns thisTraversableif it is non-empty; otherwise, returns the result of evaluating the given supplier.- Specified by:
orElsein interfaceTraversable<T>- Parameters:
supplier- a supplier of an alternativeTraversableif this is empty- Returns:
- this
Traversableif non-empty, otherwise the result ofsupplier.get()
-
partition
default Tuple2<Iterator<T>,Iterator<T>> partition(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableSplits thisTraversableinto two partitions according to a predicate.The first partition contains all elements that satisfy the predicate, and the second contains all elements that do not. The original iteration order is preserved.
- Specified by:
partitionin interfaceTraversable<T>- Parameters:
predicate- a predicate used to classify elements- Returns:
- a
Tuple2containing the two resultingTraversableinstances
-
peek
default Iterator<T> peek(@NonNull java.util.function.Consumer<? super T> action)
Description copied from interface:ValuePerforms the givenactionon the first element if this is an eager implementation. Performs the givenactionon all elements (the first immediately, successive deferred), if this is a lazy implementation.
-
reduceLeft
default T reduceLeft(@NonNull java.util.function.BiFunction<? super T,? super T,? extends T> op)
Description copied from interface:TraversableReduces the elements of this Traversable from the left using the given binary operation.- Specified by:
reduceLeftin interfaceFoldable<T>- Specified by:
reduceLeftin interfaceTraversable<T>- Parameters:
op- A binary operation combining two elements of type T- Returns:
- the result of the reduction
-
reduceRight
default T reduceRight(@NonNull java.util.function.BiFunction<? super T,? super T,? extends T> op)
Description copied from interface:TraversableReduces the elements of this Traversable from the right using the given binary operation.- Specified by:
reduceRightin interfaceFoldable<T>- Specified by:
reduceRightin interfaceTraversable<T>- Parameters:
op- A binary operation combining two elements of type T- Returns:
- the result of the reduction
-
replace
default Iterator<T> replace(T currentElement, T newElement)
Description copied from interface:TraversableReplaces the first occurrence ofcurrentElementwithnewElement, if it exists.- Specified by:
replacein interfaceTraversable<T>- Parameters:
currentElement- the element to be replacednewElement- the replacement element- Returns:
- a new Traversable with the first occurrence of
currentElementreplaced bynewElement
-
replaceAll
default Iterator<T> replaceAll(T currentElement, T newElement)
Description copied from interface:TraversableReplaces all occurrences ofcurrentElementwithnewElement.- Specified by:
replaceAllin interfaceTraversable<T>- Parameters:
currentElement- the element to be replacednewElement- the replacement element- Returns:
- a new Traversable with all occurrences of
currentElementreplaced bynewElement
-
retainAll
default Iterator<T> retainAll(@NonNull java.lang.Iterable<? extends T> elements)
Description copied from interface:TraversableRetains only the elements from this Traversable that are contained in the givenelements.- Specified by:
retainAllin interfaceTraversable<T>- Parameters:
elements- the elements to keep- Returns:
- a new Traversable containing only the elements present in
elements, in their original order
-
scan
default Traversable<T> scan(T zero, @NonNull java.util.function.BiFunction<? super T,? super T,? extends T> operation)
Description copied from interface:TraversableComputes a prefix scan of the elements of this Traversable.The neutral element
zeromay be applied more than once.- Specified by:
scanin interfaceTraversable<T>- Parameters:
zero- the neutral element for the operatoroperation- an associative binary operator- Returns:
- a new Traversable containing the prefix scan of the elements
-
scanLeft
default <U> Iterator<U> scanLeft(U zero, @NonNull java.util.function.BiFunction<? super U,? super T,? extends U> operation)
Description copied from interface:TraversableProduces a collection containing cumulative results of applying the operator from left to right.Will not terminate for infinite collections. The results may vary across runs unless the collection is ordered.
- Specified by:
scanLeftin interfaceTraversable<T>- Type Parameters:
U- the type of the resulting elements- Parameters:
zero- the initial valueoperation- a binary operator applied to the intermediate result and each element- Returns:
- a new Traversable containing the cumulative results
-
scanRight
default <U> Iterator<U> scanRight(U zero, @NonNull java.util.function.BiFunction<? super T,? super U,? extends U> operation)
Description copied from interface:TraversableProduces a collection containing cumulative results of applying the operator from right to left.The head of the resulting collection is the last cumulative result. Will not terminate for infinite collections. Results may vary across runs unless the collection is ordered.
- Specified by:
scanRightin interfaceTraversable<T>- Type Parameters:
U- the type of the resulting elements- Parameters:
zero- the initial valueoperation- a binary operator applied to each element and the intermediate result- Returns:
- a new Traversable containing the cumulative results
-
slideBy
default Iterator<Seq<T>> slideBy(@NonNull java.util.function.Function<? super T,?> classifier)
Description copied from interface:TraversablePartitions thisTraversableinto consecutive non-overlapping windows according to a classification function.Each window contains elements with the same class, as determined by
classifier. Two consecutive elements belong to the same window only ifclassifierreturns equal values for both. Otherwise, the current window ends and a new window begins with the next element.Examples:
[].slideBy(Function.identity()) = [] [1,2,3,4,4,5].slideBy(Function.identity()) = [[1],[2],[3],[4,4],[5]] [1,2,3,10,12,5,7,20,29].slideBy(x -> x / 10) = [[1,2,3],[10,12],[5,7],[20,29]]- Specified by:
slideByin interfaceTraversable<T>- Parameters:
classifier- A function classifying elements into groups- Returns:
- An
Iteratorof windows (grouped elements)
-
sliding
default Iterator<Seq<T>> sliding(int size)
Description copied from interface:TraversableSlides a window of a givensizeover thisTraversablewith a step size of 1.This is equivalent to calling
Traversable.sliding(int, int)with a step size of 1.- Specified by:
slidingin interfaceTraversable<T>- Parameters:
size- a positive window size- Returns:
- An
Iteratorof windows, each containing up tosizeelements
-
sliding
default Iterator<Seq<T>> sliding(int size, int step)
Description copied from interface:TraversableSlides a window of a specificsizewith a givenstepover thisTraversable.Examples:
[].sliding(1, 1) = [] [1,2,3,4,5].sliding(2, 3) = [[1,2],[4,5]] [1,2,3,4,5].sliding(2, 4) = [[1,2],[5]] [1,2,3,4,5].sliding(2, 5) = [[1,2]] [1,2,3,4].sliding(5, 3) = [[1,2,3,4],[4]]- Specified by:
slidingin interfaceTraversable<T>- Parameters:
size- a positive window sizestep- a positive step size- Returns:
- an
Iteratorof windows with the given size and step
-
span
default Tuple2<Iterator<T>,Iterator<T>> span(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableSplits thisTraversableinto a prefix and remainder according to the givenpredicate.The first element of the returned
Tupleis the longest prefix of elements satisfyingpredicate, and the second element is the remaining elements.- Specified by:
spanin interfaceTraversable<T>- Parameters:
predicate- a predicate used to determine the prefix- Returns:
- a
Tuplecontaining the prefix and remainder
-
stringPrefix
default java.lang.String stringPrefix()
Description copied from interface:ValueReturns the name of this Value type, which is used by toString().- Specified by:
stringPrefixin interfaceValue<T>- Returns:
- This type name.
-
tail
default Iterator<T> tail()
Description copied from interface:TraversableReturns a newTraversablewithout its first element.- Specified by:
tailin interfaceTraversable<T>- Returns:
- a new
Traversablecontaining all elements except the first
-
tailOption
default Option<Iterator<T>> tailOption()
Description copied from interface:TraversableReturns a newTraversablewithout its first element as anOption.- Specified by:
tailOptionin interfaceTraversable<T>- Returns:
Some(traversable)if non-empty, otherwiseNone
-
take
default Iterator<T> take(int n)
Take the first n elements from this iterator.- Specified by:
takein interfaceTraversable<T>- Parameters:
n- A number- Returns:
- The empty iterator, if
n <= 0or this is empty, otherwise a new iterator without the first n elements.
-
takeRight
default Iterator<T> takeRight(int n)
Description copied from interface:TraversableReturns the lastnelements of thisTraversable, or all elements ifnexceeds the length.Equivalent to
sublist(max(0, length() - n), length()), but safe forn < 0orn > length().If
n < 0, an empty instance is returned. Ifn > length(), the full instance is returned.- Specified by:
takeRightin interfaceTraversable<T>- Parameters:
n- the number of elements to take from the end- Returns:
- a new
Traversablecontaining the lastnelements
-
takeUntil
default Iterator<T> takeUntil(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableTakes elements from thisTraversableuntil the given predicate holds for an element.Equivalent to
takeWhile(predicate.negate()), but useful when using method references that cannot be negated directly.- Specified by:
takeUntilin interfaceTraversable<T>- Parameters:
predicate- a condition tested sequentially on the elements- Returns:
- a new
Traversablecontaining all elements before the first one that satisfies the predicate
-
takeWhile
default Iterator<T> takeWhile(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableTakes elements from thisTraversablewhile the given predicate holds.- Specified by:
takeWhilein interfaceTraversable<T>- Parameters:
predicate- a condition tested sequentially on the elements- Returns:
- a new
Traversablecontaining all elements up to (but not including) the first one that does not satisfy the predicate
-
-