Additional top-level class in compilation unit: ListModule.
List is an eager sequence of elements. Its immutability makes it suitable for concurrent programming.
A List is composed of a head element and a tail List.
There are two implementations of the List interface:
Nil, which represents the empty List.Cons, which represents a List containing one or more elements.List is a Stack in the sense that it stores elements allowing a last-in-first-out (LIFO) retrieval.
Stack API:
peek()peekOption()pop()popOption()pop2()pop2Option()push(Object)push(Object[])pushAll(Iterable)List:
// factory methods
List.empty() // = List.of() = Nil.instance()
List.of(x) // = new Cons<>(x, Nil.instance())
List.of(Object...) // e.g. List.of(1, 2, 3)
List.ofAll(Iterable) // e.g. List.ofAll(Stream.of(1, 2, 3)) = 1, 2, 3
List.ofAll(<primitive array>) // e.g. List.of(new int[] {1, 2, 3}) = 1, 2, 3
// int sequences
List.range(0, 3) // = 0, 1, 2
List.rangeClosed(0, 3) // = 0, 1, 2, 3
Note: A List is primarily a Seq and extends Stack for technical reasons (so Stack does not need to wrap List).
If operating on a List, please prefer
prepend(Object) over push(Object)prependAll(Iterable) over pushAll(Iterable)tail() over pop()tailOption() over popOption()
List<Integer> s1 = List.of(1);
List<Integer> s2 = List.of(1, 2, 3);
// = List.of(new Integer[] {1, 2, 3});
List<int[]> s3 = List.ofAll(1, 2, 3);
List<List<Integer>> s4 = List.ofAll(List.of(1, 2, 3));
List<Integer> s5 = List.ofAll(1, 2, 3);
List<Integer> s6 = List.ofAll(List.of(1, 2, 3));
// cuckoo's egg
List<Integer[]> s7 = List.<Integer[]> of(new Integer[] {1, 2, 3});
Example: Converting a String to digits
// = List(1, 2, 3)
List.of("123".toCharArray()).map(c -> Character.digit(c, 10))
See Okasaki, Chris: Purely Functional Data Structures (p. 7 ff.). Cambridge, 2003.
| Modifier and Type | Class and Description |
|---|---|
| public static class | |
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| public static final long |
| Modifier and Type | Method and Description |
|---|---|
| public default List | append(T
An element element)Implements io. Appends an element to this. |
| public default List | appendAll(Iterable<? extends T>
An Iterable of elements elements)Implements io. Appends all given elements to this. |
| public default List | asJava()
Implements io. Creates an immutable |
| public default List | asJava(Consumer<? super List<T>>
A side-effecting unit of work that operates on an immutable action)java.util.List view.Implements io. Creates an immutable |
| public default List | asJavaMutable()
Implements io. Creates a mutable |
| public default List | asJavaMutable(Consumer<? super List<T>>
A side-effecting unit of work that operates on a mutable action)java.util.List view.Implements io. Creates a mutable |
| public default <R> List | collect(PartialFunction<? super T, ? extends R>
A function that is not necessarily defined of all elements of this traversable. partialFunction)Implements io. Collects all elements that are in the domain of the given |
| public static < Component type of the List. T> Collector | Returns: A io.vavr.collection.List Collector.Returns a |
| public default List | combinations()
Implements io. Returns the union of all combinations from k = 0 to length(). |
| public default List | combinations(int
Size of subsets k)Implements io. Returns the k-combination of this traversable, i.e. all subset of this of k distinct elements. |
| public default Iterator | crossProduct(int
the number of cartesian multiplications power)Implements io. Calculates the n-ary cartesian power (or cross product or simply product) of this. |
| public default List | distinct()
Implements io. Returns a new version of this which contains no duplicates. |
| public default List | distinctBy(Comparator<? super T>
A comparator comparator)Implements io. Returns a new version of this which contains no duplicates. |
| public default <U> List | distinctBy(Function<? super T, ? extends U>
A key extractor keyExtractor)Implements io. Returns a new version of this which contains no duplicates. |
| public default List | drop(int
The number of elements to drop. n)Implements io. Drops the first n elements of this or all elements, if this length < n. |
| public default List | dropRight(int
The number of elements to drop. n)Implements io. Drops the last n elements of this or all elements, if this length < n. |
| public default List | dropRightUntil(Predicate<? super T>
A condition tested subsequently for this elements, starting from the end. predicate)Implements io. Drops elements until the predicate holds for the current element, starting from the end. |
| public default List | dropRightWhile(Predicate<? super T>
A condition tested subsequently for this elements, starting from the end. predicate)Implements io. Drops elements while the predicate holds for the current element, starting from the end. |
| public default List | dropUntil(Predicate<? super T>
A condition tested subsequently for this elements. predicate)Implements io. Drops elements until the predicate holds for the current element. |
| public default List | dropWhile(Predicate<? super T>
A condition tested subsequently for this elements. predicate)Implements io. Drops elements while the predicate holds for the current element. |
| public static < Component type of Nil, determined by type inference in the particular context. T> List | |
| public static < Component type of the List T> List | |
| public static < Component type of the List T> List | Returns: A List of sizen, where each element is the given element.The number of elements in the List n, T The element element)Returns a List containing |
| public default List | filter(Predicate<? super T>
A predicate predicate)Implements io. Returns a new traversable consisting of all elements which satisfy the given predicate. |
| public default <U> List | flatMap(Function<? super T, ? extends Iterable<? extends U>>
A mapper mapper)Implements io. FlatMaps this Traversable. |
| public default T | get(int
an index index)Implements io. Returns the element at the specified index. |
| public default <C> Map | groupBy(Function<? super T, ? extends C>
A function which classifies elements into classes classifier)Implements io. Groups this elements by classifying the elements. |
| public default Iterator | grouped(int
a positive block size size)Implements io. Groups this |
| public default boolean | hasDefiniteSize()
Implements io. Checks if this Traversable is known to have a finite size. |
| public default int | indexOf(T
an element element, int start index from)Implements io. Returns the index of the first occurrence of the given element after or at some start index or -1 if this does not contain the given element. |
| public default List | init()
Implements io. Dual of tail(), returning all elements except the last. |
| public default Option | initOption()
Implements io. Dual of tailOption(), returning all elements except the last as |
| public default List | insert(int
an index index, T an element element)Implements io. Inserts the given element at the specified index. |
| public default List | insertAll(int
an index index, Iterable<? extends T> An Iterable of elements elements)Implements io. Inserts the given elements at the specified index. |
| public default List | intersperse(T
An element. element)Implements io. Inserts an element between all elements of this Traversable. |
| public default boolean | |
| public boolean | isEmpty()
Redeclares as abstract: io. Checks if this Traversable is empty. |
| public default boolean | |
| public default boolean | isTraversableAgain()
Implements io. Checks if this Traversable can be repeatedly traversed. |
| public default T | |
| public default int | lastIndexOf(T
an element element, int the end index end)Implements io. Returns the index of the last occurrence of the given element before or at a given end index or -1 if this does not contain the given element. |
| public default List | leftPadTo(int
the target length length, T the padding element element)Implements io. A copy of this sequence with an element prepended until a given target length is reached. |
| public int | length()
Redeclares io. Computes the number of elements of this Traversable. |
| public default <U> List | map(Function<? super T, ? extends U>
A mapper. mapper)Implements io. Maps the elements of this |
| public static < Component type of the T> ListList. | |
| public static < The component type T> List | Returns: A new List instance containing the given elementAn element. element)Returns a singleton |
| public static < Component type of the List. T> List | Returns: A list containing the given elements in the same order.Zero or more elements. elements)Creates a List of the given elements. |
| public static < Component type of the List. T> List | |
| public static < Component type of the Stream. T> List | Returns: A List containing the given elements in the same order.Creates a List that contains the elements of the given |
| public static List | Returns: A new List of Boolean valuesboolean values elements)Creates a List from boolean values. |
| public static List | Returns: A new List of Byte valuesbyte values elements)Creates a List from byte values. |
| public static List | Returns: A new List of Character valueschar values elements)Creates a List from char values. |
| public static List | Returns: A new List of Double valuesdouble values elements)Creates a List from double values. |
| public static List | Returns: A new List of Float valuesa float values elements)Creates a List from float values. |
| public static List | Returns: A new List of Integer valuesint values elements)Creates a List from int values. |
| public static List | Returns: A new List of Long valueslong values elements)Creates a List from long values. |
| public static List | Returns: A new List of Short valuesshort values elements)Creates a List from short values. |
| public default List | orElse(Iterable<? extends T>
An alternative other)TraversableImplements io. Returns this |
| public default List | orElse(Supplier<? extends Iterable<? extends T>>
An alternative supplier)Traversable supplierImplements io. Returns this |
| public default List | padTo(int
the target length length, T the padding element element)Implements io. A copy of this sequence with an element appended until a given target length is reached. |
| public default Tuple2 | partition(Predicate<? super T>
A predicate which classifies an element if it is in the first or the second traversable. predicate)Implements io. Creates a partition of this |
| public default List | patch(int
the index of the first replaced element from, Iterable<? extends T> sequence for replacement that, int the number of elements to drop in the original list replaced)Implements io. Produces a new list where a slice of elements in this list is replaced by another sequence. |
| public default T | |
| public default List | Returns: thisListA action)ConsumerImplements io. Performs an action on the head element of this |
| public default Option | Returns: None if this List is empty, otherwise a Some containing the head elementReturns the head element without modifying the List. |
| public default List | permutations()
Implements io. Computes all unique permutations. |
| public default List | Returns: the elements of this List without the head elementRemoves the head element from this List. |
| public default Tuple2 | Returns: a tuple containing the head element and the remaining elements of this ListRemoves the head element from this List. |
| public default Option | Returns: None if this List is empty, otherwise Some Tuple containing the head element and the remaining elements of this ListRemoves the head element from this List. |
| public default Option | Returns: None if this List is empty, otherwise a Some containing the elements of this List without the head elementRemoves the head element from this List. |
| public default List | prepend(T
An element element)Implements io. Prepends an element to this. |
| public default List | prependAll(Iterable<? extends T>
An Iterable of elements elements)Implements io. Prepends all given elements to this. |
| public default List | Returns: a newList instance, containing the new element on top of this ListThe new element element)Pushes a new element on top of this List. |
| public default List | Returns: a newList instance, containing the new elements on top of this ListElements, may be empty elements)Pushes the given elements on top of this List. |
| public default List | |
| public static List | |
| public static List | Returns: a range of int values as specified or the empty range iffrom >= toExclusivethe first number from, int the last number + 1 toExclusive)Creates a List of int numbers starting from |
| public static List | Returns: a range of long values as specified or the empty range iffrom >= toExclusivethe first number from, long the last number + 1 toExclusive)Creates a List of long numbers starting from |
| public static List | |
| public static List | |
| public static List | Returns: a range of long values as specified or the empty range iffrom >= toInclusive and step > 0 orfrom <= toInclusive and step < 0the first number from, int the last number + 1 toExclusive, int the step step)Creates a List of int numbers starting from |
| public static List | Returns: a range of long values as specified or the empty range iffrom >= toInclusive and step > 0 orfrom <= toInclusive and step < 0the first number from, long the last number + 1 toExclusive, long the step step)Creates a List of long numbers starting from |
| public static List | |
| public static List | Returns: a range of int values as specified or the empty range iffrom > toInclusivethe first number from, int the last number toInclusive)Creates a List of int numbers starting from |
| public static List | Returns: a range of long values as specified or the empty range iffrom > toInclusivethe first number from, long the last number toInclusive)Creates a List of long numbers starting from |
| public static List | |
| public static List | |
| public static List | Returns: a range of int values as specified or the empty range iffrom > toInclusive and step > 0 orfrom < toInclusive and step < 0the first number from, int the last number toInclusive, int the step step)Creates a List of int numbers starting from |
| public static List | Returns: a range of int values as specified or the empty range iffrom > toInclusive and step > 0 orfrom < toInclusive and step < 0the first number from, long the last number toInclusive, long the step step)Creates a List of long numbers starting from |
| public default List | reject(Predicate<? super T>
A predicate predicate)Implements io. Returns a new traversable consisting of all elements which do not satisfy the given predicate. |
| public default List | remove(T
An element to be removed from this Seq. element)Implements io. Removes the first occurrence of the given element. |
| public default List | removeAll(T
An element to be removed from this Seq. element)Implements io. Removes all occurrences of the given element. |
| public default List | removeAll(Iterable<? extends T>
Elements to be removed from this Seq. elements)Implements io. Removes all occurrences of the given elements. |
| public default List | removeAll(Predicate<? super T>
the predicate used to test elements predicate)Implements io.
Deprecated
Returns a new Seq consisting of all elements which do not satisfy the given predicate. |
| public default List | removeAt(int
position of element to remove index)Implements io. Removes the element at the specified position in this sequence. |
| public default List | removeFirst(Predicate<T>
an predicate predicate)Implements io. Removes the first occurrence that satisfy predicate |
| public default List | removeLast(Predicate<T>
an predicate predicate)Implements io. Removes the last occurrence that satisfy predicate |
| public default List | replace(T
An element to be substituted. currentElement, T A replacement for currentElement. newElement)Implements io. Replaces the first occurrence (if exists) of the given currentElement with newElement. |
| public default List | replaceAll(T
An element to be substituted. currentElement, T A replacement for currentElement. newElement)Implements io. Replaces all occurrences of the given currentElement with newElement. |
| public default List | retainAll(Iterable<? extends T>
Elements to be kept. elements)Implements io. Keeps all occurrences of the given elements from this. |
| public default List | |
| public default List | rotateLeft(int
distance of left rotation n)Implements io. Circular rotates the elements by the specified distance to the left direction. |
| public default List | rotateRight(int
distance of right rotation n)Implements io. Circular rotates the elements by the specified distance to the right direction. |
| public default List | scan(T
neutral element for the operator op zero, BiFunction<? super T, ? super T, ? extends T> the associative operator for the scan operation)Implements io. Computes a prefix scan of the elements of the collection. |
| public default <U> List | scanLeft(U
the initial value zero, BiFunction<? super U, ? super T, ? extends U> the binary operator applied to the intermediate result and the element operation)Implements io. Produces a collection containing cumulative results of applying the operator going left to right. |
| public default <U> List | scanRight(U
the initial value zero, BiFunction<? super T, ? super U, ? extends U> the binary operator applied to the intermediate result and the element operation)Implements io. Produces a collection containing cumulative results of applying the operator going right to left. |
| public default List | shuffle()
Implements io. Randomize the order of the elements in the current sequence. |
| public default List | slice(int
the beginning index, inclusive beginIndex, int the end index, exclusive endIndex)Implements io. Returns a Seq that is a slice of this. |
| public default Iterator | slideBy(Function<? super T, ?>
A function which classifies elements into classes classifier)Implements io. Slides a non-overlapping window of a variable size over this |
| public default Iterator | sliding(int
a positive window size size)Implements io. Slides a window of a specific |
| public default Iterator | sliding(int
a positive window size size, int a positive step size step)Implements io. Slides a window of a specific |
| public default <U extends Comparable | sortBy(Function<? super T, ? extends U>
A mapper mapper)Implements io. Sorts this elements by comparing the elements in a different domain, using the given |
| public default <U> List | sortBy(Comparator<? super U>
A comparator comparator, Function<? super T, ? extends U> A mapper mapper)Implements io. Sorts this elements by comparing the elements in a different domain, using the given |
| public default List | sorted()
Implements io. Sorts this elements according to their natural order. |
| public default List | sorted(Comparator<? super T>
A comparator comparator)Implements io. Sorts this elements according to the provided |
| public default Tuple2 | span(Predicate<? super T>
A predicate. predicate)Implements io. Returns a tuple where the first element is the longest prefix of elements that satisfy the given
|
| public default Tuple2 | splitAt(int
An index. n)Implements io. Splits a Seq at the specified index. |
| public default Tuple2 | splitAt(Predicate<? super T>
An predicate predicate)Implements io. Splits a sequence at the first element which satisfies the |
| public default Tuple2 | splitAtInclusive(Predicate<? super T>
An predicate predicate)Implements io. Splits a sequence at the first element which satisfies the |
| public default String | stringPrefix()
Implements io. Returns the name of this Value type, which is used by toString(). |
| public default List | subSequence(int
the beginning index, inclusive beginIndex)Implements io. Returns a Seq that is a subsequence of this. |
| public default List | subSequence(int
the beginning index, inclusive beginIndex, int the end index, exclusive endIndex)Implements io. Returns a Seq that is a subsequence of this. |
| public static < Component type of the List T> List | |
| public List | tail()
Redeclares io. Drops the first element of a non-empty Traversable. |
| public default Option | tailOption()
Implements io. Drops the first element of a non-empty Traversable and returns an |
| public default List | take(int
The number of elements to take. n)Implements io. Takes the first n elements of this or all elements, if this length < n. |
| public default List | takeRight(int
The number of elements to take. n)Implements io. Takes the last n elements of this or all elements, if this length < n. |
| public default List | takeRightUntil(Predicate<? super T>
A condition tested subsequently for this elements, starting from the end. predicate)Implements io. Takes elements until the predicate holds for the current element, starting from the end. |
| public default List | takeRightWhile(Predicate<? super T>
A condition tested subsequently for this elements, starting from the end. predicate)Implements io. Takes elements while the predicate holds for the current element, starting from the end. |
| public default List | takeUntil(Predicate<? super T>
A condition tested subsequently for this elements. predicate)Implements io. Takes elements until the predicate holds for the current element. |
| public default List | takeWhile(Predicate<? super T>
A condition tested subsequently for the contained elements. predicate)Implements io. Takes elements while the predicate holds for the current element. |
| public default < Type of transformation result U> U | |
| public static < matrix element type T> List | |
| public static < type of seeds and unfolded values T> List | |
| public static < type of seeds T, type of unfolded values U> List | Returns: a list with the values built up by the iterationthe start value for the iteration seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> the function to get the next step of the iteration f)Creates a list from a seed value and a function. |
| public static < type of seeds T, type of unfolded values U> List | Returns: a list with the values built up by the iterationthe start value for the iteration seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> the function to get the next step of the iteration f)Creates a list from a seed value and a function. |
| public default <T1, T2> Tuple2 | unzip(Function<? super T, Tuple2<? extends T1, ? extends T2>>
a function which converts elements of this to pairs unzipper)Implements io. Unzips this elements by mapping this elements to pairs which are subsequently split into two distinct sets. |
| public default <T1, T2, T3> Tuple3 | unzip3(Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>>
a function which converts elements of this to pairs unzipper)Implements io. Unzips this elements by mapping this elements to triples which are subsequently split into three distinct sets. |
| public default List | update(int
an index index, T an element element)Implements io. Updates the given element at the specified index. |
| public default List | update(int
an index index, Function<? super T, ? extends T> a function transforming the previous value updater)Implements io. Updates the given element at the specified index using the specified function. |
| public default <U> List | zip(Iterable<? extends U>
The Iterable providing the second half of each result pair. that)Implements io. Returns a traversable formed from this traversable and another Iterable collection by combining corresponding elements in pairs. |
| public default <U> List | zipAll(Iterable<? extends U>
The Iterable providing the second half of each result pair. that, T The element to be used to fill up the result if this traversable is shorter than that. thisElem, U The element to be used to fill up the result if that is shorter than this traversable. thatElem)Implements io. Returns a traversable formed from this traversable and another Iterable by combining corresponding elements in pairs. |
| public default <U, R> List | zipWith(Iterable<? extends U>
The Iterable providing the second parameter of the mapper. that, BiFunction<? super T, ? super U, ? extends R> a mapper. mapper)Implements io. Returns a traversable formed from this traversable and another Iterable collection by mapping elements. |
| public default List | zipWithIndex()
Implements io. Zips this traversable with its indices. |
| public default <U> List | zipWithIndex(BiFunction<? super T, ? super Integer, ? extends U>
a mapper. mapper)Implements io. Zips this traversable with its indices by applying mapper provided. |