Top Description Inners Fields Methods
io.vavr.collection

public Interface Tree<T>

Additional top-level class in compilation unit: TreeModule.

extends Traversable<T>, Serializable
Known Direct Implementers
io.vavr.collection.Tree.Node, io.vavr.collection.Tree.Empty
Type Parameters
<T>
component type of this Tree
Imports
io.vavr.PartialFunction, .Tuple, .Tuple2, .Tuple3, io.vavr.collection.List.Nil, io.vavr.collection.Tree.*, io.vavr.control.Option, java.io.*, java.util.*, java.util.function.*, java.util.stream.Collector

A general Tree interface.
Author
Daniel Dietrich, Grzegorz Piwowarek

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Tree.Empty<
type of the tree's values
T
>

The empty tree.

public static class
Tree.Node<
value type
T
>

Represents a tree node.

public static enum
Tree.Order

Tree traversal order.

Field Summary

Modifier and TypeField and Description
public static final long

Method Summary

Modifier and TypeMethod and Description
public default int

Returns:

The number of branches of this tree.
branchCount
()

Counts the number of branches of this tree.

public static <
Value type
T
,
Id type
ID
>
List<Tree.Node<T>>

Returns:

a new, maybe empty List instance with non-empty Tree instances
build
(Iterable<? extends T>
Flat source
source
,
Function<? super T, ? extends ID>
A mapper from source item to unique identifier of that item
idMapper
,
Function<? super T, ? extends ID>
A mapper from source item to unique identifier of parent item. Need return null for root items
parentMapper
)

Build a List with roots of Tree from flat source.

public default <R> Tree<R>
collect(PartialFunction<? super T, ? extends R>
A function that is not necessarily defined of all elements of this traversable.
partialFunction
)

Implements io.vavr.collection.Traversable.collect.

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

public static <
Component type of the Tree.
T
>
Collector<T, ArrayList<T>, Tree<T>>

Returns:

A io.vavr.collection.Tree Collector.
collector
()

Returns a java.util.stream.Collector which may be used in conjunction with java.util.stream.Stream#collect(java.util.stream.Collector) to obtain a Tree.

public default Seq<T>
distinct()

Implements io.vavr.collection.Traversable.distinct.

Returns a new version of this which contains no duplicates.

public default Seq<T>
distinctBy(Comparator<? super T>
A comparator
comparator
)

Implements io.vavr.collection.Traversable.distinctBy.

Returns a new version of this which contains no duplicates.

public default <U> Seq<T>
distinctBy(Function<? super T, ? extends U>
A key extractor
keyExtractor
)

Implements io.vavr.collection.Traversable.distinctBy.

Returns a new version of this which contains no duplicates.

public String

Returns:

A nice string representation of the tree.
draw
()

Creates a neat 2-dimensional drawing of a tree.

public default Seq<T>
drop(int
The number of elements to drop.
n
)

Implements io.vavr.collection.Traversable.drop.

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

public default Seq<T>
dropRight(int
The number of elements to drop.
n
)

Implements io.vavr.collection.Traversable.dropRight.

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

