Class Tree.Node<T>

java.lang.Object
io.vavr.collection.Tree.Node<T>
Type Parameters:
T - value type
All Implemented Interfaces:
Foldable<T>, Traversable<T>, Tree<T>, Value<T>, Serializable, Iterable<T>
Enclosing interface:
Tree<T>

public static final class Tree.Node<T> extends Object implements Tree<T>, Serializable
Represents a tree node.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • value

      private final T value
    • children

      private final List<Tree.Node<T>> children
    • size

      private final int size
  • Constructor Details

  • Method Details

    • getChildren

      public List<Tree.Node<T>> getChildren()
      Description copied from interface: Tree
      Returns the children of this tree.
      Specified by:
      getChildren in interface Tree<T>
      Returns:
      the tree's children
    • getValue

      public T getValue()
      Description copied from interface: Tree
      Gets the value of this tree.
      Specified by:
      getValue in interface Tree<T>
      Returns:
      The value of this tree.
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Traversable
      Checks if this Traversable contains no elements.
      Specified by:
      isEmpty in interface Traversable<T>
      Specified by:
      isEmpty in interface Value<T>
      Returns:
      true if empty, false otherwise
    • length

      public int length()
      Description copied from interface: Traversable
      Returns the number of elements in this Traversable.

      Equivalent to Traversable.size().

      Specified by:
      length in interface Traversable<T>
      Returns:
      the number of elements
    • isLeaf

      public boolean isLeaf()
      Description copied from interface: Tree
      Checks 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.
      Specified by:
      isLeaf in interface Tree<T>
      Returns:
      true if this tree is a leaf, false otherwise.
    • last

      public T last()
      Description copied from interface: Traversable
      Returns the last element of this Traversable.
      Specified by:
      last in interface Traversable<T>
      Returns:
      the last element
    • equals

      public boolean equals(Object o)
      Description copied from interface: Traversable
      Determines whether this collection is equal to the given object.

      In Vavr, there are four basic collection types:

      • Seq – sequential elements
      • Set – distinct elements
      • Map – key-value pairs
      • Multimap – keys mapped to multiple values
      Two collections are considered equal if and only if:
      • 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 Map and Multimap, 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:
      equals in interface Traversable<T>
      Specified by:
      equals in interface Tree<T>
      Specified by:
      equals in interface Value<T>
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare with, may be null
      Returns:
      true if the collections are equal according to the rules above, false otherwise
    • hashCode

      public int hashCode()
      Description copied from interface: Traversable
      Returns the hash code of this collection.

      Vavr distinguishes between collections with predictable iteration order (like Seq) and collections with arbitrary iteration order (like Set, Map, and Multimap). In all cases, the hash of an empty collection is defined as 1.

      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 HashMap keys), 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 + ")"; }
       }
       
      Specified by:
      hashCode in interface Traversable<T>
      Specified by:
      hashCode in interface Tree<T>
      Specified by:
      hashCode in interface Value<T>
      Overrides:
      hashCode in class Object
      Returns:
      the hash code of this collection
    • toString

      public String toString()
      Description copied from interface: Value
      Clarifies that values have a proper toString() method implemented.

      See Object.toString().

      Specified by:
      toString in interface Tree<T>
      Specified by:
      toString in interface Value<T>
      Overrides:
      toString in class Object
      Returns:
      A String representation of this object
    • toLispString

      public String toLispString()
      Description copied from interface: Tree
      Creates a Lisp-like representation of this Tree.
      Specified by:
      toLispString in interface Tree<T>
      Returns:
      This Tree as Lisp-string, i.e. represented as list of lists.
    • draw

      public String draw()
      Description copied from interface: Tree
      Creates a neat 2-dimensional drawing of a tree. Unicode characters are used to draw node junctions.
      Specified by:
      draw in interface Tree<T>
      Returns:
      A nice string representation of the tree.
    • drawAux

      private void drawAux(String indent, StringBuilder builder)
    • toLispString

      private static String toLispString(Tree<?> tree)
    • writeReplace

      @GwtIncompatible("The Java serialization protocol is explicitly not supported") private Object writeReplace()
      writeReplace method for the serialization proxy pattern.

      The presence of this method causes the serialization system to emit a SerializationProxy instance instead of an instance of the enclosing class.

      Returns:
      A SerializationProxy for this enclosing class.
    • readObject

      @GwtIncompatible("The Java serialization protocol is explicitly not supported") private void readObject(ObjectInputStream stream) throws InvalidObjectException
      readObject method for the serialization proxy pattern.

      Guarantees that the serialization system will never generate a serialized instance of the enclosing class.

      Parameters:
      stream - An object serialization stream.
      Throws:
      InvalidObjectException - This method will throw with the message "Proxy required".