T
- component type of this Treepublic interface Tree<T> extends Traversable<T>
Modifier and Type | Interface and Description |
---|---|
static class |
Tree.Empty<T>
The empty tree.
|
static class |
Tree.Node<T>
Represents a tree node.
|
static class |
Tree.Order
Tree traversal order.
|
Modifier and Type | Method and Description |
---|---|
default int |
branchCount()
Counts the number of branches of this tree.
|
static <T> Collector<T,ArrayList<T>,Tree<T>> |
collector()
Returns a
Collector which may be used in conjunction with
Stream.collect(java.util.stream.Collector) to obtain a Tree . |
default Seq<T> |
distinct()
Returns a new version of this which contains no duplicates.
|
default Seq<T> |
distinctBy(Comparator<? super T> comparator)
Returns a new version of this which contains no duplicates.
|
default <U> Seq<T> |
distinctBy(Function<? super T,? extends U> keyExtractor)
Returns a new version of this which contains no duplicates.
|
String |
draw()
Creates a neat 2-dimensional drawing of a tree.
|
default Seq<T> |
drop(long n)
Drops the first n elements of this or all elements, if this length < n.
|
default Seq<T> |
dropRight(long n)
Drops the last n elements of this or all elements, if this length < n.
|
default Seq<T> |
dropUntil(Predicate<? super T> predicate)
Drops elements until the predicate holds for the current element.
|
default Seq<T> |
dropWhile(Predicate<? super T> predicate)
Drops elements while the predicate holds for the current element.
|
static <T> Tree.Empty<T> |
empty()
Returns the singleton empty tree.
|
boolean |
equals(Object o)
Clarifies that values have a proper equals() method implemented.
|
static <T> Tree<T> |
fill(int n,
Supplier<? extends T> s)
Returns a Tree containing
n values supplied by a given Supplier s . |
default Seq<T> |
filter(Predicate<? super T> predicate)
Returns a new traversable consisting of all elements which satisfy the given predicate.
|
default <U> Tree<U> |
flatMap(Function<? super T,? extends Iterable<? extends U>> mapper)
FlatMaps this Traversable.
|
default <U> U |
foldRight(U zero,
BiFunction<? super T,? super U,? extends U> f)
Accumulates the elements of this Traversable by successively calling the given function
f from the right,
starting with a value zero of type B. |
List<Tree.Node<T>> |
getChildren()
Returns the children of this tree.
|
T |
getValue()
Gets the value of this tree.
|
default <C> Map<C,Seq<T>> |
groupBy(Function<? super T,? extends C> classifier)
Groups this elements by classifying the elements.
|
default Iterator<Seq<T>> |
grouped(long size)
Groups this
Traversable into fixed size blocks. |
default boolean |
hasDefiniteSize()
Checks if this Traversable is known to have a finite size.
|
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
default T |
head()
Returns the first element of a non-empty Traversable.
|
default Option<T> |
headOption()
Returns the first element of a non-empty Traversable as
Option . |
default Seq<T> |
init()
Dual of Traversable.tail(), returning all elements except the last.
|
default Option<Seq<T>> |
initOption()
Dual of Traversable.tailOption(), returning all elements except the last as
Option . |
default boolean |
isBranch()
Checks if this Tree is a branch.
|
boolean |
isLeaf()
Checks if this Tree is a leaf.
|
default boolean |
isTraversableAgain()
Checks if this Traversable can be repeatedly traversed.
|
default Iterator<T> |
iterator()
An iterator by means of head() and tail().
|
default Iterator<T> |
iterator(Tree.Order order)
Traverses this tree values in a specific
Tree.Order . |
default int |
leafCount()
Counts the number of leaves of this tree.
|
default int |
length()
Computes the number of elements of this Traversable.
|
default <U> Tree<U> |
map(Function<? super T,? extends U> mapper)
Maps the elements of this
Traversable to elements of a new type preserving their order, if any. |
static <T> Tree<T> |
narrow(Tree<? extends T> tree)
Narrows a widened
Tree<? extends T> to Tree<T>
by performing a type safe-cast. |
default int |
nodeCount()
Counts the number of nodes (i.e.
|
static <T> Tree<T> |
of(T... values)
Creates a Tree of the given elements.
|
static <T> Tree.Node<T> |
of(T value)
Returns a new Node containing the given value and having no children.
|
static <T> Tree.Node<T> |
of(T value,
Iterable<Tree.Node<T>> children)
Returns a new Node containing the given value and having the given children.
|
static <T> Tree.Node<T> |
of(T value,
Tree.Node<T>... children)
Returns a new Node containing the given value and having the given children.
|
static <T> Tree<T> |
ofAll(Iterable<? extends T> iterable)
Creates a Tree of the given elements.
|
default Tuple2<Seq<T>,Seq<T>> |
partition(Predicate<? super T> predicate)
Creates a partition of this
Traversable by splitting this elements in two in distinct tarversables
according to a predicate. |
default Tree<T> |
peek(Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
default Tree<T> |
replace(T currentElement,
T newElement)
Replaces the first occurrence (if exists) of the given currentElement with newElement.
|
default Tree<T> |
replaceAll(T currentElement,
T newElement)
Replaces all occurrences of the given currentElement with newElement.
|
default Seq<T> |
retainAll(Iterable<? extends T> elements)
Keeps all occurrences of the given elements from this.
|
default Seq<T> |
scan(T zero,
BiFunction<? super T,? super T,? extends T> operation)
Computes a prefix scan of the elements of the collection.
|
default <U> Seq<U> |
scanLeft(U zero,
BiFunction<? super U,? super T,? extends U> operation)
Produces a collection containing cumulative results of applying the
operator going left to right.
|
default <U> Seq<U> |
scanRight(U zero,
BiFunction<? super T,? super U,? extends U> operation)
Produces a collection containing cumulative results of applying the
operator going right to left.
|
int |
size()
Returns the number of nodes (including root and leafs).
|
default Iterator<Seq<T>> |
sliding(long size)
Slides a window of a specific
size and step size 1 over this Traversable by calling
Traversable.sliding(long, long) . |
default Iterator<Seq<T>> |
sliding(long size,
long step)
Slides a window of a specific
size and step size over this Traversable . |
default Tuple2<Seq<T>,Seq<T>> |
span(Predicate<? super T> 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. |
default Spliterator<T> |
spliterator() |
default String |
stringPrefix()
Returns the name of this Value type, which is used by toString().
|
static <T> Tree<T> |
tabulate(int n,
Function<? super Integer,? extends T> f)
Returns a Tree containing
n values of a given Function f
over a range of integer values from 0 to n - 1 . |
default Seq<T> |
tail()
Drops the first element of a non-empty Traversable.
|
default Option<Seq<T>> |
tailOption()
Drops the first element of a non-empty Traversable and returns an
Option . |
default Seq<T> |
take(long n)
Takes the first n elements of this or all elements, if this length < n.
|
default Seq<T> |
takeRight(long n)
Takes the last n elements of this or all elements, if this length < n.
|
default Seq<T> |
takeUntil(Predicate<? super T> predicate)
Takes elements until the predicate holds for the current element.
|
default Seq<T> |
takeWhile(Predicate<? super T> predicate)
Takes elements while the predicate holds for the current element.
|
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
default <U> U |
transform(Function<? super Tree<T>,? extends U> f)
Transforms this
Tree . |
default Seq<Tree.Node<T>> |
traverse()
Traverses this tree in
Tree.Order.PRE_ORDER . |
default Seq<Tree.Node<T>> |
traverse(Tree.Order order)
Traverses this tree in a specific order.
|
default <T1,T2> Tuple2<Tree<T1>,Tree<T2>> |
unzip(Function<? super T,Tuple2<? extends T1,? extends T2>> unzipper)
Unzips this elements by mapping this elements to pairs which are subsequently split into two distinct
sets.
|
default <T1,T2,T3> Tuple3<Tree<T1>,Tree<T2>,Tree<T3>> |
unzip3(Function<? super T,Tuple3<? extends T1,? extends T2,? extends T3>> unzipper)
Unzips this elements by mapping this elements to triples which are subsequently split into three distinct
sets.
|
default Seq<T> |
values()
Traverses this tree values in
Tree.Order.PRE_ORDER . |
default Seq<T> |
values(Tree.Order order)
Traverses this tree values in a specific order.
|
default <U> Tree<Tuple2<T,U>> |
zip(Iterable<? extends U> that)
Returns a traversable formed from this traversable and another Iterable collection by combining
corresponding elements in pairs.
|
default <U> Tree<Tuple2<T,U>> |
zipAll(Iterable<? extends U> that,
T thisElem,
U thatElem)
Returns a traversable formed from this traversable and another Iterable by combining corresponding elements in
pairs.
|
default Tree<Tuple2<T,Long>> |
zipWithIndex()
Zips this traversable with its indices.
|
average, containsAll, count, existsUnique, find, findLast, foldLeft, get, hash, isEmpty, isSingleValued, last, lastOption, max, maxBy, maxBy, min, minBy, minBy, mkString, mkString, mkString, narrow, nonEmpty, product, reduceLeft, reduceLeftOption, reduceRight, reduceRightOption, sum
fold, reduce, reduceOption
contains, corresponds, eq, exists, forAll, forEach, getOption, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, narrow, out, out, stderr, stdout, toArray, toCharSeq, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaOptional, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toList, toMap, toOption, toQueue, toRight, toRight, toSet, toStack, toStream, toTree, toTry, toTry, toVector
static <T> Collector<T,ArrayList<T>,Tree<T>> collector()
Collector
which may be used in conjunction with
Stream.collect(java.util.stream.Collector)
to obtain a Tree
.T
- Component type of the Tree.static <T> Tree.Empty<T> empty()
T
- Type of tree values.static <T> Tree<T> narrow(Tree<? extends T> tree)
Tree<? extends T>
to Tree<T>
by performing a type safe-cast. This is eligible because immutable/read-only
collections are covariant.T
- Component type of the Tree
.tree
- An Tree
.tree
instance as narrowed type Tree<T>
.static <T> Tree.Node<T> of(T value)
T
- Value typevalue
- A value@SafeVarargs static <T> Tree.Node<T> of(T value, Tree.Node<T>... children)
T
- Value typevalue
- A valuechildren
- The child nodes, possibly emptystatic <T> Tree.Node<T> of(T value, Iterable<Tree.Node<T>> children)
T
- Value typevalue
- A valuechildren
- The child nodes, possibly empty@SafeVarargs static <T> Tree<T> of(T... values)
T
- Component type of the List.values
- Zero or more values.NullPointerException
- if values
is nullstatic <T> Tree<T> ofAll(Iterable<? extends T> iterable)
If the given iterable is a tree, it is returned as result. if the iteration order of the elements is stable.
T
- Component type of the List.iterable
- An Iterable of elements.NullPointerException
- if elements
is nullstatic <T> Tree<T> tabulate(int n, Function<? super Integer,? extends T> f)
n
values of a given Function f
over a range of integer values from 0 to n - 1
.T
- Component type of the Treen
- The number of elements in the Treef
- The Function computing element valuesf(0),f(1), ..., f(n - 1)
NullPointerException
- if f
is nullstatic <T> Tree<T> fill(int n, Supplier<? extends T> s)
n
values supplied by a given Supplier s
.T
- Component type of the Treen
- The number of elements in the Trees
- The Supplier computing element valuesn
, where each element contains the result supplied by s
.NullPointerException
- if s
is nullT getValue()
UnsupportedOperationException
- if this tree is emptyList<Tree.Node<T>> getChildren()
boolean isLeaf()
default boolean isBranch()
default Iterator<T> iterator(Tree.Order order)
Tree.Order
.order
- A traversal orderint size()
size
in interface Traversable<T>
default <U> U transform(Function<? super Tree<T>,? extends U> f)
Tree
.U
- Type of transformation resultf
- A transformationU
NullPointerException
- if f
is nulldefault Seq<Tree.Node<T>> traverse()
Tree.Order.PRE_ORDER
.default Seq<Tree.Node<T>> traverse(Tree.Order order)
order
- the tree traversal orderNullPointerException
- if order is nulldefault Seq<T> values()
Tree.Order.PRE_ORDER
.
Syntactic sugar for traverse().map(Node::getValue)
.default Seq<T> values(Tree.Order order)
traverse(order).map(Node::getValue)
.order
- the tree traversal orderNullPointerException
- if order is nulldefault int branchCount()
default int leafCount()
default int nodeCount()
default Seq<T> distinct()
Traversable
equals
.distinct
in interface Traversable<T>
Traversable
containing this elements without duplicatesdefault Seq<T> distinctBy(Comparator<? super T> comparator)
Traversable
comparator
.distinctBy
in interface Traversable<T>
comparator
- A comparatorTraversable
containing this elements without duplicatesdefault <U> Seq<T> distinctBy(Function<? super T,? extends U> keyExtractor)
Traversable
equals
.
The elements of the result are determined in the order of their occurrence - first match wins.
distinctBy
in interface Traversable<T>
U
- key typekeyExtractor
- A key extractorTraversable
containing this elements without duplicatesdefault Seq<T> drop(long n)
Traversable
drop
in interface Traversable<T>
n
- The number of elements to drop.default Seq<T> dropRight(long n)
Traversable
dropRight
in interface Traversable<T>
n
- The number of elements to drop.default Seq<T> dropUntil(Predicate<? super T> predicate)
Traversable
Note: This is essentially the same as dropWhile(predicate.negate())
. It is intended to be used with
method references, which cannot be negated directly.
dropUntil
in interface Traversable<T>
predicate
- A condition tested subsequently for this elements.default Seq<T> dropWhile(Predicate<? super T> predicate)
Traversable
dropWhile
in interface Traversable<T>
predicate
- A condition tested subsequently for this elements starting with the first.default Seq<T> filter(Predicate<? super T> predicate)
Traversable
filter
in interface Traversable<T>
predicate
- A predicatedefault <U> Tree<U> flatMap(Function<? super T,? extends Iterable<? extends U>> mapper)
Traversable
flatMap
in interface Traversable<T>
U
- The resulting component type.mapper
- A mapperdefault <U> U foldRight(U zero, BiFunction<? super T,? super U,? extends U> f)
Traversable
f
from the right,
starting with a value zero
of type B.
Example: List.of("a", "b", "c").foldRight("", (x, xs) -> x + xs) = "abc"
default <C> Map<C,Seq<T>> groupBy(Function<? super T,? extends C> classifier)
Traversable
groupBy
in interface Traversable<T>
C
- classified class typeclassifier
- A function which classifies elements into classesdefault Iterator<Seq<T>> grouped(long size)
Traversable
Traversable
into fixed size blocks.
Let length be the length of this Iterable. Then grouped is defined as follows:
this.isEmpty()
, the resulting Iterator
is empty.size <= length
, the resulting Iterator
will contain length / size
blocks of size
size
and maybe a non-empty block of size length % size
, if there are remaining elements.size > length
, the resulting Iterator
will contain one block of size length
.
[].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]]
Please note that grouped(int)
is a special case of Traversable.sliding(long, long), i.e.
grouped(size)
is the same as sliding(size, size)
.grouped
in interface Traversable<T>
size
- a positive block sizedefault boolean hasDefiniteSize()
Traversable
This method should be implemented by classes only, i.e. not by interfaces.
hasDefiniteSize
in interface Traversable<T>
default T head()
Traversable
head
in interface Traversable<T>
default Option<T> headOption()
Traversable
Option
.headOption
in interface Traversable<T>
Some(element)
or None
if this is empty.default Seq<T> init()
Traversable
init
in interface Traversable<T>
default Option<Seq<T>> initOption()
Traversable
Option
.initOption
in interface Traversable<T>
Some(traversable)
or None
if this is empty.default boolean isTraversableAgain()
Traversable
This method should be implemented by classes only, i.e. not by interfaces.
isTraversableAgain
in interface Traversable<T>
default Iterator<T> iterator()
Traversable
default int length()
Traversable
Same as Traversable.size()
.
length
in interface Traversable<T>
default <U> Tree<U> map(Function<? super T,? extends U> mapper)
Traversable
Traversable
to elements of a new type preserving their order, if any.default Tuple2<Seq<T>,Seq<T>> partition(Predicate<? super T> predicate)
Traversable
Traversable
by splitting this elements in two in distinct tarversables
according to a predicate.partition
in interface Traversable<T>
predicate
- A predicate which classifies an element if it is in the first or the second traversable.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.default Tree<T> peek(Consumer<? super T> action)
Value
action
on the first element if this is an eager implementation.
Performs the given action
on all elements (the first immediately, successive deferred),
if this is a lazy implementation.default Tree<T> replace(T currentElement, T newElement)
Traversable
replace
in interface Traversable<T>
currentElement
- An element to be substituted.newElement
- A replacement for currentElement.default Tree<T> replaceAll(T currentElement, T newElement)
Traversable
replaceAll
in interface Traversable<T>
currentElement
- An element to be substituted.newElement
- A replacement for currentElement.default Seq<T> retainAll(Iterable<? extends T> elements)
Traversable
retainAll
in interface Traversable<T>
elements
- Elements to be kept.default Seq<T> scan(T zero, BiFunction<? super T,? super T,? extends T> operation)
Traversable
scan
in interface Traversable<T>
zero
- neutral element for the operator opoperation
- the associative operator for the scandefault <U> Seq<U> scanLeft(U zero, BiFunction<? super U,? super T,? extends U> operation)
Traversable
scanLeft
in interface Traversable<T>
U
- the type of the elements in the resulting collectionzero
- the initial valueoperation
- the binary operator applied to the intermediate result and the elementdefault <U> Seq<U> scanRight(U zero, BiFunction<? super T,? super U,? extends U> operation)
Traversable
scanRight
in interface Traversable<T>
U
- the type of the elements in the resulting collectionzero
- the initial valueoperation
- the binary operator applied to the intermediate result and the elementdefault Iterator<Seq<T>> sliding(long size)
Traversable
size
and step size 1 over this Traversable
by calling
Traversable.sliding(long, long)
.sliding
in interface Traversable<T>
size
- a positive window sizedefault Iterator<Seq<T>> sliding(long size, long step)
Traversable
size
and step
size over this Traversable
.
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]]
sliding
in interface Traversable<T>
size
- a positive window sizestep
- a positive step sizedefault Tuple2<Seq<T>,Seq<T>> span(Predicate<? super T> predicate)
Traversable
predicate
and the second element is the remainder.span
in interface Traversable<T>
predicate
- A predicate.default Spliterator<T> spliterator()
spliterator
in interface Iterable<T>
default String stringPrefix()
Value
stringPrefix
in interface Value<T>
default Seq<T> tail()
Traversable
tail
in interface Traversable<T>
default Option<Seq<T>> tailOption()
Traversable
Option
.tailOption
in interface Traversable<T>
Some(traversable)
or None
if this is empty.default Seq<T> take(long n)
Traversable
The result is equivalent to sublist(0, max(0, min(length(), n)))
but does not throw if n < 0
or
n > length()
.
In the case of n < 0
the empty instance is returned, in the case of n > length()
this is returned.
take
in interface Traversable<T>
n
- The number of elements to take.default Seq<T> takeRight(long n)
Traversable
The result is equivalent to sublist(max(0, min(length(), length() - n)), n)
, i.e. takeRight will not
throw if n < 0
or n > length()
.
In the case of n < 0
the empty instance is returned, in the case of n > length()
this is returned.
takeRight
in interface Traversable<T>
n
- The number of elements to take.default Seq<T> takeUntil(Predicate<? super T> predicate)
Traversable
Note: This is essentially the same as takeWhile(predicate.negate())
. It is intended to be used with
method references, which cannot be negated directly.
takeUntil
in interface Traversable<T>
predicate
- A condition tested subsequently for this elements.default Seq<T> takeWhile(Predicate<? super T> predicate)
Traversable
takeWhile
in interface Traversable<T>
predicate
- A condition tested subsequently for the contained elements.default <T1,T2> Tuple2<Tree<T1>,Tree<T2>> unzip(Function<? super T,Tuple2<? extends T1,? extends T2>> unzipper)
Traversable
unzip
in interface Traversable<T>
T1
- 1st element type of a pair returned by unzipperT2
- 2nd element type of a pair returned by unzipperunzipper
- a function which converts elements of this to pairsdefault <T1,T2,T3> Tuple3<Tree<T1>,Tree<T2>,Tree<T3>> unzip3(Function<? super T,Tuple3<? extends T1,? extends T2,? extends T3>> unzipper)
Traversable
unzip3
in interface Traversable<T>
T1
- 1st element type of a triplet returned by unzipperT2
- 2nd element type of a triplet returned by unzipperT3
- 3rd element type of a triplet returned by unzipperunzipper
- a function which converts elements of this to pairsdefault <U> Tree<Tuple2<T,U>> zip(Iterable<? extends U> that)
Traversable
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
zip
in interface Traversable<T>
U
- The type of the second half of the returned pairs.that
- The Iterable providing the second half of each result pair.that
iterable.default <U> Tree<Tuple2<T,U>> zipAll(Iterable<? extends U> that, T thisElem, U thatElem)
Traversable
The length of the returned traversable is the maximum of the lengths of this traversable and that
iterable.
Special case: if this traversable is shorter than that elements, and that elements contains duplicates, the resulting traversable may be shorter than the maximum of the lengths of this and that because a traversable contains an element at most once.
If this Traversable is shorter than that, thisElem values are used to fill the result. If that is shorter than this Traversable, thatElem values are used to fill the result.
zipAll
in interface Traversable<T>
U
- The type of the second half of the returned pairs.that
- The Iterable providing the second half of each result pair.thisElem
- The element to be used to fill up the result if this traversable is shorter than that.thatElem
- The element to be used to fill up the result if that is shorter than this traversable.default Tree<Tuple2<T,Long>> zipWithIndex()
Traversable
zipWithIndex
in interface Traversable<T>
boolean equals(Object o)
Value
int hashCode()
Value
See Object.hashCode().
String toString()
Value
See Object.toString().
String draw()
Copyright © 2016. All Rights Reserved.