public default Seq<T>
dropUntil(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Implements io.vavr.collection.Traversable.dropUntil.

Drops elements until the predicate holds for the current element.

public default Seq<T>
dropWhile(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Implements io.vavr.collection.Traversable.dropWhile.

Drops elements while the predicate holds for the current element.

public static <
Type of tree values.
T
>
Tree.Empty<T>

Returns:

The empty tree.
empty
()

Returns the singleton empty tree.

public boolean
equals(Object
an object, may be null
o
)

Redeclares io.vavr.collection.Traversable.equals.

In Vavr there are four basic classes of collections:

  • Seq (sequential elements)
  • Set (distinct elements)
  • Map (indexed elements)
  • Multimap (indexed collections)
public static <
Component type of the Tree
T
>
Tree<T>

Returns:

A Tree of size n, where each element contains the result supplied by s.
fill
(int
The number of elements in the Tree
n
,
Supplier<? extends T>
The Supplier computing element values
s
)

Returns a Tree containing n values supplied by a given Supplier s.

public static <
Component type of the Tree
T
>
Tree<T>

Returns:

A Tree of size n, where each element is the given element.
fill
(int
The number of elements in the Tree
n
,
T
The element
element
)

Returns a Tree containing n times the given element

public default Seq<T>
filter(Predicate<? super T>
A predicate
predicate
)

Implements io.vavr.collection.Traversable.filter.

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

public default <U> Tree<U>
flatMap(Function<? super T, ? extends Iterable<? extends U>>
A mapper
mapper
)

Implements io.vavr.collection.Traversable.flatMap.

FlatMaps this Traversable.

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

Implements io.vavr.collection.Traversable.foldRight.

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

public List<Tree.Node<T>>

Returns:

the tree's children
getChildren
()

Returns the children of this tree.

public T

Returns:

The value of this tree.
getValue
()

Gets the value of this tree.

public default <C> Map<C, Seq<T>>
groupBy(Function<? super T, ? extends C>
A function which classifies elements into classes
classifier
)

Implements io.vavr.collection.Traversable.groupBy.

Groups this elements by classifying the elements.

public default Iterator<Seq<T>>
grouped(int
a positive block size
size
)

Implements io.vavr.collection.Traversable.grouped.

Groups this Traversable into fixed size blocks.

public default boolean
hasDefiniteSize()

Implements io.vavr.collection.Traversable.hasDefiniteSize.

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

public int
hashCode()

Redeclares io.vavr.collection.Traversable.hashCode.

Returns the hash code of this collection.

public default T
head()

Implements io.vavr.collection.Traversable.head.

Returns the first element of a non-empty Traversable.

public default Seq<T>
init()

Implements io.vavr.collection.Traversable.init.

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

public default Option<Seq<T>>
initOption()

Overrides default io.vavr.collection.Traversable.initOption.

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

public default boolean

Returns:

false
isAsync
()

Implements io.vavr.Value.isAsync.

A Tree is computed synchronously.

public default boolean

Returns:

true if this tree is a branch, false otherwise.
isBranch
()

Checks if this Tree is a branch.

public default boolean
isDistinct()

Overrides default io.vavr.collection.Traversable.isDistinct.

Checks if this Traversable may consist of distinct elements only.

public default boolean

Returns:

false
isLazy
()

Implements io.vavr.Value.isLazy.

A Tree is computed eagerly.

public boolean

Returns:

true if this tree is a leaf, false otherwise.
isLeaf
()

Checks if this Tree is a leaf.

public default boolean
isSequential()

Overrides default io.vavr.collection.Traversable.isSequential.

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

public default boolean
isTraversableAgain()

Implements io.vavr.collection.Traversable.isTraversableAgain.

Checks if this Traversable can be repeatedly traversed.

public default Iterator<T>

Returns:

A new Iterator
iterator
(Tree.Order
A traversal order
order
)

Traverses this tree values in a specific Order.

public default Iterator<T>
iterator()

Overrides default io.vavr.collection.Traversable.iterator.

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

public default int

Returns:

The number of leaves of this tree.
leafCount
()

Counts the number of leaves of this tree.

public default <U> Tree<U>
map(Function<? super T, ? extends U>
A mapper.
mapper
)

Implements io.vavr.collection.Traversable.map.

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

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

Returns:

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

Narrows a widened Tree<?

public default int

Returns:

The number of nodes of this tree.
nodeCount
()

Counts the number of nodes (i.e. branches and leaves) of this tree.

public static <
Value type
T
>
Tree.Node<T>

Returns:

A new Node instance.
of
(T
A value
value
)

Returns a new Node containing the given value and having no children.

public static <
Value type
T
>
Tree.Node<T>

Returns:

A new Node instance.
of
(T
A value
value
,
Tree.Node<T>...
The child nodes, possibly empty
children
)

Returns a new Node containing the given value and having the given children.

public static <
Value type
T
>
Tree.Node<T>

Returns:

A new Node instance.
of
(T
A value
value
,
Iterable<Tree.Node<T>>
The child nodes, possibly empty
children
)

Returns a new Node containing the given value and having the given children.

public static <
Component type of the List.
T
>
Tree<T>

Returns:

A Tree containing the given values.
of
(T...
Zero or more values.
values
)

Creates a Tree of the given elements.

public static <
Component type of the List.
T
>
Tree<T>

Returns:

A list containing the given elements in the same order.
ofAll
(Iterable<? extends T>
An Iterable of elements.
iterable
)

Creates a Tree of the given elements.

public static <
Component type of the Stream.
T
>
Tree<T>

Returns:

A Tree containing the given elements in the same order.
ofAll
(Stream<? extends T> javaStream)

Creates a Tree that contains the elements of the given java.util.stream.Stream.

public default Tree<T>
orElse(Iterable<? extends T>
An alternative Traversable
other
)

Implements io.vavr.collection.Traversable.orElse.

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

public default Tree<T>
orElse(Supplier<? extends Iterable<? extends T>>
An alternative Traversable supplier
supplier
)

Implements io.vavr.collection.Traversable.orElse.

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

public default Tuple2<Seq<T>, Seq<T>>
partition(Predicate<? super T>
A predicate which classifies an element if it is in the first or the second traversable.
predicate
)

Implements io.vavr.collection.Traversable.partition.

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

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

Implements io.vavr.collection.Traversable.peek.

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

public static <
Value type
T
>
Tree.Node<T>

Returns:

a new, non-empty Tree instance
recurse
(T
The start value for the Tree
seed
,
Function<? super T, ? extends Iterable<? extends T>>
A function to calculate the child values
descend
)

Recursively builds a non-empty Tree, starting with the given seed value and proceeding in depth-first order.

public default Seq<T>
reject(Predicate<? super T>
A predicate
predicate
)

Overrides default io.vavr.collection.Traversable.reject.

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

public default Tree<T>
replace(T
An element to be substituted.
currentElement
,
T
A replacement for currentElement.
newElement
)

Implements io.vavr.collection.Traversable.replace.

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

public default Tree<T>
replaceAll(T
An element to be substituted.
currentElement
,
T
A replacement for currentElement.
newElement
)

Implements io.vavr.collection.Traversable.replaceAll.

Replaces all occurrences of the given currentElement with newElement.

public default Seq<T>
retainAll(Iterable<? extends T>
Elements to be kept.
elements
)

Implements io.vavr.collection.Traversable.retainAll.

Keeps all occurrences of the given elements from this.

public default Seq<T>
scan(T
neutral element for the operator op
zero
,
BiFunction<? super T, ? super T, ? extends T>
the associative operator for the scan
operation
)

Implements io.vavr.collection.Traversable.scan.

Computes a prefix scan of the elements of the collection.

public default <U> Seq<U>
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
)

Implements io.vavr.collection.Traversable.scanLeft.

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

public default <U> Seq<U>
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
)

