Class Tree.Empty<T>
- Type Parameters:
T- type of the tree's values
- All Implemented Interfaces:
Foldable<T>, Traversable<T>, Tree<T>, Value<T>, Serializable, Iterable<T>
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface Tree
Tree.Empty<T>, Tree.Node<T>, Tree.Order -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondraw()Creates a neat 2-dimensional drawing of a tree.booleanDetermines whether this collection is equal to the given object.Returns the children of this tree.getValue()Gets the value of this tree.inthashCode()Returns the hash code of this collection.static <T> Tree.Empty<T> instance()Returns the singleton instance of the empty tree.booleanisEmpty()Checks if this Traversable contains no elements.booleanisLeaf()Checks if this Tree is a leaf.last()Returns the last element of this Traversable.intlength()Returns the number of elements in this Traversable.private ObjectInstance control for object serialization.Creates a Lisp-like representation of thisTree.toString()Clarifies that values have a proper toString() method implemented.Methods inherited from interface Foldable
fold, reduce, reduceOptionMethods inherited from interface Traversable
arrangeBy, average, containsAll, count, existsUnique, find, findLast, foldLeft, forEachWithIndex, get, headOption, isOrdered, isSingleValued, lastOption, max, maxBy, maxBy, min, minBy, minBy, mkCharSeq, mkCharSeq, mkCharSeq, mkString, mkString, mkString, nonEmpty, product, reduceLeft, reduceLeftOption, reduceRight, reduceRightOption, single, singleOption, size, spliterator, sumMethods inherited from interface Tree
branchCount, collect, distinct, distinctBy, distinctBy, drop, dropRight, dropUntil, dropWhile, filter, flatMap, foldRight, groupBy, grouped, hasDefiniteSize, head, init, initOption, isAsync, isBranch, isDistinct, isLazy, isSequential, isTraversableAgain, iterator, iterator, leafCount, map, mapTo, mapToVoid, nodeCount, orElse, orElse, partition, peek, reject, replace, replaceAll, retainAll, scan, scanLeft, scanRight, slideBy, sliding, sliding, span, stringPrefix, tail, tailOption, take, takeRight, takeUntil, takeWhile, transform, traverse, traverse, unzip, unzip3, values, values, zip, zipAll, zipWith, zipWithIndex, zipWithIndexMethods inherited from interface 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, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
INSTANCE
-
-
Constructor Details
-
Empty
private Empty()
-
-
Method Details
-
instance
Returns the singleton instance of the empty tree.- Type Parameters:
T- The component type- Returns:
- The empty tree instance
-
getChildren
-
getValue
-
isEmpty
public boolean isEmpty()Description copied from interface:TraversableChecks if this Traversable contains no elements. -
length
public 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
-
isLeaf
public boolean isLeaf()Description copied from interface:TreeChecks if this Tree is a leaf. A tree is a leaf if it is a Node with no children. Because the empty tree is no Node, it is not a leaf by definition. -
last
Description copied from interface:TraversableReturns the last element of this Traversable.- Specified by:
lastin interfaceTraversable<T>- Returns:
- the last element
-
equals
Description copied from interface:TraversableDetermines whether this collection is equal to the given object.In Vavr, there are four basic collection types:
Seq– sequential elementsSet– distinct elementsMap– key-value pairsMultimap– keys mapped to multiple values
- They are of the same collection type (Seq, Set, Map, Multimap)
- They contain the same elements
- For
Seq, the element order is the same
For
MapandMultimap, two entries(key1, value1)and(key2, value2)are equal if both their keys and values are equal.Additional notes:
- No collection equals
null(e.g.,Queue(1) != null) - Null elements are allowed and treated as expected
(e.g.,
List(null, 1) == Stream(null, 1),HashMap((null,1)) == LinkedHashMap((null,1))) - Element order matters only for
Seq - Other collection classes are equal if their types and elements (in iteration order) are equal
- Iterators are compared by reference only
- Specified by:
equalsin interfaceTraversable<T>- Specified by:
equalsin interfaceTree<T>- Specified by:
equalsin interfaceValue<T>- Overrides:
equalsin classObject- Parameters:
o- the object to compare with, may benull- Returns:
trueif the collections are equal according to the rules above,falseotherwise
-
hashCode
public int hashCode()Description copied from interface:TraversableReturns the hash code of this collection.Vavr distinguishes between collections with predictable iteration order (like
Seq) and collections with arbitrary iteration order (likeSet,Map, andMultimap). In all cases, the hash of an empty collection is defined as1.For collections with predictable iteration order, the hash is computed as:
int hash = 1; for (T t : this) { hash = hash * 31 + Objects.hashCode(t); }For collections with arbitrary iteration order, the hash is computed to be independent of element order:
int hash = 1; for (T t : this) { hash += Objects.hashCode(t); }Note that these algorithms may change in future Vavr versions. Hash codes are generally not cached, unlike size/length, because caching would increase memory usage due to persistent tree-based structures. Computing the hash code is linear in time, O(n). For frequently re-used collections (e.g., as
HashMapkeys), caching can be done externally using a wrapper, for example:public final class Hashed<K> { private final K key; private final Lazy<Integer> hashCode; public Hashed(K key) { this.key = key; this.hashCode = Lazy.of(() -> Objects.hashCode(key)); } public K key() { return key; } @Override public boolean equals(Object o) { if (o == key) return true; if (key != null && o instanceof Hashed) return key.equals(((Hashed<?>) o).key); return false; } @Override public int hashCode() { return hashCode.get(); } @Override public String toString() { return "Hashed(" + key + ")"; } } -
toString
Description copied from interface:ValueClarifies that values have a proper toString() method implemented.See Object.toString().
-
toLispString
-
draw
-
readResolve
Instance control for object serialization.- Returns:
- The singleton instance of Nil.
- See Also:
-