Class List.Cons<T>
java.lang.Object
io.vavr.collection.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<Integer,T>, PartialFunction<Integer, T>, Value<T>, Serializable, Iterable<T>, Function<Integer, T>
Non-empty
List, consisting of a head and a tail.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classA serialization proxy which, in this context, is used to deserialize immutable, linked Lists with final instance fields. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanIn 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.inthashCode()Returns the hash code of this collection.head()Returns the first element of a non-empty Traversable.booleanisEmpty()Checks if this Traversable is empty.intlength()Computes the number of elements of this Traversable.private voidreadObject(ObjectInputStream stream) readObjectmethod for the serialization proxy pattern.tail()Drops the first element of a non-empty Traversable.toString()Clarifies that values have a proper toString() method implemented.private ObjectwriteReplacemethod for the serialization proxy pattern.Methods inherited from interface Foldable
fold, reduce, reduceOptionMethods inherited from interface Function1
andThen, arity, compose, curried, isMemoized, memoized, partial, reversed, tupledMethods inherited from interface LinearSeq
asPartialFunction, indexOfSlice, indexWhere, isDefinedAt, lastIndexOfSlice, lastIndexWhere, reverseIterator, search, search, segmentLengthMethods inherited from interface List
append, appendAll, asJava, asJava, asJavaMutable, asJavaMutable, collect, combinations, combinations, crossProduct, distinct, distinctBy, distinctBy, drop, dropRight, dropRightUntil, dropRightWhile, dropUntil, dropWhile, filter, flatMap, get, groupBy, grouped, hasDefiniteSize, indexOf, init, initOption, insert, insertAll, intersperse, isAsync, isLazy, isTraversableAgain, last, lastIndexOf, leftPadTo, map, orElse, orElse, padTo, partition, patch, peek, peek, peekOption, permutations, pop, pop2, pop2Option, popOption, prepend, prependAll, push, push, pushAll, reject, remove, removeAll, removeAll, removeAll, removeAt, removeFirst, removeLast, replace, replaceAll, retainAll, reverse, rotateLeft, rotateRight, scan, scanLeft, scanRight, shuffle, slice, slideBy, sliding, sliding, sortBy, sortBy, sorted, sorted, span, splitAt, splitAt, splitAtInclusive, stringPrefix, subSequence, subSequence, tailOption, take, takeRight, takeRightUntil, takeRightWhile, takeUntil, takeWhile, transform, unzip, unzip3, update, update, zip, zipAll, zipWith, zipWithIndex, zipWithIndexMethods inherited from interface Seq
apply, containsSlice, crossProduct, crossProduct, endsWith, foldRight, indexOf, indexOfOption, indexOfOption, indexOfSlice, indexOfSliceOption, indexOfSliceOption, indexWhere, indexWhereOption, indexWhereOption, isSequential, iterator, lastIndexOf, lastIndexOfOption, lastIndexOfOption, lastIndexOfSlice, lastIndexOfSliceOption, lastIndexOfSliceOption, lastIndexWhere, lastIndexWhereOption, lastIndexWhereOption, lift, prefixLength, startsWith, startsWith, withDefault, withDefaultValueMethods inherited from interface Traversable
arrangeBy, average, containsAll, count, existsUnique, find, findLast, foldLeft, forEachWithIndex, get, headOption, isDistinct, isOrdered, isSingleValued, iterator, 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 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:
-
head
-
tail
-
length
private final int length
-
-
Constructor Details
-
Cons
-
-
Method Details
-
head
Description copied from interface:TraversableReturns the first element of a non-empty Traversable.- Specified by:
headin interfaceTraversable<T>- Returns:
- The first element of this Traversable.
-
length
public int length()Description copied from interface:TraversableComputes the number of elements of this Traversable.Same as
Traversable.size(). -
tail
Description copied from interface:TraversableDrops the first element of a non-empty Traversable. -
isEmpty
public boolean isEmpty()Description copied from interface:TraversableChecks if this Traversable is empty. -
equals
Description copied from interface:TraversableIn Vavr there are four basic classes of collections:- Seq (sequential elements)
- Set (distinct elements)
- Map (indexed elements)
- Multimap (indexed 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
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.
-
hashCode
public int hashCode()Description copied from interface:TraversableReturns 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:
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 = hash * 31 + Objects.hashCode(t); }
Please note that the particular hashing algorithms may change in a future version of Vavr.int hash = 1; for (T t : this) { hash += Objects.hashCode(t); }
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; } @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; } } @Override public int hashCode() { return hashCode.get(); } @Override public String toString() { return "Hashed(" + (key == null ? "null" : key.toString()) + ")"; } } -
toString
-
writeReplace
@GwtIncompatible("The Java serialization protocol is explicitly not supported") private Object writeReplace()writeReplacemethod 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 readObjectmethod 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".
-