Implements io.vavr.collection.Traversable.scanRight.

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

public default Iterator<Seq<T>>
slideBy(Function<? super T, ?>
A function which classifies elements into classes
classifier
)

Implements io.vavr.collection.Traversable.slideBy.

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

public default Iterator<Seq<T>>
sliding(int
a positive window size
size
)

Implements io.vavr.collection.Traversable.sliding.

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

public default Iterator<Seq<T>>
sliding(int
a positive window size
size
,
int
a positive step size
step
)

Implements io.vavr.collection.Traversable.sliding.

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

public default Tuple2<Seq<T>, Seq<T>>
span(Predicate<? super T>
A predicate.
predicate
)

Implements io.vavr.collection.Traversable.span.

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 String
stringPrefix()

Implements io.vavr.Value.stringPrefix.

Returns the name of this Value type, which is used by toString().

public static <
Component type of the Tree
T
>
Tree<T>

Returns:

A Tree consisting of elements f(0),f(1), ..., f(n - 1)
tabulate
(int
The number of elements in the Tree
n
,
Function<? super Integer, ? extends T>
The Function computing element values
f
)

Returns a Tree containing n values of a given Function f over a range of integer values from 0 to n - 1.

public default Seq<T>
tail()

Implements io.vavr.collection.Traversable.tail.

