Top Description Methods
io.vavr.collection

public Interface Traversable<T>

Additional top-level class in compilation unit: TraversableModule.

extends Foldable<T>, Value<T>
Known Direct Subinterfaces
io.vavr.collection.Tree, io.vavr.collection.Iterator, io.vavr.collection.Map, io.vavr.collection.Multimap, io.vavr.collection.Seq, io.vavr.collection.Set
Known Direct Implementers
io.vavr.collection.AbstractQueue
Type Parameters
<T>
Component type
Imports
io.vavr.PartialFunction, .Tuple2, .Tuple3, .Value, io.vavr.control.Option, java.math.BigDecimal, .BigInteger, java.util.*, java.util.function.*, java.util.stream.DoubleStream

An interface for inherently recursive, multi-valued data structures. The order of elements is determined by Iterable#iterator(), which may vary each time it is called.

Basic operations:

Iteration: Numeric operations: Reduction/Folding: Selection: Tests: Transformation:
Author
Daniel Dietrich and others

Method Summary

Modifier and TypeMethod and Description
public default <
key class type
K
>
Option<Map<K, T>>

Returns:

A Map containing the elements arranged by their keys.
arrangeBy
(Function<? super T, ? extends K>
A function which extracts a key from elements
getKey
)

Matches each element with a unique key that you extract from it.

public default Option<Double>

Returns:

Some(average) or None, if there are no elements
average
()

Calculates the average of this elements, assuming that the element type is Number.

public <
The new element type
R
>
Traversable<R>

Returns:

A new Traversable instance containing elements of type R
collect
(PartialFunction<? super T, ? extends R>
A function that is not necessarily defined of all elements of this traversable.
partialFunction
)

Collects all elements that are in the domain of the given partialFunction by mapping the elements to type R.

public default boolean

Returns:

true, if this List contains all given elements, false otherwise.
containsAll
(Iterable<? extends T>
A List of values of type T.
elements
)

Tests if this Traversable contains all given elements.

public default int

Returns:

A number >= 0
count
(Predicate<? super T>
A predicate
predicate
)

Counts the elements which satisfy the given predicate.

public Traversable<T>

Returns:

a new Traversable containing this elements without duplicates
distinct
()

Returns a new version of this which contains no duplicates.

public Traversable<T>

Returns:

a new Traversable containing this elements without duplicates
distinctBy
(Comparator<? super T>
A comparator
comparator
)

Returns a new version of this which contains no duplicates.

public <
key type
U
>
Traversable<T>

Returns:

a new Traversable containing this elements without duplicates
distinctBy
(Function<? super T, ? extends U>
A key extractor
keyExtractor
)

Returns a new version of this which contains no duplicates.

public Traversable<T>

Returns:

a new instance consisting of all elements of this except the first n ones, or else the empty instance, if this has less than n elements.
drop
(int
The number of elements to drop.
n
)

Drops the first n elements of this or all elements, if this length < n.

public Traversable<T>

Returns:

a new instance consisting of all elements of this except the last n ones, or else the empty instance, if this has less than n elements.
dropRight
(int
The number of elements to drop.
n
)

Drops the last n elements of this or all elements, if this length < n.

public Traversable<T>

Returns:

