Interface List<T>
-
- Type Parameters:
T- Component type of the List
- All Superinterfaces:
Foldable<T>,java.util.function.Function<java.lang.Integer,T>,Function1<java.lang.Integer,T>,java.lang.Iterable<T>,LinearSeq<T>,PartialFunction<java.lang.Integer,T>,Seq<T>,java.io.Serializable,Traversable<T>,Value<T>
public interface List<T> extends LinearSeq<T>
An immutableListis an eager sequence of elements. Its immutability makes it suitable for concurrent programming.A
Listis composed of aheadelement and atailList.There are two implementations of the
Listinterface:List.Nil, which represents the emptyList.List.Cons, which represents aListcontaining one or more elements.
Listis aStackin 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:
Note: A// 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, 3Listis primarily aSeqand extendsStackfor technical reasons (soStackdoes not need to wrapList).If operating on a
List, please preferprepend(Object)overpush(Object)prependAll(Iterable)overpushAll(Iterable)tail()overpop()tailOption()overpopOption()
Example: Converting a String to digitsList<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});
See Okasaki, Chris: Purely Functional Data Structures (p. 7 ff.). Cambridge, 2003.// = List(1, 2, 3) List.of("123".toCharArray()).map(c -> Character.digit(c, 10))
-
-
Field Summary
Fields Modifier and Type Field Description static longserialVersionUIDThe serial version UID for serialization.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default List<T>append(T element)Returns a new sequence with the given element appended at the end.default List<T>appendAll(@NonNull java.lang.Iterable<? extends T> elements)Returns a new sequence with all elements from the givenIterableappended at the end of this sequence.default java.util.List<T>asJava()Returns an immutableListview of thisSeq.default List<T>asJava(@NonNull java.util.function.Consumer<? super java.util.List<T>> action)Creates an immutableListview of thisSeqand passes it to the givenaction.default java.util.List<T>asJavaMutable()Returns a mutableListview of thisSeq.default List<T>asJavaMutable(@NonNull java.util.function.Consumer<? super java.util.List<T>> action)Creates a mutableListview of thisSeqand passes it to the givenaction.default <R> List<R>collect(@NonNull PartialFunction<? super T,? extends R> partialFunction)Applies aPartialFunctionto all elements that are defined for it and collects the results.static <T> java.util.stream.Collector<T,java.util.ArrayList<T>,List<T>>collector()Returns aCollectorwhich may be used in conjunction withStream.collect(java.util.stream.Collector)to obtain aList.default List<List<T>>combinations()Returns a sequence containing all combinations of elements from this sequence, for all sizes from0tolength().default List<List<T>>combinations(int k)Returns all subsets of this sequence containing exactlykdistinct elements, i.e., the k-combinations of this sequence.default Iterator<List<T>>crossProduct(int power)Returns the n-ary Cartesian power (cross product) of this sequence.default List<T>distinct()Returns a newTraversablecontaining the elements of this instance with all duplicates removed.default List<T>distinctBy(@NonNull java.util.Comparator<? super T> comparator)Returns a newTraversablecontaining the elements of this instance without duplicates, as determined by the givencomparator.default <U> List<T>distinctBy(@NonNull java.util.function.Function<? super T,? extends U> keyExtractor)Returns a newTraversablecontaining the elements of this instance without duplicates, based on keys extracted from elements usingkeyExtractor.default List<T>distinctByKeepLast(@NonNull java.util.Comparator<? super T> comparator)Returns a sequence with duplicate elements removed, as determined by the provided comparator.default <U> List<T>distinctByKeepLast(@NonNull java.util.function.Function<? super T,? extends U> keyExtractor)Returns a sequence with duplicates removed based on a key extracted from each element.default List<T>drop(int n)Returns a newTraversablewithout the firstnelements, or an empty instance if this contains fewer thannelements.default List<T>dropRight(int n)Returns a newTraversablewithout the lastnelements, or an empty instance if this contains fewer thannelements.default List<T>dropRightUntil(@NonNull java.util.function.Predicate<? super T> predicate)Drops elements from the end of the sequence until an element satisfies the given predicate.default List<T>dropRightWhile(@NonNull java.util.function.Predicate<? super T> predicate)Drops elements from the end of the sequence while the given predicate holds.default List<T>dropUntil(@NonNull java.util.function.Predicate<? super T> predicate)Returns a newTraversablestarting from the first element that satisfies the givenpredicate, dropping all preceding elements.default List<T>dropWhile(@NonNull java.util.function.Predicate<? super T> predicate)Returns a newTraversablestarting from the first element that does not satisfy the givenpredicate, dropping all preceding elements.static <T> List<T>empty()Returns the single instance of Nil.static <T> List<T>fill(int n, @NonNull java.util.function.Supplier<? extends T> s)Returns a List containingnvalues supplied by a given Suppliers.static <T> List<T>fill(int n, T element)Returns a List containingntimes the givenelementdefault List<T>filter(@NonNull java.util.function.Predicate<? super T> predicate)Returns a new traversable containing only the elements that satisfy the given predicate.default <U> List<U>flatMap(@NonNull java.util.function.Function<? super T,? extends java.lang.Iterable<? extends U>> mapper)Transforms each element of this Traversable into anIterableof elements and flattens the resulting iterables into a single Traversable.default Tget(int index)Returns the element at the specified index.default <C> Map<C,List<T>>groupBy(@NonNull java.util.function.Function<? super T,? extends C> classifier)Groups elements of thisTraversablebased on a classifier function.default Iterator<List<T>>grouped(int size)Splits thisTraversableinto consecutive blocks of the given size.default booleanhasDefiniteSize()Indicates whether thisTraversablehas a known finite size.default intindexOf(T element, int from)Returns the index of the first occurrence of the given element, starting at the specified index, or-1if this sequence does not contain the element.default List<T>init()Returns all elements of this Traversable except the last one.default Option<List<T>>initOption()Returns all elements of this Traversable except the last one, wrapped in anOption.default List<T>insert(int index, T element)Returns a new sequence with the given element inserted at the specified index.default List<T>insertAll(int index, @NonNull java.lang.Iterable<? extends T> elements)Returns a new sequence with the given elements inserted at the specified index.default List<T>intersperse(T element)Returns a new sequence where the given element is inserted between all elements of this sequence.default booleanisAsync()AListis computed synchronously.booleanisEmpty()Checks if this Traversable contains no elements.default booleanisLazy()AListis computed eagerly.default booleanisTraversableAgain()Checks if this Traversable can be traversed multiple times without side effects.default Tlast()Returns the last element of this Traversable.default intlastIndexOf(T element, int end)Returns the index of the last occurrence of the given element at or before the specified end index, or-1if this sequence does not contain the element.default List<T>leftPadTo(int length, T element)Returns a new sequence with this sequence padded on the left with the given element until the specified target length is reached.intlength()Returns the number of elements in this Traversable.default <U> List<U>map(@NonNull java.util.function.Function<? super T,? extends U> mapper)Transforms the elements of this Traversable to a new type, preserving order if defined.default <U> List<U>mapTo(U value)Maps the underlying value to another fixed value.default List<java.lang.Void>mapToVoid()Maps the underlying value to Voidstatic <T> List<T>narrow(List<? extends T> list)Narrows a widenedList<? extends T>toList<T>by performing a type-safe cast.static <T> List<T>of(T element)Returns a singletonList, i.e.static <T> List<T>of(T @NonNull ... elements)Creates a List of the given elements.static List<java.lang.Boolean>ofAll(boolean @NonNull ... elements)Creates a List from boolean values.static List<java.lang.Byte>ofAll(byte @NonNull ... elements)Creates a List from byte values.static List<java.lang.Character>ofAll(char @NonNull ... elements)Creates a List from char values.static List<java.lang.Double>ofAll(double @NonNull ... elements)Creates a List from double values.static List<java.lang.Float>ofAll(float @NonNull ... elements)Creates a List from float values.static List<java.lang.Integer>ofAll(int @NonNull ... elements)Creates a List from int values.static List<java.lang.Long>ofAll(long @NonNull ... elements)Creates a List from long values.static List<java.lang.Short>ofAll(short @NonNull ... elements)Creates a List from short values.static <T> List<T>ofAll(@NonNull java.lang.Iterable<? extends T> elements)Creates a List of the given elements.static <T> List<T>ofAll(@NonNull java.util.stream.Stream<? extends T> javaStream)Creates a List that contains the elements of the givenStream.default List<T>orElse(@NonNull java.lang.Iterable<? extends T> other)Returns thisTraversableif it is non-empty; otherwise, returns the given alternative.default List<T>orElse(@NonNull java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)Returns thisTraversableif it is non-empty; otherwise, returns the result of evaluating the given supplier.default List<T>padTo(int length, T element)Returns a new sequence with this sequence padded on the right with the given element until the specified target length is reached.default Tuple2<List<T>,List<T>>partition(@NonNull java.util.function.Predicate<? super T> predicate)Splits thisTraversableinto two partitions according to a predicate.default List<T>patch(int from, @NonNull java.lang.Iterable<? extends T> that, int replaced)Returns a new sequence in which a slice of elements in this sequence is replaced by the elements of another sequence.default Tpeek()Returns the head element without modifying the List.default List<T>peek(@NonNull java.util.function.Consumer<? super T> action)Performs an action on the head element of thisList.default Option<T>peekOption()Returns the head element without modifying the List.default List<List<T>>permutations()Returns all unique permutations of this sequence.default List<T>pop()Removes the head element from this List.default Tuple2<T,List<T>>pop2()Removes the head element from this List.default Option<Tuple2<T,List<T>>>pop2Option()Removes the head element from this List.default Option<List<T>>popOption()Removes the head element from this List.default List<T>prepend(T element)Returns a new sequence with the given element prepended to this sequence.default List<T>prependAll(@NonNull java.lang.Iterable<? extends T> elements)Returns a new sequence with all given elements prepended to this sequence.default List<T>push(T element)Pushes a new element on top of this List.default List<T>push(T @NonNull ... elements)Pushes the given elements on top of this List.default List<T>pushAll(@NonNull java.lang.Iterable<T> elements)Pushes the given elements on top of this List.static List<java.lang.Character>range(char from, char toExclusive)Creates a List of char numbers starting fromfrom, extending totoExclusive - 1.static List<java.lang.Integer>range(int from, int toExclusive)Creates a List of int numbers starting fromfrom, extending totoExclusive - 1.static List<java.lang.Long>range(long from, long toExclusive)Creates a List of long numbers starting fromfrom, extending totoExclusive - 1.static List<java.lang.Character>rangeBy(char from, char toExclusive, int step)Creates a List of char numbers starting fromfrom, extending totoExclusive - 1, withstep.static List<java.lang.Double>rangeBy(double from, double toExclusive, double step)Creates a List of double numbers starting fromfrom, extending up to but not includingtoExclusive, withstep.static List<java.lang.Integer>rangeBy(int from, int toExclusive, int step)Creates a List of int numbers starting fromfrom, extending totoExclusive - 1, withstep.static List<java.lang.Long>rangeBy(long from, long toExclusive, long step)Creates a List of long numbers starting fromfrom, extending totoExclusive - 1, withstep.static List<java.lang.Character>rangeClosed(char from, char toInclusive)Creates a List of char numbers starting fromfrom, extending totoInclusive.static List<java.lang.Integer>rangeClosed(int from, int toInclusive)Creates a List of int numbers starting fromfrom, extending totoInclusive.static List<java.lang.Long>rangeClosed(long from, long toInclusive)Creates a List of long numbers starting fromfrom, extending totoInclusive.static List<java.lang.Character>rangeClosedBy(char from, char toInclusive, int step)Creates a List of char numbers starting fromfrom, extending totoInclusive, withstep.static List<java.lang.Double>rangeClosedBy(double from, double toInclusive, double step)Creates a List of double numbers starting fromfrom, extending totoInclusive, withstep.static List<java.lang.Integer>rangeClosedBy(int from, int toInclusive, int step)Creates a List of int numbers starting fromfrom, extending totoInclusive, withstep.static List<java.lang.Long>rangeClosedBy(long from, long toInclusive, long step)Creates a List of long numbers starting fromfrom, extending totoInclusive, withstep.default List<T>reject(@NonNull java.util.function.Predicate<? super T> predicate)Returns a new traversable containing only the elements that do not satisfy the given predicate.default List<T>remove(T element)Returns a new sequence with the first occurrence of the given element removed.default List<T>removeAll(@NonNull java.lang.Iterable<? extends T> elements)Returns a new sequence with all occurrences of the given elements removed.default List<T>removeAll(@NonNull java.util.function.Predicate<? super T> predicate)Deprecated.default List<T>removeAll(T element)Returns a new sequence with all occurrences of the given element removed.default List<T>removeAt(int index)Returns a new sequence with the element at the specified position removed.default List<T>removeFirst(@NonNull java.util.function.Predicate<T> predicate)Returns a new sequence with the first element that satisfies the given predicate removed.default List<T>removeLast(@NonNull java.util.function.Predicate<T> predicate)Returns a new sequence with the last element that satisfies the given predicate removed.default List<T>replace(T currentElement, T newElement)Replaces the first occurrence ofcurrentElementwithnewElement, if it exists.default List<T>replaceAll(T currentElement, T newElement)Replaces all occurrences ofcurrentElementwithnewElement.default List<T>retainAll(@NonNull java.lang.Iterable<? extends T> elements)Retains only the elements from this Traversable that are contained in the givenelements.default List<T>reverse()Returns a new sequence with the order of elements reversed.default List<T>rotateLeft(int n)Returns a new sequence with the elements circularly rotated to the left by the specified distance.default List<T>rotateRight(int n)Returns a new sequence with the elements circularly rotated to the right by the specified distance.default List<T>scan(T zero, @NonNull java.util.function.BiFunction<? super T,? super T,? extends T> operation)Computes a prefix scan of the elements of this Traversable.default <U> List<U>scanLeft(U zero, @NonNull java.util.function.BiFunction<? super U,? super T,? extends U> operation)Produces a collection containing cumulative results of applying the operator from left to right.default <U> List<U>scanRight(U zero, @NonNull java.util.function.BiFunction<? super T,? super U,? extends U> operation)Produces a collection containing cumulative results of applying the operator from right to left.default List<T>shuffle()Returns a new sequence with the elements randomly shuffled.default List<T>slice(int beginIndex, int endIndex)Returns a subsequence (slice) of this sequence, starting atbeginIndex(inclusive) and ending atendIndex(exclusive).default Iterator<List<T>>slideBy(@NonNull java.util.function.Function<? super T,?> classifier)Partitions thisTraversableinto consecutive non-overlapping windows according to a classification function.default Iterator<List<T>>sliding(int size)Slides a window of a givensizeover thisTraversablewith a step size of 1.default Iterator<List<T>>sliding(int size, int step)Slides a window of a specificsizewith a givenstepover thisTraversable.default <U> List<T>sortBy(@NonNull java.util.Comparator<? super U> comparator, java.util.function.Function<? super T,? extends U> mapper)Returns a new sequence sorted by comparing elements in a different domain defined by the givenmapper, using the providedcomparator.default <U extends java.lang.Comparable<? super U>>
List<T>sortBy(@NonNull java.util.function.Function<? super T,? extends U> mapper)Returns a new sequence sorted by comparing elements in a different domain defined by the givenmapper.default List<T>sorted()Returns a new sequence with elements sorted according to their natural order.default List<T>sorted(@NonNull java.util.Comparator<? super T> comparator)Returns a new sequence with elements sorted according to the givenComparator.default Tuple2<List<T>,List<T>>span(@NonNull java.util.function.Predicate<? super T> predicate)Splits thisTraversableinto a prefix and remainder according to the givenpredicate.default Tuple2<List<T>,List<T>>splitAt(int n)Splits this sequence at the specified index.default Tuple2<List<T>,List<T>>splitAt(@NonNull java.util.function.Predicate<? super T> predicate)Splits this sequence at the first element satisfying the given predicate.default Tuple2<List<T>,List<T>>splitAtInclusive(@NonNull java.util.function.Predicate<? super T> predicate)Splits this sequence at the first element satisfying the given predicate, including the element in the first part.default java.lang.StringstringPrefix()Returns the name of this Value type, which is used by toString().default List<T>subSequence(int beginIndex)Returns aSeqthat is a subsequence of this sequence, starting from the specifiedbeginIndexand extending to the end of this sequence.default List<T>subSequence(int beginIndex, int endIndex)Returns aSeqthat is a subsequence of this sequence, starting from the specifiedbeginIndex(inclusive) and ending atendIndex(exclusive).static <T> List<T>tabulate(int n, @NonNull java.util.function.Function<? super java.lang.Integer,? extends T> f)Returns a List containingnvalues of a given Functionfover a range of integer values from 0 ton - 1.List<T>tail()Returns a newTraversablewithout its first element.default Option<List<T>>tailOption()Returns a newTraversablewithout its first element as anOption.default List<T>take(int n)Returns the firstnelements of thisTraversable, or all elements ifnexceeds the length.default List<T>takeRight(int n)Returns the lastnelements of thisTraversable, or all elements ifnexceeds the length.default List<T>takeRightUntil(@NonNull java.util.function.Predicate<? super T> predicate)Takes elements from the end of the sequence until an element satisfies the given predicate.default List<T>takeRightWhile(@NonNull java.util.function.Predicate<? super T> predicate)Takes elements from the end of the sequence while the given predicate holds.default List<T>takeUntil(@NonNull java.util.function.Predicate<? super T> predicate)Takes elements from thisTraversableuntil the given predicate holds for an element.default List<T>takeWhile(@NonNull java.util.function.Predicate<? super T> predicate)Takes elements from thisTraversablewhile the given predicate holds.default <U> Utransform(@NonNull java.util.function.Function<? super List<T>,? extends U> f)Transforms thisList.static <T> List<List<T>>transpose(@NonNull List<List<T>> matrix)Transposes the rows and columns of aListmatrix.static <T> List<T>unfold(T seed, @NonNull java.util.function.Function<? super T,Option<Tuple2<? extends T,? extends T>>> f)Creates a list from a seed value and a function.static <T,U>
List<U>unfoldLeft(T seed, @NonNull java.util.function.Function<? super T,Option<Tuple2<? extends T,? extends U>>> f)Creates a list from a seed value and a function.static <T,U>
List<U>unfoldRight(T seed, @NonNull java.util.function.Function<? super T,@NonNull Option<Tuple2<? extends U,? extends T>>> f)Creates a list from a seed value and a function.default <T1,T2>
Tuple2<List<T1>,List<T2>>unzip(@NonNull java.util.function.Function<? super T,Tuple2<? extends T1,? extends T2>> unzipper)Unzips the elements of thisTraversableby mapping each element to a pair and splitting them into two separateTraversablecollections.default <T1,T2,T3>
Tuple3<List<T1>,List<T2>,List<T3>>unzip3(@NonNull java.util.function.Function<? super T,Tuple3<? extends T1,? extends T2,? extends T3>> unzipper)Unzips the elements of thisTraversableby mapping each element to a triple and splitting them into three separateTraversablecollections.default List<T>update(int index, @NonNull java.util.function.Function<? super T,? extends T> updater)Returns a newSeqwith the element at the specified index updated using the given function.default List<T>update(int index, T element)Returns a newSeqwith the element at the specified index replaced by the given value.default <U> List<Tuple2<T,U>>zip(@NonNull java.lang.Iterable<? extends U> that)Returns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable.default <U> List<Tuple2<T,U>>zipAll(@NonNull java.lang.Iterable<? extends U> that, T thisElem, U thatElem)Returns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable, filling in placeholder elements when one collection is shorter than the other.default <U,R>
List<R>zipWith(@NonNull java.lang.Iterable<? extends U> that, java.util.function.BiFunction<? super T,? super U,? extends R> mapper)Returns aTraversableby combining elements of thisTraversablewith elements of anotherIterableusing a mapping function.default List<Tuple2<T,java.lang.Integer>>zipWithIndex()Zips thisTraversablewith its indices, starting at 0.default <U> List<U>zipWithIndex(@NonNull java.util.function.BiFunction<? super T,? super java.lang.Integer,? extends U> mapper)Zips thisTraversablewith its indices and maps the resulting pairs using the provided mapper.-
Methods inherited from interface io.vavr.collection.Foldable
fold, reduce, reduceOption
-
Methods inherited from interface io.vavr.Function1
andThen, arity, compose, compose1, curried, isMemoized, memoized, partial, reversed, tupled
-
Methods inherited from interface io.vavr.collection.LinearSeq
asPartialFunction, indexOfSlice, indexWhere, isDefinedAt, lastIndexOfSlice, lastIndexWhere, reverseIterator, search, search, segmentLength
-
Methods inherited from interface io.vavr.collection.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, withDefaultValue
-
Methods inherited from interface io.vavr.collection.Traversable
arrangeBy, average, containsAll, count, equals, existsUnique, find, findLast, foldLeft, forEachWithIndex, get, hashCode, head, 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, sum
-
Methods inherited from interface io.vavr.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, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
The serial version UID for serialization.- See Also:
- Constant Field Values
-
-
Method Detail
-
collector
static <T> java.util.stream.Collector<T,java.util.ArrayList<T>,List<T>> collector()
Returns aCollectorwhich may be used in conjunction withStream.collect(java.util.stream.Collector)to obtain aList.- Type Parameters:
T- Component type of the List.- Returns:
- A io.vavr.collection.List Collector.
-
empty
static <T> List<T> empty()
Returns the single instance of Nil. Convenience method forNil.instance().Note: this method intentionally returns type
Listand notNil. This comes in handy when folding. If you explicitly need typeNiluse List.Nil.instance().- Type Parameters:
T- Component type of Nil, determined by type inference in the particular context.- Returns:
- The empty list.
-
isAsync
default boolean isAsync()
AListis computed synchronously.
-
isEmpty
boolean isEmpty()
Description copied from interface:TraversableChecks if this Traversable contains no elements.
-
isLazy
default boolean isLazy()
AListis computed eagerly.
-
narrow
static <T> List<T> narrow(List<? extends T> list)
Narrows a widenedList<? extends T>toList<T>by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
T- Component type of theList.- Parameters:
list- AList.- Returns:
- the given
listinstance as narrowed typeList<T>.
-
of
static <T> List<T> of(T element)
Returns a singletonList, i.e. aListof one element.- Type Parameters:
T- The component type- Parameters:
element- An element.- Returns:
- A new List instance containing the given element
-
of
@SafeVarargs static <T> List<T> of(T @NonNull ... elements)
Creates a List of the given elements.List.of(1, 2, 3, 4) = Nil.instance().prepend(4).prepend(3).prepend(2).prepend(1) = new Cons(1, new Cons(2, new Cons(3, new Cons(4, Nil.instance()))))- Type Parameters:
T- Component type of the List.- Parameters:
elements- Zero or more elements.- Returns:
- A list containing the given elements in the same order.
- Throws:
java.lang.NullPointerException- ifelementsis null
-
ofAll
static <T> List<T> ofAll(@NonNull java.lang.Iterable<? extends T> elements)
Creates a List of the given elements.The resulting list has the same iteration order as the given iterable of elements if the iteration order of the elements is stable.
- Type Parameters:
T- Component type of the List.- Parameters:
elements- An Iterable of elements.- Returns:
- A list containing the given elements in the same order.
- Throws:
java.lang.NullPointerException- ifelementsis null
-
ofAll
static <T> List<T> ofAll(@NonNull java.util.stream.Stream<? extends T> javaStream)
Creates a List that contains the elements of the givenStream.- Type Parameters:
T- Component type of the Stream.- Parameters:
javaStream- AStream- Returns:
- A List containing the given elements in the same order.
-
ofAll
static List<java.lang.Boolean> ofAll(boolean @NonNull ... elements)
Creates a List from boolean values.- Parameters:
elements- boolean values- Returns:
- A new List of Boolean values
- Throws:
java.lang.NullPointerException- if elements is null
-
ofAll
static List<java.lang.Byte> ofAll(byte @NonNull ... elements)
Creates a List from byte values.- Parameters:
elements- byte values- Returns:
- A new List of Byte values
- Throws:
java.lang.NullPointerException- if elements is null
-
ofAll
static List<java.lang.Character> ofAll(char @NonNull ... elements)
Creates a List from char values.- Parameters:
elements- char values- Returns:
- A new List of Character values
- Throws:
java.lang.NullPointerException- if elements is null
-
ofAll
static List<java.lang.Double> ofAll(double @NonNull ... elements)
Creates a List from double values.- Parameters:
elements- double values- Returns:
- A new List of Double values
- Throws:
java.lang.NullPointerException- if elements is null
-
ofAll
static List<java.lang.Float> ofAll(float @NonNull ... elements)
Creates a List from float values.- Parameters:
elements- a float values- Returns:
- A new List of Float values
- Throws:
java.lang.NullPointerException- if elements is null
-
ofAll
static List<java.lang.Integer> ofAll(int @NonNull ... elements)
Creates a List from int values.- Parameters:
elements- int values- Returns:
- A new List of Integer values
- Throws:
java.lang.NullPointerException- if elements is null
-
ofAll
static List<java.lang.Long> ofAll(long @NonNull ... elements)
Creates a List from long values.- Parameters:
elements- long values- Returns:
- A new List of Long values
- Throws:
java.lang.NullPointerException- if elements is null
-
ofAll
static List<java.lang.Short> ofAll(short @NonNull ... elements)
Creates a List from short values.- Parameters:
elements- short values- Returns:
- A new List of Short values
- Throws:
java.lang.NullPointerException- if elements is null
-
tabulate
static <T> List<T> tabulate(int n, @NonNull java.util.function.Function<? super java.lang.Integer,? extends T> f)
Returns a List containingnvalues of a given Functionfover a range of integer values from 0 ton - 1.- Type Parameters:
T- Component type of the List- Parameters:
n- The number of elements in the Listf- The Function computing element values- Returns:
- A List consisting of elements
f(0),f(1), ..., f(n - 1) - Throws:
java.lang.NullPointerException- iffis null
-
fill
static <T> List<T> fill(int n, @NonNull java.util.function.Supplier<? extends T> s)
Returns a List containingnvalues supplied by a given Suppliers.- Type Parameters:
T- Component type of the List- Parameters:
n- The number of elements in the Lists- The Supplier computing element values- Returns:
- A List of size
n, where each element contains the result supplied bys. - Throws:
java.lang.NullPointerException- ifsis null
-
fill
static <T> List<T> fill(int n, T element)
Returns a List containingntimes the givenelement- Type Parameters:
T- Component type of the List- Parameters:
n- The number of elements in the Listelement- The element- Returns:
- A List of size
n, where each element is the givenelement.
-
range
static List<java.lang.Character> range(char from, char toExclusive)
Creates a List of char numbers starting fromfrom, extending totoExclusive - 1.Examples:
List.range('a', 'a') // = List() List.range('c', 'a') // = List() List.range('a', 'd') // = List('a', 'b', 'c')- Parameters:
from- the first chartoExclusive- the last char + 1- Returns:
- a range of char values as specified or the empty range if
from >= toExclusive
-
rangeBy
static List<java.lang.Character> rangeBy(char from, char toExclusive, int step)
Creates a List of char numbers starting fromfrom, extending totoExclusive - 1, withstep.Examples:
List.rangeBy('a', 'c', 1) // = List('a', 'b') List.rangeBy('a', 'd', 2) // = List('a', 'c') List.rangeBy('d', 'a', -2) // = List('d', 'b') List.rangeBy('d', 'a', 2) // = List()- Parameters:
from- the first chartoExclusive- the last char + 1step- the step- Returns:
- a range of char values as specified or the empty range if
from >= toExclusiveandstep > 0or
from <= toExclusiveandstep < 0 - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeBy
@GwtIncompatible static List<java.lang.Double> rangeBy(double from, double toExclusive, double step)
Creates a List of double numbers starting fromfrom, extending up to but not includingtoExclusive, withstep.Examples:
List.rangeBy(1.0, 3.0, 1.0) // = List(1.0, 2.0) List.rangeBy(1.0, 4.0, 2.0) // = List(1.0, 3.0) List.rangeBy(4.0, 1.0, -2.0) // = List(4.0, 2.0) List.rangeBy(4.0, 1.0, 2.0) // = List()- Parameters:
from- the first doubletoExclusive- the upper bound (exclusive)step- the step- Returns:
- a range of double values as specified or the empty range if
from >= toExclusiveandstep > 0or
from <= toExclusiveandstep < 0 - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
range
static List<java.lang.Integer> range(int from, int toExclusive)
Creates a List of int numbers starting fromfrom, extending totoExclusive - 1.Examples:
List.range(0, 0) // = List() List.range(2, 0) // = List() List.range(-2, 2) // = List(-2, -1, 0, 1)- Parameters:
from- the first numbertoExclusive- the last number + 1- Returns:
- a range of int values as specified or the empty range if
from >= toExclusive
-
rangeBy
static List<java.lang.Integer> rangeBy(int from, int toExclusive, int step)
Creates a List of int numbers starting fromfrom, extending totoExclusive - 1, withstep.Examples:
List.rangeBy(1, 3, 1) // = List(1, 2) List.rangeBy(1, 4, 2) // = List(1, 3) List.rangeBy(4, 1, -2) // = List(4, 2) List.rangeBy(4, 1, 2) // = List()- Parameters:
from- the first numbertoExclusive- the last number + 1step- the step- Returns:
- a range of long values as specified or the empty range if
from >= toInclusiveandstep > 0or
from <= toInclusiveandstep < 0 - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
range
static List<java.lang.Long> range(long from, long toExclusive)
Creates a List of long numbers starting fromfrom, extending totoExclusive - 1.Examples:
List.range(0L, 0L) // = List() List.range(2L, 0L) // = List() List.range(-2L, 2L) // = List(-2L, -1L, 0L, 1L)- Parameters:
from- the first numbertoExclusive- the last number + 1- Returns:
- a range of long values as specified or the empty range if
from >= toExclusive
-
rangeBy
static List<java.lang.Long> rangeBy(long from, long toExclusive, long step)
Creates a List of long numbers starting fromfrom, extending totoExclusive - 1, withstep.Examples:
List.rangeBy(1L, 3L, 1L) // = List(1L, 2L) List.rangeBy(1L, 4L, 2L) // = List(1L, 3L) List.rangeBy(4L, 1L, -2L) // = List(4L, 2L) List.rangeBy(4L, 1L, 2L) // = List()- Parameters:
from- the first numbertoExclusive- the last number + 1step- the step- Returns:
- a range of long values as specified or the empty range if
from >= toInclusiveandstep > 0or
from <= toInclusiveandstep < 0 - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeClosed
static List<java.lang.Character> rangeClosed(char from, char toInclusive)
Creates a List of char numbers starting fromfrom, extending totoInclusive.Examples:
List.rangeClosed('a', 'a') // = List('a') List.rangeClosed('c', 'a') // = List() List.rangeClosed('a', 'd') // = List('a', 'b', 'c', 'd')- Parameters:
from- the first chartoInclusive- the last char- Returns:
- a range of char values as specified or the empty range if
from > toInclusive
-
rangeClosedBy
static List<java.lang.Character> rangeClosedBy(char from, char toInclusive, int step)
Creates a List of char numbers starting fromfrom, extending totoInclusive, withstep.Examples:
List.rangeClosedBy('a', 'c', 1) // = List('a', 'b', 'c') List.rangeClosedBy('a', 'd', 2) // = List('a', 'c') List.rangeClosedBy('d', 'a', -2) // = List('d', 'b') List.rangeClosedBy('d', 'a', 2) // = List()- Parameters:
from- the first chartoInclusive- the last charstep- the step- Returns:
- a range of char values as specified or the empty range if
from > toInclusiveandstep > 0or
from < toInclusiveandstep < 0 - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeClosedBy
@GwtIncompatible static List<java.lang.Double> rangeClosedBy(double from, double toInclusive, double step)
Creates a List of double numbers starting fromfrom, extending totoInclusive, withstep.Examples:
List.rangeClosedBy(1.0, 3.0, 1.0) // = List(1.0, 2.0, 3.0) List.rangeClosedBy(1.0, 4.0, 2.0) // = List(1.0, 3.0) List.rangeClosedBy(4.0, 1.0, -2.0) // = List(4.0, 2.0) List.rangeClosedBy(4.0, 1.0, 2.0) // = List()- Parameters:
from- the first doubletoInclusive- the upper bound (inclusive)step- the step- Returns:
- a range of double values as specified or the empty range if
from > toInclusiveandstep > 0or
from < toInclusiveandstep < 0 - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeClosed
static List<java.lang.Integer> rangeClosed(int from, int toInclusive)
Creates a List of int numbers starting fromfrom, extending totoInclusive.Examples:
List.rangeClosed(0, 0) // = List(0) List.rangeClosed(2, 0) // = List() List.rangeClosed(-2, 2) // = List(-2, -1, 0, 1, 2)- Parameters:
from- the first numbertoInclusive- the last number- Returns:
- a range of int values as specified or the empty range if
from > toInclusive
-
rangeClosedBy
static List<java.lang.Integer> rangeClosedBy(int from, int toInclusive, int step)
Creates a List of int numbers starting fromfrom, extending totoInclusive, withstep.Examples:
List.rangeClosedBy(1, 3, 1) // = List(1, 2, 3) List.rangeClosedBy(1, 4, 2) // = List(1, 3) List.rangeClosedBy(4, 1, -2) // = List(4, 2) List.rangeClosedBy(4, 1, 2) // = List()- Parameters:
from- the first numbertoInclusive- the last numberstep- the step- Returns:
- a range of int values as specified or the empty range if
from > toInclusiveandstep > 0or
from < toInclusiveandstep < 0 - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
rangeClosed
static List<java.lang.Long> rangeClosed(long from, long toInclusive)
Creates a List of long numbers starting fromfrom, extending totoInclusive.Examples:
List.rangeClosed(0L, 0L) // = List(0L) List.rangeClosed(2L, 0L) // = List() List.rangeClosed(-2L, 2L) // = List(-2L, -1L, 0L, 1L, 2L)- Parameters:
from- the first numbertoInclusive- the last number- Returns:
- a range of long values as specified or the empty range if
from > toInclusive
-
rangeClosedBy
static List<java.lang.Long> rangeClosedBy(long from, long toInclusive, long step)
Creates a List of long numbers starting fromfrom, extending totoInclusive, withstep.Examples:
List.rangeClosedBy(1L, 3L, 1L) // = List(1L, 2L, 3L) List.rangeClosedBy(1L, 4L, 2L) // = List(1L, 3L) List.rangeClosedBy(4L, 1L, -2L) // = List(4L, 2L) List.rangeClosedBy(4L, 1L, 2L) // = List()- Parameters:
from- the first numbertoInclusive- the last numberstep- the step- Returns:
- a range of int values as specified or the empty range if
from > toInclusiveandstep > 0or
from < toInclusiveandstep < 0 - Throws:
java.lang.IllegalArgumentException- ifstepis zero
-
transpose
static <T> List<List<T>> transpose(@NonNull List<List<T>> matrix)
Transposes the rows and columns of aListmatrix.- Type Parameters:
T- matrix element type- Parameters:
matrix- to be transposed.- Returns:
- a transposed
Listmatrix. - Throws:
java.lang.IllegalArgumentException- if the row lengths ofmatrixdiffer.ex:
List.transpose(List(List(1,2,3), List(4,5,6))) → List(List(1,4), List(2,5), List(3,6))
-
unfoldRight
static <T,U> List<U> unfoldRight(T seed, @NonNull java.util.function.Function<? super T,@NonNull Option<Tuple2<? extends U,? extends T>>> f)
Creates a list from a seed value and a function. The function takes the seed at first. The function should returnNonewhen it's done generating the list, otherwiseSomeTupleof the element for the next call and the value to add to the resulting list.Example:
List.unfoldRight(10, x -> x == 0 ? Option.none() : Option.of(new Tuple2<>(x, x-1))); // List(10, 9, 8, 7, 6, 5, 4, 3, 2, 1))- Type Parameters:
T- type of seedsU- type of unfolded values- Parameters:
seed- the start value for the iterationf- the function to get the next step of the iteration- Returns:
- a list with the values built up by the iteration
- Throws:
java.lang.NullPointerException- iffis null
-
unfoldLeft
static <T,U> List<U> unfoldLeft(T seed, @NonNull java.util.function.Function<? super T,Option<Tuple2<? extends T,? extends U>>> f)
Creates a list from a seed value and a function. The function takes the seed at first. The function should returnNonewhen it's done generating the list, otherwiseSomeTupleof the value to add to the resulting list and the element for the next call.Example:
List.unfoldLeft(10, x -> x == 0 ? Option.none() : Option.of(new Tuple2<>(x-1, x))); // List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))- Type Parameters:
T- type of seedsU- type of unfolded values- Parameters:
seed- the start value for the iterationf- the function to get the next step of the iteration- Returns:
- a list with the values built up by the iteration
- Throws:
java.lang.NullPointerException- iffis null
-
unfold
static <T> List<T> unfold(T seed, @NonNull java.util.function.Function<? super T,Option<Tuple2<? extends T,? extends T>>> f)
Creates a list from a seed value and a function. The function takes the seed at first. The function should returnNonewhen it's done generating the list, otherwiseSomeTupleof the value to add to the resulting list and the element for the next call.Example:
List.unfold(10, x -> x == 0 ? Option.none() : Option.of(new Tuple2<>(x-1, x))); // List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))- Type Parameters:
T- type of seeds and unfolded values- Parameters:
seed- the start value for the iterationf- the function to get the next step of the iteration- Returns:
- a list with the values built up by the iteration
- Throws:
java.lang.NullPointerException- iffis null
-
append
default List<T> append(T element)
Description copied from interface:SeqReturns a new sequence with the given element appended at the end.
-
appendAll
default List<T> appendAll(@NonNull java.lang.Iterable<? extends T> elements)
Description copied from interface:SeqReturns a new sequence with all elements from the givenIterableappended at the end of this sequence.
-
asJava
@GwtIncompatible default java.util.List<T> asJava()
Description copied from interface:SeqReturns an immutableListview of thisSeq. Any attempt to modify the view (e.g., via mutator methods) will throwUnsupportedOperationExceptionat runtime.This is a view, not a copy. Compared to conversion methods like
toJava*():- Creating the view is O(1) (constant time), whereas conversions take O(n), with n = collection size.
- Operations on the view have the same performance characteristics as the underlying persistent Vavr collection, while converted collections behave like standard Java collections.
Note: the immutable Java list view throws
UnsupportedOperationExceptionbefore checking method arguments, which may differ from standard Java behavior.
-
asJava
@GwtIncompatible default List<T> asJava(@NonNull java.util.function.Consumer<? super java.util.List<T>> action)
Description copied from interface:SeqCreates an immutableListview of thisSeqand passes it to the givenaction.The view is immutable: any attempt to modify it will throw
UnsupportedOperationExceptionat runtime.
-
asJavaMutable
@GwtIncompatible default java.util.List<T> asJavaMutable()
Description copied from interface:SeqReturns a mutableListview of thisSeq. All standard mutator methods of theListinterface are supported and modify the underlying view.Unlike
Seq.asJava(), this view allows modifications, but the performance characteristics correspond to the underlying persistent Vavr collection.- Specified by:
asJavaMutablein interfaceSeq<T>- Returns:
- a mutable
Listview of this sequence - See Also:
Seq.asJava()
-
asJavaMutable
@GwtIncompatible default List<T> asJavaMutable(@NonNull java.util.function.Consumer<? super java.util.List<T>> action)
Description copied from interface:SeqCreates a mutableListview of thisSeqand passes it to the givenaction.The view supports all standard mutator methods. The result of the action determines what is returned:
- If only read operations are performed, this instance is returned.
- If any write operations are performed, a new
Seqreflecting those changes is returned.
- Specified by:
asJavaMutablein interfaceLinearSeq<T>- Specified by:
asJavaMutablein interfaceSeq<T>- Parameters:
action- a side-effecting operation that receives a mutablejava.util.Listview- Returns:
- this sequence or a new sequence reflecting modifications made through the view
- See Also:
Seq.asJavaMutable()
-
collect
default <R> List<R> collect(@NonNull PartialFunction<? super T,? extends R> partialFunction)
Description copied from interface:TraversableApplies aPartialFunctionto all elements that are defined for it and collects the results.For each element in iteration order, the function is first tested:
IfpartialFunction.isDefinedAt(element)true, the element is mapped to typeR:R newElement = partialFunction.apply(element)Note: If this
Traversableis ordered (i.e., extendsOrdered), the caller must ensure that the resulting elements are comparable (i.e., implementComparable).- Specified by:
collectin interfaceLinearSeq<T>- Specified by:
collectin interfaceSeq<T>- Specified by:
collectin interfaceTraversable<T>- Type Parameters:
R- the type of elements in the resultingTraversable- Parameters:
partialFunction- a function that may not be defined for all elements of this traversable- Returns:
- a new
Traversablecontaining the results of applying the partial function
-
combinations
default List<List<T>> combinations()
Description copied from interface:SeqReturns a sequence containing all combinations of elements from this sequence, for all sizes from0tolength().Examples:
[].combinations() = [[]] [1,2,3].combinations() = [ [], // k = 0 [1], [2], [3], // k = 1 [1,2], [1,3], [2,3], // k = 2 [1,2,3] // k = 3 ]- Specified by:
combinationsin interfaceLinearSeq<T>- Specified by:
combinationsin interfaceSeq<T>- Returns:
- a sequence of sequences representing all combinations of this sequence's elements
-
combinations
default List<List<T>> combinations(int k)
Description copied from interface:SeqReturns all subsets of this sequence containing exactlykdistinct elements, i.e., the k-combinations of this sequence.- Specified by:
combinationsin interfaceLinearSeq<T>- Specified by:
combinationsin interfaceSeq<T>- Parameters:
k- the size of each subset- Returns:
- a sequence of sequences representing all k-element combinations
- See Also:
- Combination
-
crossProduct
default Iterator<List<T>> crossProduct(int power)
Description copied from interface:SeqReturns the n-ary Cartesian power (cross product) of this sequence. Each element of the resulting iterator is a sequence of lengthpower, containing all possible combinations of elements from this sequence.Example for power = 2:
// Result: [(A,A), (A,B), (A,C), ..., (B,A), (B,B), ..., (Z,Y), (Z,Z)] CharSeq.rangeClosed('A', 'Z').crossProduct(2);If
poweris negative, the result is an empty iterator:// Result: () CharSeq.rangeClosed('A', 'Z').crossProduct(-1);- Specified by:
crossProductin interfaceLinearSeq<T>- Specified by:
crossProductin interfaceSeq<T>- Parameters:
power- the number of Cartesian multiplications- Returns:
- an
Iteratorover sequences representing the Cartesian power of this sequence
-
distinct
default List<T> distinct()
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance with all duplicates removed. Element equality is determined usingequals.
-
distinctBy
default List<T> distinctBy(@NonNull java.util.Comparator<? super T> comparator)
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance without duplicates, as determined by the givencomparator.- Specified by:
distinctByin interfaceLinearSeq<T>- Specified by:
distinctByin interfaceSeq<T>- Specified by:
distinctByin interfaceTraversable<T>- Parameters:
comparator- a comparator used to determine equality of elements- Returns:
- a new
Traversablewith duplicates removed
-
distinctBy
default <U> List<T> distinctBy(@NonNull java.util.function.Function<? super T,? extends U> keyExtractor)
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance without duplicates, based on keys extracted from elements usingkeyExtractor.The first occurrence of each key is retained in the resulting sequence.
- Specified by:
distinctByin interfaceLinearSeq<T>- Specified by:
distinctByin interfaceSeq<T>- Specified by:
distinctByin interfaceTraversable<T>- Type Parameters:
U- the type of key- Parameters:
keyExtractor- a function to extract keys for determining uniqueness- Returns:
- a new
Traversablewith duplicates removed based on keys
-
distinctByKeepLast
default List<T> distinctByKeepLast(@NonNull java.util.Comparator<? super T> comparator)
Description copied from interface:SeqReturns a sequence with duplicate elements removed, as determined by the provided comparator. When duplicates are found, the **last occurrence** of each element is retained.- Specified by:
distinctByKeepLastin interfaceLinearSeq<T>- Specified by:
distinctByKeepLastin interfaceSeq<T>- Parameters:
comparator- a comparator defining equality between elements- Returns:
- a new sequence with duplicates removed, keeping the last occurrence of each element
-
distinctByKeepLast
default <U> List<T> distinctByKeepLast(@NonNull java.util.function.Function<? super T,? extends U> keyExtractor)
Description copied from interface:SeqReturns a sequence with duplicates removed based on a key extracted from each element. The key is obtained via the providedkeyExtractorfunction. When duplicates are found, the **last occurrence** of each element for a given key is retained.- Specified by:
distinctByKeepLastin interfaceLinearSeq<T>- Specified by:
distinctByKeepLastin interfaceSeq<T>- Type Parameters:
U- the type of the key used for determining uniqueness- Parameters:
keyExtractor- a function extracting a key from each element for uniqueness comparison- Returns:
- a new sequence of elements distinct by the extracted key, keeping the last occurrence
-
drop
default List<T> drop(int n)
Description copied from interface:TraversableReturns a newTraversablewithout the firstnelements, or an empty instance if this contains fewer thannelements.
-
dropUntil
default List<T> dropUntil(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableReturns a newTraversablestarting from the first element that satisfies the givenpredicate, dropping all preceding elements.
-
dropWhile
default List<T> dropWhile(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableReturns a newTraversablestarting from the first element that does not satisfy the givenpredicate, dropping all preceding elements.This is equivalent to
dropUntil(predicate.negate()), which is useful for method references that cannot be negated directly.
-
dropRight
default List<T> dropRight(int n)
Description copied from interface:TraversableReturns a newTraversablewithout the lastnelements, or an empty instance if this contains fewer thannelements.
-
dropRightUntil
default List<T> dropRightUntil(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:SeqDrops elements from the end of the sequence until an element satisfies the given predicate. The returned sequence includes the last element that satisfies the predicate.- Specified by:
dropRightUntilin interfaceLinearSeq<T>- Specified by:
dropRightUntilin interfaceSeq<T>- Parameters:
predicate- a condition to test elements, starting from the end- Returns:
- a new sequence containing all elements up to and including the last element that satisfies the predicate
-
dropRightWhile
default List<T> dropRightWhile(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:SeqDrops elements from the end of the sequence while the given predicate holds.This is equivalent to
dropRightUntil(predicate.negate()). Useful when using method references that cannot be negated directly.- Specified by:
dropRightWhilein interfaceLinearSeq<T>- Specified by:
dropRightWhilein interfaceSeq<T>- Parameters:
predicate- a condition to test elements, starting from the end- Returns:
- a new sequence containing all elements up to and including the last element that does not satisfy the predicate
-
filter
default List<T> filter(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableReturns a new traversable containing only the elements that satisfy the given predicate.
-
reject
default List<T> reject(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableReturns a new traversable containing only the elements that do not satisfy the given predicate.This is equivalent to
filter(predicate.negate()).
-
flatMap
default <U> List<U> flatMap(@NonNull java.util.function.Function<? super T,? extends java.lang.Iterable<? extends U>> mapper)
Description copied from interface:TraversableTransforms each element of this Traversable into anIterableof elements and flattens the resulting iterables into a single Traversable.- Specified by:
flatMapin interfaceLinearSeq<T>- Specified by:
flatMapin interfaceSeq<T>- Specified by:
flatMapin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the resulting Traversable- Parameters:
mapper- a function mapping elements to iterables- Returns:
- a new Traversable containing all elements produced by applying
mapperand flattening
-
get
default T get(int index)
Description copied from interface:SeqReturns the element at the specified index.
-
groupBy
default <C> Map<C,List<T>> groupBy(@NonNull java.util.function.Function<? super T,? extends C> classifier)
Description copied from interface:TraversableGroups elements of thisTraversablebased on a classifier function.- Specified by:
groupByin interfaceLinearSeq<T>- Specified by:
groupByin interfaceSeq<T>- Specified by:
groupByin interfaceTraversable<T>- Type Parameters:
C- The type of the group keys- Parameters:
classifier- A function that assigns each element to a group- Returns:
- A map where each key corresponds to a group of elements
- See Also:
Traversable.arrangeBy(Function)
-
grouped
default Iterator<List<T>> grouped(int size)
Description copied from interface:TraversableSplits thisTraversableinto consecutive blocks of the given size.Let
lengthbe the number of elements in thisTraversable:- If empty, the resulting
Iteratoris empty. - If
size <= length, the resultingIteratorcontainslength / sizeblocks of sizesizeand possibly a final smaller block of sizelength % size. - If
size > length, the resultingIteratorcontains a single block of sizelength.
Examples:
[].grouped(1) = [] [].grouped(0) throws [].grouped(-1) throws [1,2,3,4].grouped(2) = [[1,2],[3,4]] [1,2,3,4,5].grouped(2) = [[1,2],[3,4],[5]] [1,2,3,4].grouped(5) = [[1,2,3,4]]Note:
grouped(size)is equivalent tosliding(size, size). - If empty, the resulting
-
hasDefiniteSize
default boolean hasDefiniteSize()
Description copied from interface:TraversableIndicates whether thisTraversablehas a known finite size.This should typically be implemented by concrete classes, not interfaces.
- Specified by:
hasDefiniteSizein interfaceTraversable<T>- Returns:
trueif the number of elements is finite and known,falseotherwise.
-
indexOf
default int indexOf(T element, int from)
Description copied from interface:SeqReturns the index of the first occurrence of the given element, starting at the specified index, or-1if this sequence does not contain the element.
-
init
default List<T> init()
Description copied from interface:TraversableReturns all elements of this Traversable except the last one.This is the dual of
Traversable.tail().
-
initOption
default Option<List<T>> initOption()
Description copied from interface:TraversableReturns all elements of this Traversable except the last one, wrapped in anOption.This is the dual of
Traversable.tailOption().- Specified by:
initOptionin interfaceLinearSeq<T>- Specified by:
initOptionin interfaceSeq<T>- Specified by:
initOptionin interfaceTraversable<T>- Returns:
Some(traversable)if non-empty, orNoneif this Traversable is empty
-
length
int length()
Description copied from interface:TraversableReturns the number of elements in this Traversable.Equivalent to
Traversable.size().- Specified by:
lengthin interfaceTraversable<T>- Returns:
- the number of elements
-
insert
default List<T> insert(int index, T element)
Description copied from interface:SeqReturns a new sequence with the given element inserted at the specified index.
-
insertAll
default List<T> insertAll(int index, @NonNull java.lang.Iterable<? extends T> elements)
Description copied from interface:SeqReturns a new sequence with the given elements inserted at the specified index.
-
intersperse
default List<T> intersperse(T element)
Description copied from interface:SeqReturns a new sequence where the given element is inserted between all elements of this sequence.- Specified by:
interspersein interfaceLinearSeq<T>- Specified by:
interspersein interfaceSeq<T>- Parameters:
element- the element to intersperse- Returns:
- a new
Seqwith the element interspersed
-
isTraversableAgain
default boolean isTraversableAgain()
Description copied from interface:TraversableChecks if this Traversable can be traversed multiple times without side effects.Implementations should provide the correct behavior; this is not meant for interfaces alone.
- Specified by:
isTraversableAgainin interfaceTraversable<T>- Returns:
trueif this Traversable is guaranteed to be repeatably traversable,falseotherwise
-
last
default T last()
Description copied from interface:TraversableReturns the last element of this Traversable.- Specified by:
lastin interfaceTraversable<T>- Returns:
- the last element
-
lastIndexOf
default int lastIndexOf(T element, int end)
Description copied from interface:SeqReturns the index of the last occurrence of the given element at or before the specified end index, or-1if this sequence does not contain the element.- Specified by:
lastIndexOfin interfaceSeq<T>- Parameters:
element- the element to search forend- the maximum index to consider- Returns:
- the index of the last occurrence at or before
end, or-1if not found
-
map
default <U> List<U> map(@NonNull java.util.function.Function<? super T,? extends U> mapper)
Description copied from interface:TraversableTransforms the elements of this Traversable to a new type, preserving order if defined.- Specified by:
mapin interfaceLinearSeq<T>- Specified by:
mapin interfaceSeq<T>- Specified by:
mapin interfaceTraversable<T>- Specified by:
mapin interfaceValue<T>- Type Parameters:
U- the target element type- Parameters:
mapper- a mapping function- Returns:
- a new Traversable containing the mapped elements
-
mapTo
default <U> List<U> mapTo(U value)
Description copied from interface:ValueMaps the underlying value to another fixed value.
-
mapToVoid
default List<java.lang.Void> mapToVoid()
Description copied from interface:ValueMaps the underlying value to Void
-
orElse
default List<T> orElse(@NonNull java.lang.Iterable<? extends T> other)
Description copied from interface:TraversableReturns thisTraversableif it is non-empty; otherwise, returns the given alternative.
-
orElse
default List<T> orElse(@NonNull java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Description copied from interface:TraversableReturns thisTraversableif it is non-empty; otherwise, returns the result of evaluating the given supplier.
-
padTo
default List<T> padTo(int length, T element)
Description copied from interface:SeqReturns a new sequence with this sequence padded on the right with the given element until the specified target length is reached.Note: Lazily-evaluated sequences may need to process all elements to determine the overall length.
- Specified by:
padToin interfaceLinearSeq<T>- Specified by:
padToin interfaceSeq<T>- Parameters:
length- the target length of the resulting sequenceelement- the element to append as padding- Returns:
- a new
Seqconsisting of this sequence followed by the minimal number of occurrences ofelementto reach at leastlength
-
leftPadTo
default List<T> leftPadTo(int length, T element)
Description copied from interface:SeqReturns a new sequence with this sequence padded on the left with the given element until the specified target length is reached.Note: Lazily-evaluated sequences may need to process all elements to determine the overall length.
-
patch
default List<T> patch(int from, @NonNull java.lang.Iterable<? extends T> that, int replaced)
Description copied from interface:SeqReturns a new sequence in which a slice of elements in this sequence is replaced by the elements of another sequence.- Specified by:
patchin interfaceLinearSeq<T>- Specified by:
patchin interfaceSeq<T>- Parameters:
from- the starting index of the slice to be replacedthat- the sequence of elements to insert; must not benullreplaced- the number of elements to remove from this sequence starting atfrom- Returns:
- a new
Seqwith the specified slice replaced
-
partition
default Tuple2<List<T>,List<T>> partition(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableSplits thisTraversableinto two partitions according to a predicate.The first partition contains all elements that satisfy the predicate, and the second contains all elements that do not. The original iteration order is preserved.
-
peek
default T peek()
Returns the head element without modifying the List.- Returns:
- the first element
- Throws:
java.util.NoSuchElementException- if this List is empty
-
peekOption
default Option<T> peekOption()
Returns the head element without modifying the List.- Returns:
Noneif this List is empty, otherwise aSomecontaining the head element
-
peek
default List<T> peek(@NonNull java.util.function.Consumer<? super T> action)
Performs an action on the head element of thisList.
-
permutations
default List<List<T>> permutations()
Description copied from interface:SeqReturns all unique permutations of this sequence.Example:
[].permutations() = [] [1, 2, 3].permutations() = [ [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] ]- Specified by:
permutationsin interfaceLinearSeq<T>- Specified by:
permutationsin interfaceSeq<T>- Returns:
- a sequence of all unique permutations of this sequence
-
pop
default List<T> pop()
Removes the head element from this List.- Returns:
- the elements of this List without the head element
- Throws:
java.util.NoSuchElementException- if this List is empty
-
popOption
default Option<List<T>> popOption()
Removes the head element from this List.- Returns:
Noneif this List is empty, otherwise aSomecontaining the elements of this List without the head element
-
pop2
default Tuple2<T,List<T>> pop2()
Removes the head element from this List.- Returns:
- a tuple containing the head element and the remaining elements of this List
- Throws:
java.util.NoSuchElementException- if this List is empty
-
pop2Option
default Option<Tuple2<T,List<T>>> pop2Option()
Removes the head element from this List.- Returns:
Noneif this List is empty, otherwiseSomeTuplecontaining the head element and the remaining elements of this List
-
prepend
default List<T> prepend(T element)
Description copied from interface:SeqReturns a new sequence with the given element prepended to this sequence.
-
prependAll
default List<T> prependAll(@NonNull java.lang.Iterable<? extends T> elements)
Description copied from interface:SeqReturns a new sequence with all given elements prepended to this sequence.- Specified by:
prependAllin interfaceLinearSeq<T>- Specified by:
prependAllin interfaceSeq<T>- Parameters:
elements- the elements to prepend; must not benull- Returns:
- a new
Seqwith the elements added at the front
-
push
default List<T> push(T element)
Pushes a new element on top of this List.- Parameters:
element- The new element- Returns:
- a new
Listinstance, containing the new element on top of this List
-
push
default List<T> push(T @NonNull ... elements)
Pushes the given elements on top of this List. A List has LIFO order, i.e. the last of the given elements is the first which will be retrieved.- Parameters:
elements- Elements, may be empty- Returns:
- a new
Listinstance, containing the new elements on top of this List - Throws:
java.lang.NullPointerException- if elements is null
-
pushAll
default List<T> pushAll(@NonNull java.lang.Iterable<T> elements)
Pushes the given elements on top of this List. A List has LIFO order, i.e. the last of the given elements is the first which will be retrieved.- Parameters:
elements- An Iterable of elements, may be empty- Returns:
- a new
Listinstance, containing the new elements on top of this List - Throws:
java.lang.NullPointerException- if elements is null
-
remove
default List<T> remove(T element)
Description copied from interface:SeqReturns a new sequence with the first occurrence of the given element removed.
-
removeFirst
default List<T> removeFirst(@NonNull java.util.function.Predicate<T> predicate)
Description copied from interface:SeqReturns a new sequence with the first element that satisfies the given predicate removed.- Specified by:
removeFirstin interfaceLinearSeq<T>- Specified by:
removeFirstin interfaceSeq<T>- Parameters:
predicate- the predicate used to identify the element to remove; must not benull- Returns:
- a new
Seqwithout the first matching element
-
removeLast
default List<T> removeLast(@NonNull java.util.function.Predicate<T> predicate)
Description copied from interface:SeqReturns a new sequence with the last element that satisfies the given predicate removed.- Specified by:
removeLastin interfaceLinearSeq<T>- Specified by:
removeLastin interfaceSeq<T>- Parameters:
predicate- the predicate used to identify the element to remove; must not benull- Returns:
- a new
Seqwithout the last matching element
-
removeAt
default List<T> removeAt(int index)
Description copied from interface:SeqReturns a new sequence with the element at the specified position removed. Subsequent elements are shifted to the left (indices decreased by one).
-
removeAll
default List<T> removeAll(T element)
Description copied from interface:SeqReturns a new sequence with all occurrences of the given element removed.
-
removeAll
default List<T> removeAll(@NonNull java.lang.Iterable<? extends T> elements)
Description copied from interface:SeqReturns a new sequence with all occurrences of the given elements removed.
-
removeAll
@Deprecated default List<T> removeAll(@NonNull java.util.function.Predicate<? super T> predicate)
Deprecated.Description copied from interface:SeqReturns a new Seq consisting of all elements which do not satisfy the given predicate.
-
replace
default List<T> replace(T currentElement, T newElement)
Description copied from interface:TraversableReplaces the first occurrence ofcurrentElementwithnewElement, if it exists.- Specified by:
replacein interfaceLinearSeq<T>- Specified by:
replacein interfaceSeq<T>- Specified by:
replacein interfaceTraversable<T>- Parameters:
currentElement- the element to be replacednewElement- the replacement element- Returns:
- a new Traversable with the first occurrence of
currentElementreplaced bynewElement
-
replaceAll
default List<T> replaceAll(T currentElement, T newElement)
Description copied from interface:TraversableReplaces all occurrences ofcurrentElementwithnewElement.- Specified by:
replaceAllin interfaceLinearSeq<T>- Specified by:
replaceAllin interfaceSeq<T>- Specified by:
replaceAllin interfaceTraversable<T>- Parameters:
currentElement- the element to be replacednewElement- the replacement element- Returns:
- a new Traversable with all occurrences of
currentElementreplaced bynewElement
-
retainAll
default List<T> retainAll(@NonNull java.lang.Iterable<? extends T> elements)
Description copied from interface:TraversableRetains only the elements from this Traversable that are contained in the givenelements.
-
reverse
default List<T> reverse()
Description copied from interface:SeqReturns a new sequence with the order of elements reversed.
-
rotateLeft
default List<T> rotateLeft(int n)
Description copied from interface:SeqReturns a new sequence with the elements circularly rotated to the left by the specified distance.Example:
// Result: List(3, 4, 5, 1, 2) List.of(1, 2, 3, 4, 5).rotateLeft(2);- Specified by:
rotateLeftin interfaceLinearSeq<T>- Specified by:
rotateLeftin interfaceSeq<T>- Parameters:
n- the number of positions to rotate left- Returns:
- a new
Seqwith elements rotated left
-
rotateRight
default List<T> rotateRight(int n)
Description copied from interface:SeqReturns a new sequence with the elements circularly rotated to the right by the specified distance.Example:
// Result: List(4, 5, 1, 2, 3) List.of(1, 2, 3, 4, 5).rotateRight(2);- Specified by:
rotateRightin interfaceLinearSeq<T>- Specified by:
rotateRightin interfaceSeq<T>- Parameters:
n- the number of positions to rotate right- Returns:
- a new
Seqwith elements rotated right
-
scan
default List<T> scan(T zero, @NonNull java.util.function.BiFunction<? super T,? super T,? extends T> operation)
Description copied from interface:TraversableComputes a prefix scan of the elements of this Traversable.The neutral element
zeromay be applied more than once.
-
scanLeft
default <U> List<U> scanLeft(U zero, @NonNull java.util.function.BiFunction<? super U,? super T,? extends U> operation)
Description copied from interface:TraversableProduces a collection containing cumulative results of applying the operator from left to right.Will not terminate for infinite collections. The results may vary across runs unless the collection is ordered.
- Specified by:
scanLeftin interfaceLinearSeq<T>- Specified by:
scanLeftin interfaceSeq<T>- Specified by:
scanLeftin interfaceTraversable<T>- Type Parameters:
U- the type of the resulting elements- Parameters:
zero- the initial valueoperation- a binary operator applied to the intermediate result and each element- Returns:
- a new Traversable containing the cumulative results
-
scanRight
default <U> List<U> scanRight(U zero, @NonNull java.util.function.BiFunction<? super T,? super U,? extends U> operation)
Description copied from interface:TraversableProduces a collection containing cumulative results of applying the operator from right to left.The head of the resulting collection is the last cumulative result. Will not terminate for infinite collections. Results may vary across runs unless the collection is ordered.
- Specified by:
scanRightin interfaceLinearSeq<T>- Specified by:
scanRightin interfaceSeq<T>- Specified by:
scanRightin interfaceTraversable<T>- Type Parameters:
U- the type of the resulting elements- Parameters:
zero- the initial valueoperation- a binary operator applied to each element and the intermediate result- Returns:
- a new Traversable containing the cumulative results
-
shuffle
default List<T> shuffle()
Description copied from interface:SeqReturns a new sequence with the elements randomly shuffled.
-
slice
default List<T> slice(int beginIndex, int endIndex)
Description copied from interface:SeqReturns a subsequence (slice) of this sequence, starting atbeginIndex(inclusive) and ending atendIndex(exclusive).Examples:
List.of(1, 2, 3, 4).slice(1, 3); // = (2, 3) List.of(1, 2, 3, 4).slice(0, 4); // = (1, 2, 3, 4) List.of(1, 2, 3, 4).slice(2, 2); // = () List.of(1, 2).slice(1, 0); // = () List.of(1, 2).slice(-10, 10); // = (1, 2)See also
Seq.subSequence(int, int), which may throw an exception instead of returning a sequence in some cases.
-
slideBy
default Iterator<List<T>> slideBy(@NonNull java.util.function.Function<? super T,?> classifier)
Description copied from interface:TraversablePartitions thisTraversableinto consecutive non-overlapping windows according to a classification function.Each window contains elements with the same class, as determined by
classifier. Two consecutive elements belong to the same window only ifclassifierreturns equal values for both. Otherwise, the current window ends and a new window begins with the next element.Examples:
[].slideBy(Function.identity()) = [] [1,2,3,4,4,5].slideBy(Function.identity()) = [[1],[2],[3],[4,4],[5]] [1,2,3,10,12,5,7,20,29].slideBy(x -> x / 10) = [[1,2,3],[10,12],[5,7],[20,29]]
-
sliding
default Iterator<List<T>> sliding(int size)
Description copied from interface:TraversableSlides a window of a givensizeover thisTraversablewith a step size of 1.This is equivalent to calling
Traversable.sliding(int, int)with a step size of 1.
-
sliding
default Iterator<List<T>> sliding(int size, int step)
Description copied from interface:TraversableSlides a window of a specificsizewith a givenstepover thisTraversable.Examples:
[].sliding(1, 1) = [] [1,2,3,4,5].sliding(2, 3) = [[1,2],[4,5]] [1,2,3,4,5].sliding(2, 4) = [[1,2],[5]] [1,2,3,4,5].sliding(2, 5) = [[1,2]] [1,2,3,4].sliding(5, 3) = [[1,2,3,4],[4]]
-
sorted
default List<T> sorted()
Description copied from interface:SeqReturns a new sequence with elements sorted according to their natural order.
-
sorted
default List<T> sorted(@NonNull java.util.Comparator<? super T> comparator)
Description copied from interface:SeqReturns a new sequence with elements sorted according to the givenComparator.
-
sortBy
default <U extends java.lang.Comparable<? super U>> List<T> sortBy(@NonNull java.util.function.Function<? super T,? extends U> mapper)
Description copied from interface:SeqReturns a new sequence sorted by comparing elements in a different domain defined by the givenmapper.
-
sortBy
default <U> List<T> sortBy(@NonNull java.util.Comparator<? super U> comparator, java.util.function.Function<? super T,? extends U> mapper)
Description copied from interface:SeqReturns a new sequence sorted by comparing elements in a different domain defined by the givenmapper, using the providedcomparator.- Specified by:
sortByin interfaceLinearSeq<T>- Specified by:
sortByin interfaceSeq<T>- Type Parameters:
U- the type used for comparison- Parameters:
comparator- the comparator used to compare mapped values; must not benullmapper- a function mapping elements to the domain for comparison; must not benull- Returns:
- a new
Seqsorted according to the mapped values and comparator
-
span
default Tuple2<List<T>,List<T>> span(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableSplits thisTraversableinto a prefix and remainder according to the givenpredicate.The first element of the returned
Tupleis the longest prefix of elements satisfyingpredicate, and the second element is the remaining elements.
-
splitAt
default Tuple2<List<T>,List<T>> splitAt(int n)
Description copied from interface:SeqSplits this sequence at the specified index.The result of
splitAt(n)is equivalent toTuple.of(take(n), drop(n)).
-
splitAt
default Tuple2<List<T>,List<T>> splitAt(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:SeqSplits this sequence at the first element satisfying the given predicate.
-
splitAtInclusive
default Tuple2<List<T>,List<T>> splitAtInclusive(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:SeqSplits this sequence at the first element satisfying the given predicate, including the element in the first part.- Specified by:
splitAtInclusivein interfaceSeq<T>- Parameters:
predicate- the predicate used to determine the split point; must not benull- Returns:
- a
Tuple2containing the sequence up to and including the first matching element and the remaining sequence
-
stringPrefix
default java.lang.String stringPrefix()
Description copied from interface:ValueReturns the name of this Value type, which is used by toString().- Specified by:
stringPrefixin interfaceValue<T>- Returns:
- This type name.
-
subSequence
default List<T> subSequence(int beginIndex)
Description copied from interface:SeqReturns aSeqthat is a subsequence of this sequence, starting from the specifiedbeginIndexand extending to the end of this sequence.Examples:
List.of(1, 2).subSequence(0); // = (1, 2) List.of(1, 2).subSequence(1); // = (2) List.of(1, 2).subSequence(2); // = () List.of(1, 2).subSequence(10); // throws IndexOutOfBoundsException List.of(1, 2).subSequence(-10); // throws IndexOutOfBoundsExceptionSee also
Seq.drop(int), which provides similar functionality but does not throw an exception for out-of-bounds indices.- Specified by:
subSequencein interfaceLinearSeq<T>- Specified by:
subSequencein interfaceSeq<T>- Parameters:
beginIndex- the starting index (inclusive) of the subsequence- Returns:
- a new
Seqrepresenting the subsequence frombeginIndexto the end
-
subSequence
default List<T> subSequence(int beginIndex, int endIndex)
Description copied from interface:SeqReturns aSeqthat is a subsequence of this sequence, starting from the specifiedbeginIndex(inclusive) and ending atendIndex(exclusive).Examples:
List.of(1, 2, 3, 4).subSequence(1, 3); // = (2, 3) List.of(1, 2, 3, 4).subSequence(0, 4); // = (1, 2, 3, 4) List.of(1, 2, 3, 4).subSequence(2, 2); // = () List.of(1, 2).subSequence(1, 0); // throws IndexOutOfBoundsException List.of(1, 2).subSequence(-10, 1); // throws IndexOutOfBoundsException List.of(1, 2).subSequence(0, 10); // throws IndexOutOfBoundsExceptionSee also
Seq.slice(int, int), which returns an empty sequence instead of throwing exceptions when indices are out of range.- Specified by:
subSequencein interfaceLinearSeq<T>- Specified by:
subSequencein interfaceSeq<T>- Parameters:
beginIndex- the starting index (inclusive) of the subsequenceendIndex- the ending index (exclusive) of the subsequence- Returns:
- a new
Seqrepresenting the subsequence frombeginIndextoendIndex - 1
-
tail
List<T> tail()
Description copied from interface:TraversableReturns a newTraversablewithout its first element.
-
tailOption
default Option<List<T>> tailOption()
Description copied from interface:TraversableReturns a newTraversablewithout its first element as anOption.- Specified by:
tailOptionin interfaceLinearSeq<T>- Specified by:
tailOptionin interfaceSeq<T>- Specified by:
tailOptionin interfaceTraversable<T>- Returns:
Some(traversable)if non-empty, otherwiseNone
-
take
default List<T> take(int n)
Description copied from interface:TraversableReturns the firstnelements of thisTraversable, or all elements ifnexceeds the length.Equivalent to
sublist(0, max(0, min(length(), n))), but safe forn < 0orn > length().If
n < 0, an empty instance is returned. Ifn > length(), the full instance is returned.
-
takeUntil
default List<T> takeUntil(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableTakes elements from thisTraversableuntil the given predicate holds for an element.Equivalent to
takeWhile(predicate.negate()), but useful when using method references that cannot be negated directly.- Specified by:
takeUntilin interfaceLinearSeq<T>- Specified by:
takeUntilin interfaceSeq<T>- Specified by:
takeUntilin interfaceTraversable<T>- Parameters:
predicate- a condition tested sequentially on the elements- Returns:
- a new
Traversablecontaining all elements before the first one that satisfies the predicate
-
takeWhile
default List<T> takeWhile(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:TraversableTakes elements from thisTraversablewhile the given predicate holds.- Specified by:
takeWhilein interfaceLinearSeq<T>- Specified by:
takeWhilein interfaceSeq<T>- Specified by:
takeWhilein interfaceTraversable<T>- Parameters:
predicate- a condition tested sequentially on the elements- Returns:
- a new
Traversablecontaining all elements up to (but not including) the first one that does not satisfy the predicate
-
takeRight
default List<T> takeRight(int n)
Description copied from interface:TraversableReturns the lastnelements of thisTraversable, or all elements ifnexceeds the length.Equivalent to
sublist(max(0, length() - n), length()), but safe forn < 0orn > length().If
n < 0, an empty instance is returned. Ifn > length(), the full instance is returned.
-
takeRightUntil
default List<T> takeRightUntil(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:SeqTakes elements from the end of the sequence until an element satisfies the given predicate. The returned sequence starts after the last element that satisfies the predicate.- Specified by:
takeRightUntilin interfaceLinearSeq<T>- Specified by:
takeRightUntilin interfaceSeq<T>- Parameters:
predicate- a condition to test elements, starting from the end- Returns:
- a new sequence containing all elements after the last element that satisfies the predicate
-
takeRightWhile
default List<T> takeRightWhile(@NonNull java.util.function.Predicate<? super T> predicate)
Description copied from interface:SeqTakes elements from the end of the sequence while the given predicate holds.This is an equivalent to
takeRightUntil(predicate.negate()). Useful when using method references that cannot be negated directly.- Specified by:
takeRightWhilein interfaceLinearSeq<T>- Specified by:
takeRightWhilein interfaceSeq<T>- Parameters:
predicate- a condition to test elements, starting from the end- Returns:
- a new sequence containing all elements after the last element that does not satisfy the predicate
-
transform
default <U> U transform(@NonNull java.util.function.Function<? super List<T>,? extends U> f)
Transforms thisList.- Type Parameters:
U- Type of transformation result- Parameters:
f- A transformation- Returns:
- An instance of type
U - Throws:
java.lang.NullPointerException- iffis null
-
unzip
default <T1,T2> Tuple2<List<T1>,List<T2>> unzip(@NonNull java.util.function.Function<? super T,Tuple2<? extends T1,? extends T2>> unzipper)
Description copied from interface:TraversableUnzips the elements of thisTraversableby mapping each element to a pair and splitting them into two separateTraversablecollections.- Specified by:
unzipin interfaceLinearSeq<T>- Specified by:
unzipin interfaceSeq<T>- Specified by:
unzipin interfaceTraversable<T>- Type Parameters:
T1- type of the first element in the resulting pairsT2- type of the second element in the resulting pairs- Parameters:
unzipper- a function that maps elements of thisTraversableto pairs- Returns:
- a
Tuple2containing twoTraversablecollections with the split elements
-
unzip3
default <T1,T2,T3> Tuple3<List<T1>,List<T2>,List<T3>> unzip3(@NonNull java.util.function.Function<? super T,Tuple3<? extends T1,? extends T2,? extends T3>> unzipper)
Description copied from interface:TraversableUnzips the elements of thisTraversableby mapping each element to a triple and splitting them into three separateTraversablecollections.- Specified by:
unzip3in interfaceSeq<T>- Specified by:
unzip3in interfaceTraversable<T>- Type Parameters:
T1- type of the first element in the resulting triplesT2- type of the second element in the resulting triplesT3- type of the third element in the resulting triples- Parameters:
unzipper- a function that maps elements of thisTraversableto triples- Returns:
- a
Tuple3containing threeTraversablecollections with the split elements
-
update
default List<T> update(int index, T element)
Description copied from interface:SeqReturns a newSeqwith the element at the specified index replaced by the given value.
-
update
default List<T> update(int index, @NonNull java.util.function.Function<? super T,? extends T> updater)
Description copied from interface:SeqReturns a newSeqwith the element at the specified index updated using the given function.
-
zip
default <U> List<Tuple2<T,U>> zip(@NonNull java.lang.Iterable<? extends U> that)
Description copied from interface:TraversableReturns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable. Pairing stops when either collection runs out of elements; any remaining elements in the longer collection are ignored.The length of the resulting
Traversableis the minimum of the lengths of thisTraversableandthat.- Specified by:
zipin interfaceLinearSeq<T>- Specified by:
zipin interfaceSeq<T>- Specified by:
zipin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the second half of each pair- Parameters:
that- anIterableproviding the second element of each pair- Returns:
- a new
Traversablecontaining pairs of corresponding elements
-
zipWith
default <U,R> List<R> zipWith(@NonNull java.lang.Iterable<? extends U> that, java.util.function.BiFunction<? super T,? super U,? extends R> mapper)
Description copied from interface:TraversableReturns aTraversableby combining elements of thisTraversablewith elements of anotherIterableusing a mapping function. Pairing stops when either collection runs out of elements.The length of the resulting
Traversableis the minimum of the lengths of thisTraversableandthat.- Specified by:
zipWithin interfaceLinearSeq<T>- Specified by:
zipWithin interfaceSeq<T>- Specified by:
zipWithin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the second parameter of the mapperR- the type of elements in the resultingTraversable- Parameters:
that- anIterableproviding the second parameter of the mappermapper- a function that combines elements from this andthatinto a new element- Returns:
- a new
Traversablecontaining mapped elements
-
zipAll
default <U> List<Tuple2<T,U>> zipAll(@NonNull java.lang.Iterable<? extends U> that, T thisElem, U thatElem)
Description copied from interface:TraversableReturns aTraversableformed by pairing elements of thisTraversablewith elements of anotherIterable, filling in placeholder elements when one collection is shorter than the other.The length of the resulting
Traversableis the maximum of the lengths of thisTraversableandthat.If this
Traversableis shorter thanthat,thisElemis used as a filler. Conversely, ifthatis shorter,thatElemis used.- Specified by:
zipAllin interfaceLinearSeq<T>- Specified by:
zipAllin interfaceSeq<T>- Specified by:
zipAllin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the second half of each pair- Parameters:
that- anIterableproviding the second element of each pairthisElem- the element used to fill missing values if thisTraversableis shorter thanthatthatElem- the element used to fill missing values ifthatis shorter than thisTraversable- Returns:
- a new
Traversablecontaining pairs of elements, including fillers as needed
-
zipWithIndex
default List<Tuple2<T,java.lang.Integer>> zipWithIndex()
Description copied from interface:TraversableZips thisTraversablewith its indices, starting at 0.- Specified by:
zipWithIndexin interfaceLinearSeq<T>- Specified by:
zipWithIndexin interfaceSeq<T>- Specified by:
zipWithIndexin interfaceTraversable<T>- Returns:
- a new
Traversablecontaining each element paired with its index
-
zipWithIndex
default <U> List<U> zipWithIndex(@NonNull java.util.function.BiFunction<? super T,? super java.lang.Integer,? extends U> mapper)
Description copied from interface:TraversableZips thisTraversablewith its indices and maps the resulting pairs using the provided mapper.- Specified by:
zipWithIndexin interfaceLinearSeq<T>- Specified by:
zipWithIndexin interfaceSeq<T>- Specified by:
zipWithIndexin interfaceTraversable<T>- Type Parameters:
U- the type of elements in the resultingTraversable- Parameters:
mapper- a function mapping an element and its index to a new element- Returns:
- a new
Traversablecontaining the mapped elements
-
-