Drops the first element of a non-empty Traversable.

public default Option<Seq<T>>
tailOption()

Implements io.vavr.collection.Traversable.tailOption.

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

public default Seq<T>
take(int
The number of elements to take.
n
)

Implements io.vavr.collection.Traversable.take.

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

public default Seq<T>
takeRight(int
The number of elements to take.
n
)

Implements io.vavr.collection.Traversable.takeRight.

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

public default Seq<T>
takeUntil(Predicate<? super T>
A condition tested subsequently for this elements.
predicate
)

Implements io.vavr.collection.Traversable.takeUntil.

Takes elements until the predicate holds for the current element.

public default Seq<T>
takeWhile(Predicate<? super T>
A condition tested subsequently for the contained elements.
predicate
)

Implements io.vavr.collection.Traversable.takeWhile.

Takes elements while the predicate holds for the current element.

public String

Returns:

This Tree as Lisp-string, i.e. represented as list of lists.
toLispString
()

Creates a Lisp-like representation of this Tree.

public String
toString()

Redeclares io.vavr.Value.toString.

Clarifies that values have a proper toString() method implemented.

public default <
Type of transformation result
U
>
U

Returns:

An instance of type U
transform
(Function<? super Tree<T>, ? extends U>
A transformation
f
)

Transforms this Tree.

public default Seq<Tree.Node<T>>

Returns:

A sequence of nodes.
traverse
()

Traverses this tree in Order#PRE_ORDER.

public default Seq<Tree.Node<T>>

Returns:

A sequence of nodes.
traverse
(Tree.Order
the tree traversal order
order
)

Traverses this tree in a specific order.

public default <T1, T2> Tuple2<Tree<T1>, Tree<T2>>
unzip(Function<? super T, Tuple2<? extends T1, ? extends T2>>
a function which converts elements of this to pairs
unzipper
)

Implements io.vavr.collection.Traversable.unzip.

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

public default <T1, T2, T3> Tuple3<Tree<T1>, Tree<T2>, Tree<T3>>
unzip3(Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>>
a function which converts elements of this to pairs
unzipper
)

Implements io.vavr.collection.Traversable.unzip3.

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

public default Seq<T>

Returns:

A sequence of the tree values.
values
()

Traverses this tree values in Order#PRE_ORDER.

public default Seq<T>

Returns:

A sequence of the tree values.
values
(Tree.Order
the tree traversal order
order
)

Traverses this tree values in a specific order.

public default <U> Tree<Tuple2<T, U>>
zip(Iterable<? extends U>
The Iterable providing the second half of each result pair.
that
)

Implements io.vavr.collection.Traversable.zip.

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

public default <U> Tree<Tuple2<T, U>>
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
)

Implements io.vavr.collection.Traversable.zipAll.

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

public default <U, R> Tree<R>
zipWith(Iterable<? extends U>
The Iterable providing the second parameter of the mapper.
that
,
BiFunction<? super T, ? super U, ? extends R>
a mapper.
mapper
)

Implements io.vavr.collection.Traversable.zipWith.

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

public default Tree<Tuple2<T, Integer>>
zipWithIndex()

Implements io.vavr.collection.Traversable.zipWithIndex.

Zips this traversable with its indices.

public default <U> Tree<U>
zipWithIndex(BiFunction<? super T, ? super Integer, ? extends U>
a mapper.
mapper
)

Implements io.vavr.collection.Traversable.zipWithIndex.

Zips this traversable with its indices by applying mapper provided.

Inherited from io.vavr.collection.Traversable:
arrangeByaveragecontainsAllcountexistsUniquefindfindLastfoldLeftforEachWithIndexgetheadOptionisEmptyisOrderedisSingleValuedlastlastOptionlengthmaxmaxBymaxByminminByminBymkCharSeqmkCharSeqmkCharSeqmkStringmkStringmkStringnarrownonEmptyproductreduceLeftreduceLeftOptionreduceRightreduceRightOptionsinglesingleOptionsizespliteratorsum