Class Stream.Empty<T>

  • Type Parameters:
    T - Component type of the Stream.
    All Implemented Interfaces:
    Foldable<T>, LinearSeq<T>, Seq<T>, Stream<T>, Traversable<T>, Function1<java.lang.Integer,​T>, PartialFunction<java.lang.Integer,​T>, Value<T>, java.io.Serializable, java.lang.Iterable<T>, java.util.function.Function<java.lang.Integer,​T>
    Enclosing interface:
    Stream<T>

    public static final class Stream.Empty<T>
    extends java.lang.Object
    implements Stream<T>, java.io.Serializable
    The empty Stream.

    This is a singleton, i.e. not Cloneable.

    See Also:
    Serialized Form
    • Constructor Detail

      • Empty

        private Empty()
    • Method Detail

      • instance

        public static <T> Stream.Empty<T> instance()
        Returns the singleton empty Stream instance.
        Type Parameters:
        T - Component type of the Stream
        Returns:
        The empty Stream
      • head

        public T head()
        Description copied from interface: Traversable
        Returns the first element of this non-empty Traversable.
        Specified by:
        head in interface Traversable<T>
        Returns:
        the first element
      • 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
      • iterator

        public @NonNull Iterator<T> iterator()
        Description copied from interface: Traversable
        Returns an iterator over the elements of this Traversable, implemented via Traversable.head() and Traversable.tail(). Subclasses may override for a more efficient implementation.
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface Traversable<T>
        Specified by:
        iterator in interface Value<T>
        Returns:
        a new Iterator over the elements of this Traversable
      • tail

        public Stream<T> tail()
        Description copied from interface: Traversable
        Returns a new Traversable without its first element.
        Specified by:
        tail in interface LinearSeq<T>
        Specified by:
        tail in interface Seq<T>
        Specified by:
        tail in interface Stream<T>
        Specified by:
        tail in interface Traversable<T>
        Returns:
        a new Traversable containing all elements except the first
      • equals

        public boolean equals​(java.lang.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 Value<T>
        Overrides:
        equals in class java.lang.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:

        {@code
         public final class Hashed {
             private final K key;
             private final Lazy hashCode;
        
             public Hashed(K key) {
                 this.key = key;
                 this.hashCode = Lazy.of(() -> Objects.hashCode(key));
             }
        
             public K key() { return key; }
        Specified by:
        hashCode in interface Traversable<T>
        Specified by:
        hashCode in interface Value<T>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code of this collection
      • toString

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

        See Object.toString().

        Specified by:
        toString in interface Value<T>
        Overrides:
        toString in class java.lang.Object
        Returns:
        A String representation of this object
      • readResolve

        private java.lang.Object readResolve()
        Instance control for object serialization.
        Returns:
        The singleton instance of Nil.
        See Also:
        Serializable