Class List.Cons<T>

  • Type Parameters:
    T - Component type of the List.
    All Implemented Interfaces:
    Foldable<T>, LinearSeq<T>, List<T>, Seq<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:
    List<T>

    public static final class List.Cons<T>
    extends java.lang.Object
    implements List<T>, java.io.Serializable
    Non-empty List, consisting of a head and a tail.
    See Also:
    Serialized Form
    • Field Detail

      • head

        private final T head
      • tail

        private final List<T> tail
      • length

        private final int length
    • Constructor Detail

      • Cons

        private Cons​(T head,
                     List<T> tail)
        Creates a List consisting of a head value and a trailing List.
        Parameters:
        head - The head
        tail - The tail
    • Method Detail

      • head

        public T head()
        Description copied from interface: Traversable
        Returns the first element of a non-empty Traversable.
        Specified by:
        head in interface Traversable<T>
        Returns:
        The first element of this Traversable.
      • tail

        public List<T> tail()
        Description copied from interface: Traversable
        Drops the first element of a non-empty Traversable.
        Specified by:
        tail in interface LinearSeq<T>
        Specified by:
        tail in interface List<T>
        Specified by:
        tail in interface Seq<T>
        Specified by:
        tail in interface Traversable<T>
        Returns:
        A new instance of Traversable containing all elements except the first.
      • isEmpty

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

        public boolean equals​(java.lang.Object o)
        Description copied from interface: Traversable
        In Vavr there are four basic classes of collections:
        • Seq (sequential elements)
        • Set (distinct elements)
        • Map (indexed elements)
        • Multimap (indexed collections)
        Two collection instances of these classes are equal if and only if both collections
        • belong to the same basic collection class (Seq, Set, Map or Multimap)
        • contain the same elements
        • have the same element order, if the collections are of type Seq
        Two Map/Multimap elements, resp. entries, (key1, value1) and (key2, value2) are equal, if the keys are equal and the values are equal.

        Notes:

        • No collection instance equals null, e.g. Queue(1) not equals null.
        • Nulls are allowed and handled as expected, e.g. List(null, 1) equals Stream(null, 1) and HashMap((null, 1)) equals LinkedHashMap((null, 1)).
        • The element order is taken into account for Seq only. E.g. List(null, 1) not equals Stream(1, null) and HashMap((null, 1), ("a", null)) equals LinkedHashMap(("a", null), (null, 1)). The reason is, that we do not know which implementations we compare when having two instances of type Map, Multimap or Set (see Liskov Substitution Principle).
        • Other collection classes are equal if their types are equal and their elements are equal (in iteration order).
        • Iterator equality is defined to be object reference equality.
        Specified by:
        equals in interface Traversable<T>
        Specified by:
        equals in interface Value<T>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - an object, may be null
        Returns:
        true, if this collection equals the given object according to the rules described above, false otherwise.
      • hashCode

        public int hashCode()
        Description copied from interface: Traversable
        Returns the hash code of this collection.
        We distinguish between two types of hashes, those for collections with predictable iteration order (like Seq) and those with arbitrary iteration order (like Set, Map and Multimap).
        In all cases the hash of an empty collection is defined to be 1.
        Collections with predictable iteration order are hashed as follows:
        
         int hash = 1;
         for (T t : this) { hash = hash * 31 + Objects.hashCode(t); }
         
        Collections with arbitrary iteration order are hashed in a way such that the hash of a fixed number of elements is independent of their iteration order.
        
         int hash = 1;
         for (T t : this) { hash += Objects.hashCode(t); }
         
        Please note that the particular hashing algorithms may change in a future version of Vavr.
        Generally, hash codes of collections aren't cached in Vavr (opposed to the size/length). Storing hash codes in order to reduce the time complexity would increase the memory footprint. Persistent collections are built upon tree structures, it allows us to implement efficient memory sharing. A drawback of tree structures is that they make it necessary to store collection attributes at each tree node (read: element).
        The computation of the hash code is linear in time, i.e. O(n). If the hash code of a collection is re-calculated often, e.g. when using a List as HashMap key, we might want to cache the hash code. This can be achieved by simply using a wrapper class, which is not included in Vavr but could be implemented like this:
        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;
             }
        
             &#64;Override
             public boolean equals(Object o) {
                 if (o == key) {
                     return true;
                 } else if (key != null && o instanceof Hashed) {
                     final Hashed that = (Hashed) o;
                     return key.equals(that.key);
                 } else {
                     return false;
                 }
             }
        
             &#64;Override
             public int hashCode() {
                 return hashCode.get();
             }
        
             &#64;Override
             public String toString() {
                 return "Hashed(" + (key == null ? "null" : key.toString()) + ")";
             }
         }
        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
      • writeReplace

        @GwtIncompatible("The Java serialization protocol is explicitly not supported")
        private java.lang.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​(java.io.ObjectInputStream stream)
                         throws java.io.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:
        java.io.InvalidObjectException - This method will throw with the message "Proxy required".