a new instance consisting of all elements starting from the first one which does satisfy the given predicate.
dropUntil
(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Drops elements until the predicate holds for the current element.

public Traversable<T>

Returns:

a new instance consisting of all elements starting from the first one which does not satisfy the given predicate.
dropWhile
(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Drops elements while the predicate holds for the current element.

public boolean

Returns:

true, if this collection equals the given object according to the rules described above, false otherwise.
equals
(Object
an object, may be null
obj
)

Redeclares io.vavr.Value.equals.

In Vavr there are four basic classes of collections:

  • Seq (sequential elements)
  • Set (distinct elements)
  • Map (indexed elements)
  • Multimap (indexed collections)
public default boolean

Returns:

true, if predicate holds for a unique element, false otherwise
existsUnique
(Predicate<? super T>
A Predicate
predicate
)

Checks, if a unique elements exists such that the predicate holds.

public Traversable<T>

Returns:

a new traversable
filter
(Predicate<? super T>
A predicate
predicate
)

Returns a new traversable consisting of all elements which satisfy the given predicate.

public default Option<T>

Returns:

Some(element) or None, where element may be null (i.e. List.of(null).find(e -> e == null)).
find
(Predicate<? super T>
A predicate.
predicate
)

Returns the first element of this which satisfies the given predicate.

public default Option<T>

Returns:

Some(element) or None, where element may be null (i.e. List.of(null).find(e -> e == null)).
findLast
(Predicate<? super T>
A predicate.
predicate
)

Returns the last element of this which satisfies the given predicate.

public <
The resulting component type.
U
>
Traversable<U>

Returns:

A new Traversable instance.
flatMap
(Function<? super T, ? extends Iterable<? extends U>>
A mapper
mapper
)

FlatMaps this Traversable.

public default <U> U
foldLeft(U
A zero element to start with.
zero
,
BiFunction<? super U, ? super T, ? extends U>
A function which combines elements.
f
)

Implements io.vavr.collection.Foldable.foldLeft.

Folds this elements from the left, starting with zero and successively calling combine.

public <U> U
foldRight(U
A zero element to start with.
zero
,
BiFunction<? super T, ? super U, ? extends U>
A function which combines elements.
f
)

Redeclares io.vavr.collection.Foldable.foldRight.

Folds this elements from the right, starting with zero and successively calling combine.

public default void
forEachWithIndex(ObjIntConsumer<? super T> action)

Performs an action on each element.

public default T

Returns:

the first value
get
()

Implements io.vavr.Value.get.

Gets the first value in iteration order if this Traversable is not empty, otherwise throws.

public <
classified class type
C
>
Map<C, ? extends Traversable<T>>

Returns:

A Map containing the grouped elements
groupBy
(Function<? super T, ? extends C>
A function which classifies elements into classes
classifier
)

Groups this elements by classifying the elements.

public Iterator<? extends Traversable<T>>

Returns:

A new Iterator of grouped blocks of the given size
grouped
(int
a positive block size
size
)

Groups this Traversable into fixed size blocks.

public boolean

Returns:

true, if this Traversable is known to have a finite size, false otherwise.
hasDefiniteSize
()

Checks if this Traversable is known to have a finite size.

public int

Returns:

The hash code of this collection
hashCode
()

Redeclares io.vavr.Value.hashCode.

Returns the hash code of this collection.

public T

Returns:

The first element of this Traversable.
head
()

Returns the first element of a non-empty Traversable.

public default Option<T>

Returns:

Some(element) or None if this is empty.
headOption
()

Returns the first element of a non-empty Traversable as Option.

public Traversable<T>

Returns:

a new instance containing all elements except the last.
init
()

Dual of tail(), returning all elements except the last.

public default Option<? extends Traversable<T>>

Returns:

Some(traversable) or None if this is empty.
initOption
()

Dual of tailOption(), returning all elements except the last as Option.

public default boolean

Returns:

true if this Traversable may consist of distinct elements only, false otherwise.
isDistinct
()

Checks if this Traversable may consist of distinct elements only.

public default boolean

Returns:

true, if this Traversable contains no elements, false otherwise.
isEmpty
()

Implements io.vavr.Value.isEmpty.

Checks if this Traversable is empty.

public default boolean

Returns:

true, if this Traversable is ordered, false otherwise.
isOrdered
()

Checks if this Traversable is ordered

public default boolean

Returns:

true, if the insertion order of elements is preserved, false otherwise.
isSequential
()

Checks if the elements of this Traversable appear in encounter order.

public default boolean

Returns:

false
isSingleValued
()

Implements io.vavr.Value.isSingleValued.

Each of Vavr's collections may contain more than one element.

public boolean

Returns:

true, if this Traversable is known to be traversable repeatedly, false otherwise.
isTraversableAgain
()

Checks if this Traversable can be repeatedly traversed.

public default Iterator<T>

Returns:

A new Iterator of this Traversable elements.
iterator
()

Implements io.vavr.Value.iterator.

An iterator by means of head() and tail().

public T

Returns:

the last element.
last
()

Dual of head(), returning the last element.

public default Option<T>

Returns:

Some(element) or None if this is empty.
lastOption
()

Dual of headOption(), returning the last element as Option.

public int

Returns:

the number of elements
length
()

Computes the number of elements of this Traversable.

public <
Component type of the target Traversable
U
>
Traversable<U>

Returns:

a mapped Traversable
map
(Function<? super T, ? extends U>
A mapper.
mapper
)

Redeclares io.vavr.Value.map.

Maps the elements of this Traversable to elements of a new type preserving their order, if any.

public default Option<T>

Returns:

Some(maximum) of this elements or None if this is empty
max
()

Calculates the maximum of this elements according to their natural order.

public default Option<T>

Returns:

Some(maximum) of this elements or None if this is empty
maxBy
(Comparator<? super T>
A non-null element comparator
comparator
)

Calculates the maximum of this elements using a specific comparator.

public default <
The type where elements are compared
U extends Comparable<? super U>
>
Option<T>

Returns:

The element of type T which is the maximum within U
maxBy
(Function<? super T, ? extends U>
A function that maps this elements to comparable elements
f
)

Calculates the maximum of this elements within the co-domain of a specific function.

public default Option<T>

Returns:

Some(minimum) of this elements or None if this is empty
min
()

Calculates the minimum of this elements according to their natural order in O(n).

public default Option<T>

Returns:

Some(minimum) of this elements or None if this is empty
minBy
(Comparator<? super T>
A non-null element comparator
comparator
)

Calculates the minimum of this elements using a specific comparator.

public default <
The type where elements are compared
U extends Comparable<? super U>
>
Option<T>

Returns:

The element of type T which is the minimum within U
minBy
(Function<? super T, ? extends U>
A function that maps this elements to comparable elements
f
)

Calculates the minimum of this elements within the co-domain of a specific function.

public default CharSeq

Returns:

a new CharSeq
mkCharSeq
()

Joins the elements of this by concatenating their string representations.

public default CharSeq

Returns:

A new CharSeq
mkCharSeq
(CharSequence
A delimiter string put between string representations of elements of this
delimiter
)

Joins the string representations of this elements using a specific delimiter.

public default CharSeq

Returns:

a new CharSeq
mkCharSeq
(CharSequence
prefix of the resulting CharSeq
prefix
,
CharSequence
A delimiter string put between string representations of elements of this
delimiter
,
CharSequence
suffix of the resulting CharSeq
suffix
)

Joins the string representations of this elements using a specific delimiter, prefix and suffix.

public default String

Returns:

a new String
mkString
()

Joins the elements of this by concatenating their string representations.

public default String

Returns:

A new String
mkString
(CharSequence
A delimiter string put between string representations of elements of this
delimiter
)

Joins the string representations of this elements using a specific delimiter.

public default String

Returns:

a new String
mkString
(CharSequence
prefix of the resulting string
prefix
,
CharSequence
A delimiter string put between string representations of elements of this
delimiter
,
CharSequence
suffix of the resulting string
suffix
)

Joins the string representations of this elements using a specific delimiter, prefix and suffix.

public static <
Component type of the Traversable.
T
>
Traversable<T>

Returns:

the given traversable instance as narrowed type Traversable<T>.
narrow
(Traversable<? extends T>
An Traversable.
traversable
)

Narrows a widened Traversable<?

public default boolean

Returns:

true, if an underlying value is present, false otherwise.
nonEmpty
()

Checks, this Traversable is not empty.

public Traversable<T>

Returns:

this Traversable if it is nonempty, otherwise return the alternative.
orElse
(Iterable<? extends T>
An alternative Traversable
other
)

Returns this Traversable if it is nonempty, otherwise return the alternative.

public Traversable<T>

Returns:

this Traversable if it is nonempty, otherwise return the result of evaluating supplier.
orElse
(Supplier<? extends Iterable<? extends T>>
An alternative Traversable supplier
supplier
)

Returns this Traversable if it is nonempty, otherwise return the result of evaluating supplier.

public Tuple2<? extends Traversable<T>, ? extends Traversable<T>>

Returns:

A disjoint union of two traversables. The first Traversable contains all elements that satisfy the given predicate, the second Traversable contains all elements that don't. The original order of elements is preserved.
partition
(Predicate<? super T>
A predicate which classifies an element if it is in the first or the second traversable.
predicate
)

Creates a partition of this Traversable by splitting this elements in two in distinct traversables according to a predicate.

public Traversable<T>
peek(Consumer<? super T>
The action that will be performed on the element(s).
action
)

Redeclares io.vavr.Value.peek.

Performs the given action on the first element if this is an eager implementation.

public default Number

Returns:

a Number representing the sum of this elements
product
()

Calculates the product of this elements.

public default T

Returns:

the reduced value.
reduceLeft
(BiFunction<? super T, ? super T, ? extends T>
A BiFunction of type T
op
)

Implements io.vavr.collection.Foldable.reduceLeft.

Accumulates the elements of this Traversable by successively calling the given operation op from the left.

public default Option<T>

Returns:

a reduced value
reduceLeftOption
(BiFunction<? super T, ? super T, ? extends T>
A BiFunction of type T
op
)

Implements io.vavr.collection.Foldable.reduceLeftOption.

Shortcut for isEmpty() ?

public default T

Returns:

the reduced value.
reduceRight
(BiFunction<? super T, ? super T, ? extends T>
An operation of type T
op
)

Implements io.vavr.collection.Foldable.reduceRight.

Accumulates the elements of this Traversable by successively calling the given operation op from the right.

public default Option<T>

Returns:

a reduced value
reduceRightOption
(BiFunction<? super T, ? super T, ? extends T>
An operation of type T
op
)

Implements io.vavr.collection.Foldable.reduceRightOption.

Shortcut for isEmpty() ?

public default Traversable<T>

Returns:

a new traversable
reject
(Predicate<? super T>
A predicate
predicate
)

Returns a new traversable consisting of all elements which do not satisfy the given predicate.

public Traversable<T>

Returns:

a Traversable containing all elements of this where the first occurrence of currentElement is replaced with newElement.
replace
(T
An element to be substituted.
currentElement
,
T
A replacement for currentElement.
newElement
)

Replaces the first occurrence (if exists) of the given currentElement with newElement.

public Traversable<T>

Returns:

a Traversable containing all elements of this where all occurrences of currentElement are replaced with newElement.
replaceAll
(T
An element to be substituted.
currentElement
,
T
A replacement for currentElement.
newElement
)

Replaces all occurrences of the given currentElement with newElement.

public Traversable<T>

Returns:

a Traversable containing all occurrences of the given elements.
retainAll
(Iterable<? extends T>
Elements to be kept.
elements
)

Keeps all occurrences of the given elements from this.

public Traversable<T>

Returns:

a new traversable collection containing the prefix scan of the elements in this traversable collection
scan
(T
neutral element for the operator op
zero
,
BiFunction<? super T, ? super T, ? extends T>
the associative operator for the scan
operation
)

Computes a prefix scan of the elements of the collection.

public <
the type of the elements in the resulting collection
U
>
Traversable<U>

Returns:

collection with intermediate results
scanLeft
(U
the initial value
zero
,
BiFunction<? super U, ? super T, ? extends U>
the binary operator applied to the intermediate result and the element
operation
)

Produces a collection containing cumulative results of applying the operator going left to right.

public <
the type of the elements in the resulting collection
U
>
Traversable<U>

Returns:

collection with intermediate results
scanRight
(U
the initial value
zero
,
BiFunction<? super T, ? super U, ? extends U>
the binary operator applied to the intermediate result and the element
operation
)

Produces a collection containing cumulative results of applying the operator going right to left.

public default T

Returns:

the single element from the Traversable
single
()

Returns the single element of this Traversable or throws, if this is empty or contains more than one element.

public default Option<T>

Returns:

Some(element) or None if the Traversable does not contain a single element.
singleOption
()

Returns the only element of a Traversable as Option.

public default int

Returns:

the number of elements
size
()

Computes the number of elements of this Traversable.

public Iterator<? extends Traversable<T>>

Returns:

A new Iterator of windows of the grouped elements
slideBy
(Function<? super T, ?>
A function which classifies elements into classes
classifier
)

Slides a non-overlapping window of a variable size over this Traversable.

public Iterator<? extends Traversable<T>>

Returns:

a new Iterator of windows of a specific size using step size 1
sliding
(int
a positive window size
size
)

Slides a window of a specific size and step size 1 over this Traversable by calling sliding(int, int).

public Iterator<? extends Traversable<T>>

Returns:

a new Iterator of windows of a specific size using a specific step size
sliding
(int
a positive window size
size
,
int
a positive step size
step
)

Slides a window of a specific size and step size over this Traversable.

public Tuple2<? extends Traversable<T>, ? extends Traversable<T>>

Returns:

a Tuple containing the longest prefix of elements that satisfy p and the remainder.
span
(Predicate<? super T>
A predicate.
predicate
)

Returns a tuple where the first element is the longest prefix of elements that satisfy the given predicate and the second element is the remainder.

public default Spliterator<T>
spliterator()

Overrides default io.vavr.Value.spliterator.

Creates a Spliterator over the elements described by this Iterable.

public default Number

Returns:

a Number representing the sum of this elements
sum
()

Calculates the sum of this elements.

public Traversable<T>

Returns:

A new instance of Traversable containing all elements except the first.
tail
()

Drops the first element of a non-empty Traversable.

public Option<? extends Traversable<T>>

Returns:

Some(traversable) or None if this is empty.
tailOption
()

Drops the first element of a non-empty Traversable and returns an Option.

public Traversable<T>

Returns:

A new instance consisting of the first n elements of this or all elements, if this has less than n elements.
take
(int
The number of elements to take.
n
)

Takes the first n elements of this or all elements, if this length < n.

public Traversable<T>

Returns:

A new instance consisting of the last n elements of this or all elements, if this has less than n elements.
takeRight
(int
The number of elements to take.
n
)

Takes the last n elements of this or all elements, if this length < n.

public Traversable<T>

Returns:

a new instance consisting of all elements before the first one which does satisfy the given predicate.
takeUntil
(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Takes elements until the predicate holds for the current element.

public Traversable<T>

Returns:

a new instance consisting of all elements before the first one which does not satisfy the given predicate.
takeWhile
(Predicate<? super T>
A condition tested subsequently for the contained elements.
predicate
)

Takes elements while the predicate holds for the current element.

public <
1st element type of a pair returned by unzipper
T1
,
2nd element type of a pair returned by unzipper
T2
>
Tuple2<? extends Traversable<T1>, ? extends Traversable<T2>>

Returns:

A pair of set containing elements split by unzipper
unzip
(Function<? super T, Tuple2<? extends T1, ? extends T2>>
a function which converts elements of this to pairs
unzipper
)

Unzips this elements by mapping this elements to pairs which are subsequently split into two distinct sets.

public <
1st element type of a triplet returned by unzipper
T1
,
2nd element type of a triplet returned by unzipper
T2
,
3rd element type of a triplet returned by unzipper
T3
>
Tuple3<? extends Traversable<T1>, ? extends Traversable<T2>, ? extends Traversable<T3>>

Returns:

A triplet of set containing elements split by unzipper
unzip3
(Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>>
a function which converts elements of this to pairs
unzipper
)

Unzips this elements by mapping this elements to triples which are subsequently split into three distinct sets.

public <
The type of the second half of the returned pairs.
U
>
Traversable<Tuple2<T, U>>

Returns:

a new traversable containing pairs consisting of corresponding elements of this traversable and that iterable.
zip
(Iterable<? extends U>
The Iterable providing the second half of each result pair.
that
)

Returns a traversable formed from this traversable and another Iterable collection by combining corresponding elements in pairs.

public <
The type of the second half of the returned pairs.
U
>
Traversable<Tuple2<T, U>>

Returns:

A new traversable containing pairs consisting of corresponding elements of this traversable and that.
zipAll
(Iterable<? extends U>
The Iterable providing the second half of each result pair.
that
,
T
The element to be used to fill up the result if this traversable is shorter than that.
thisElem
,
U
The element to be used to fill up the result if that is shorter than this traversable.
thatElem
)

Returns a traversable formed from this traversable and another Iterable by combining corresponding elements in pairs.

public <
The type of the second parameter of the mapper.
U
,
The type of the mapped elements.
R
>
Traversable<R>

Returns:

a new traversable containing mapped elements of this traversable and that iterable.
zipWith
(Iterable<? extends U>
The Iterable providing the second parameter of the mapper.
that
,
BiFunction<? super T, ? super U, ? extends R>
a mapper.
mapper
)

Returns a traversable formed from this traversable and another Iterable collection by mapping elements.

public Traversable<Tuple2<T, Integer>>

Returns:

A new traversable containing all elements of this traversable paired with their index, starting with 0.
zipWithIndex
()

Zips this traversable with its indices.

public <
The type of the mapped elements.
U
>
Traversable<U>

Returns:

a new traversable containing elements of this traversable, zipped with indices, and mapped with mapper provided.
zipWithIndex
(BiFunction<? super T, ? super Integer, ? extends U>
a mapper.
mapper
)

Zips this traversable with its indices by applying mapper provided.

Inherited from io.vavr.collection.Foldable:
foldreducereduceOption
Inherited from io.vavr.Value:
collectcollectcontainscorrespondseqexistsforAllforEachgetOrElsegetOrElsegetOrElseThrowgetOrElseTrygetOrNullisAsyncisLazynarrowoutoutstderrstdoutstringPrefixtoArraytoCharSeqtoCompletableFuturetoEithertoEithertoInvalidtoInvalidtoJavaArraytoJavaArraytoJavaArraytoJavaCollectiontoJavaListtoJavaListtoJavaMaptoJavaMaptoJavaMaptoJavaOptionaltoJavaParallelStreamtoJavaSettoJavaSettoJavaStreamtoLefttoLefttoLinkedMaptoLinkedMaptoLinkedSettoListtoMaptoMaptoOptiontoPriorityQueuetoPriorityQueuetoQueuetoRighttoRighttoSettoSortedMaptoSortedMaptoSortedMaptoSortedMaptoSortedSettoSortedSettoStreamtoStringtoTreetoTreetoTrytoTrytoValidtoValidtoValidationtoValidationtoVector