Interface Iterator<T>
- Type Parameters:
T- the element type
- All Superinterfaces:
Foldable<T>,Iterable<T>,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
java.util.Iterator designed 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,
and next(), which consumes and returns that element. If
hasNext() returns false, next() will throw
NoSuchElementException.
Caution: Methods other than hasNext() and
next() 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
Modifier and TypeMethodDescriptiondefault <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> Creates anIteratorthat iterates over all elements of the supplied sequence of iterables, in order.static <T> Iterator<T> Creates anIteratorthat traverses the elements of the provided iterables in sequence, as if they were concatenated.Returns a newIteratorthat yields the elements of this iterator followed by all elements of the specified iterator.static <T> Iterator<T> continually(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.distinct()Returns a newTraversablecontaining the elements of this instance with all duplicates removed.distinctBy(@NonNull Comparator<? super T> comparator) Returns a newTraversablecontaining the elements of this instance without duplicates, as determined by the givencomparator.distinctBy(@NonNull Function<? super T, ? extends U> keyExtractor) Returns a newTraversablecontaining the elements of this instance without duplicates, based on keys extracted from elements usingkeyExtractor.distinctByKeepLast(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.distinctByKeepLast(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.drop(int n) Removes up to n elements from this iterator.dropRight(int n) Returns a newTraversablewithout the lastnelements, or an empty instance if this contains fewer thannelements.Returns a newTraversablestarting from the first element that satisfies the givenpredicate, dropping all preceding elements.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> Returns anIteratorover a sequence ofnelements supplied by the givenSupplier.static <T> Iterator<T> fill(int n, T element) Returns anIteratorcontaining the givenelementrepeatedntimes.Returns an Iterator that contains elements that satisfy the givenpredicate.Returns the last element that satisfies the given predicate.default <U> Iterator<U> FlatMaps the elements of this Iterator to Iterables, which are iterated in the order of occurrence.default <U> UfoldRight(U zero, @NonNull 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.from(int value) Returns an infiniteIteratorof int values starting fromvalue.from(int value, int step) Returns an infiniteIteratorof int values starting fromvalueand advancing by the specifiedstep.from(long value) Returns an infiniteIteratorof long values starting fromvalue.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.Groups elements of thisTraversablebased on a classifier function.grouped(int size) Splits thisTraversableinto consecutive blocks of the given size.default booleanIndicates whether thisTraversablehas a known finite size.default Thead()Returns the first element of this non-emptyTraversable.init()Returns all elements of this Traversable except the last one.Returns all elements of this Traversable except the last one, wrapped in anOption.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 booleanIndicates whether the elements of this Traversable appear in encounter (insertion) order.default booleanChecks if this Traversable can be traversed multiple times without side effects.static <T> Iterator<T> Creates anIteratorthat repeatedly invokes the givenSupplieras long as it returns aSomevalue, terminating when it returnsNone.static <T> Iterator<T> Returns an infiniteIteratorthat generates values by repeatedly applying the given function to the previous value, starting withseed.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> 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.Maps the underlying value to Voidstatic <T> Iterator<T> 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.ofAll(boolean... elements) Creates anIteratorover the given boolean values.ofAll(byte... elements) Creates anIteratorover the given byte values.ofAll(char... elements) Creates anIteratorover the given char values.ofAll(double... elements) Creates anIteratorover the given double values.ofAll(float... elements) Creates anIteratorover the given float values.ofAll(int... elements) Creates anIteratorover the given int values.ofAll(long... elements) Creates anIteratorover the given long values.ofAll(short... elements) Creates anIteratorover the given short values.static <T> Iterator<T> Creates anIteratorfrom the providedIterable.static <T> Iterator<T> Returns thisTraversableif it is non-empty; otherwise, returns the given alternative.Returns thisTraversableif it is non-empty; otherwise, returns the result of evaluating the given supplier.Splits thisTraversableinto two partitions according to a predicate.Performs the givenactionon the first element if this is an eager implementation.range(char from, char toExclusive) Creates anIteratorof characters starting fromfrom(inclusive) up totoExclusive(exclusive).range(int from, int toExclusive) Creates anIteratorof int values starting fromfrom(inclusive) up totoExclusive(exclusive).range(long from, long toExclusive) Creates anIteratorof long values starting fromfrom(inclusive) up totoExclusive(exclusive).rangeBy(char from, char toExclusive, int step) Creates anIteratorof characters starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.rangeBy(double from, double toExclusive, double step) Creates anIteratorof double values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.rangeBy(int from, int toExclusive, int step) Creates anIteratorof int values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.rangeBy(long from, long toExclusive, long step) Creates anIteratorof long values starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.static Iterator<BigDecimal> rangeBy(BigDecimal from, BigDecimal toExclusive, BigDecimal step) Creates anIteratorofBigDecimalvalues starting fromfrom(inclusive) up totoExclusive(exclusive), advancing by the specifiedstep.rangeClosed(char from, char toInclusive) Creates anIteratorof characters starting fromfrom(inclusive) up totoInclusive(inclusive).rangeClosed(int from, int toInclusive) Creates anIteratorof int values starting fromfrom(inclusive) up totoInclusive(inclusive).rangeClosed(long from, long toInclusive) Creates anIteratorof long values starting fromfrom(inclusive) up totoInclusive(inclusive).rangeClosedBy(char from, char toInclusive, int step) Creates anIteratorof characters starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.rangeClosedBy(double from, double toInclusive, double step) Creates anIteratorof double values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.rangeClosedBy(int from, int toInclusive, int step) Creates anIteratorof int values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.rangeClosedBy(long from, long toInclusive, long step) Creates anIteratorof long values starting fromfrom(inclusive) up totoInclusive(inclusive), advancing by the specifiedstep.default TreduceLeft(@NonNull 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 BiFunction<? super T, ? super T, ? extends T> op) Reduces the elements of this Traversable from the right using the given binary operation.Returns a new traversable containing only the elements that do not satisfy the given predicate.Replaces the first occurrence ofcurrentElementwithnewElement, if it exists.replaceAll(T currentElement, T newElement) Replaces all occurrences ofcurrentElementwithnewElement.Retains only the elements from this Traversable that are contained in the givenelements.default Traversable<T> Computes a prefix scan of the elements of this Traversable.default <U> Iterator<U> scanLeft(U zero, @NonNull 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 BiFunction<? super T, ? super U, ? extends U> operation) Produces a collection containing cumulative results of applying the operator from right to left.Partitions thisTraversableinto consecutive non-overlapping windows according to a classification function.sliding(int size) Slides a window of a givensizeover thisTraversablewith a step size of 1.sliding(int size, int step) Slides a window of a specificsizewith a givenstepover thisTraversable.Splits thisTraversableinto a prefix and remainder according to the givenpredicate.default StringReturns the name of this Value type, which is used by toString().static <T> Iterator<T> Returns anIteratorover a sequence ofnelements, where each element is computed by the given functionfapplied to its index.tail()Returns a newTraversablewithout its first element.Returns a newTraversablewithout its first element as anOption.take(int n) Take the first n elements from this iterator.takeRight(int n) Returns the lastnelements of thisTraversable, or all elements ifnexceeds the length.Takes elements from thisTraversableuntil the given predicate holds for an element.Takes elements from thisTraversablewhile the given predicate holds.default <U> UApplies a transformation function to thisIteratorand returns the result.static <T> Iterator<T> Creates anIteratorby repeatedly applying a function to a seed value.static <T,U> Iterator <U> unfoldLeft(T seed, 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, 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.Unzips the elements of thisTraversableby mapping each element to a pair and splitting them into two separateTraversablecollections.Unzips the elements of thisTraversableby mapping each element to a triple and splitting them into three separateTraversablecollections.Returns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable.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 Iterable<? extends U> that, BiFunction<? super T, ? super U, ? extends R> mapper) Returns aTraversableby combining elements of thisTraversablewith elements of anotherIterableusing a mapping function.Zips thisTraversablewith its indices, starting at 0.default <U> Iterator<U> zipWithIndex(@NonNull BiFunction<? super T, ? super 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, reduceOptionMethods inherited from interface java.util.Iterator
forEachRemaining, hasNext, next, removeMethods 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, sumMethods 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 Details
-
concat
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:
NullPointerException- ifiterablesisnull
-
concat
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:
NullPointerException- ifiterablesisnull
-
empty
Returns an emptyIterator.- Type Parameters:
T- the element type- Returns:
- an iterator with no elements
-
narrow
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:
NullPointerException- ifiteratorisnull
-
of
Creates anIteratorthat yields exactly one element.- Type Parameters:
T- the element type- Parameters:
element- the single element- Returns:
- an iterator containing only
element
-
of
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
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:
NullPointerException- ifiterableisnull
-
ofAll
- Type Parameters:
T- the element type- Parameters:
iterator- the underlying iterator- Returns:
- an iterator that forwards calls to
iterator - Throws:
NullPointerException- ifiteratorisnull
-
ofAll
Creates anIteratorover the given boolean values.- Parameters:
elements- the boolean values- Returns:
- an iterator yielding the boxed values
- Throws:
NullPointerException- ifelementsisnull
-
ofAll
Creates anIteratorover the given byte values.- Parameters:
elements- the byte values- Returns:
- an iterator yielding the boxed
Bytevalues - Throws:
NullPointerException- ifelementsisnull
-
ofAll
Creates anIteratorover the given char values.- Parameters:
elements- the char values- Returns:
- an iterator yielding the boxed
Charactervalues - Throws:
NullPointerException- ifelementsisnull
-
ofAll
Creates anIteratorover the given double values.- Parameters:
elements- the double values- Returns:
- an iterator yielding the boxed
Doublevalues - Throws:
NullPointerException- ifelementsisnull
-
ofAll
Creates anIteratorover the given float values.- Parameters:
elements- the float values- Returns:
- an iterator yielding the boxed
Floatvalues - Throws:
NullPointerException- ifelementsisnull
-
ofAll
Creates anIteratorover the given int values.- Parameters:
elements- the int values- Returns:
- an iterator yielding the boxed
Integervalues - Throws:
NullPointerException- ifelementsisnull
-
ofAll
Creates anIteratorover the given long values.- Parameters:
elements- the long values- Returns:
- an iterator yielding the boxed
Longvalues - Throws:
NullPointerException- ifelementsisnull
-
ofAll
Creates anIteratorover the given short values.- Parameters:
elements- the short values- Returns:
- an iterator yielding the boxed
Shortvalues - Throws:
NullPointerException- ifelementsisnull
-
tabulate
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:
NullPointerException- iffisnull
-
fill
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:
NullPointerException- ifsisnull
-
fill
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
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
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:
IllegalArgumentException- ifstepis zero
-
rangeBy
@GwtIncompatible("BigDecimalHelper is GwtIncompatible") static Iterator<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:
IllegalArgumentException- ifstepis zero
-
rangeBy
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:
IllegalArgumentException- ifstepis zero
-
range
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
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:
IllegalArgumentException- ifstepis zero
-
range
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
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:
IllegalArgumentException- ifstepis zero
-
rangeClosed
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
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:
IllegalArgumentException- ifstepis zero
-
rangeClosedBy
@GwtIncompatible static Iterator<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:
IllegalArgumentException- ifstepis zero
-
rangeClosed
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
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:
IllegalArgumentException- ifstepis zero
-
rangeClosed
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
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:
IllegalArgumentException- ifstepis zero
-
from
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
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
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
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
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:
NullPointerException- ifsupplierisnull
-
iterate
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:
NullPointerException- if the supplier produces anullvalue
-
iterate
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:
NullPointerException- iffisnull
-
continually
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
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
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:
NullPointerException- ifthatisnull
-
intersperse
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
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:
NullPointerException- iffisnull
-
zip
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 Iterable<? extends U> that, 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 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
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 BiFunction<? super T, ? super 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 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, Tuple3<Iterator<T1>,T3> Iterator<T2>, unzip3Iterator<T3>> (@NonNull 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, 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:
NullPointerException- iffisnull
-
unfoldLeft
static <T,U> Iterator<U> unfoldLeft(T seed, 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:
NullPointerException- iffisnull
-
unfoldRight
static <T,U> Iterator<U> unfoldRight(T seed, 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:
NullPointerException- iffisnull
-
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
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
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
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:
NullPointerException- ifcomparatorisnull
-
distinctByKeepLast
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:
NullPointerException- ifkeyExtractorisnull
-
drop
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
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
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
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
Returns an Iterator that contains elements that satisfy the givenpredicate.- Specified by:
filterin interfaceTraversable<T>- Parameters:
predicate- A predicate- Returns:
- A new Iterator
-
reject
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
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 Function<? super T, ? extends 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
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
Description copied from interface:TraversableReturns the first element of thisTraversablein iteration order. -
groupBy
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:
-
grouped
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
Description copied from interface:TraversableReturns the first element of this non-emptyTraversable.- Specified by:
headin interfaceTraversable<T>- Returns:
- the first element
-
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
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
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
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
Maps the elements of this Iterator lazily using the givenmapper. -
mapTo
Description copied from interface:ValueMaps the underlying value to another fixed value. -
mapToVoid
Description copied from interface:ValueMaps the underlying value to Void -
orElse
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
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
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
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
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
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
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
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
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
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 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 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
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
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
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
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
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
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
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
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
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
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
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
-