Package-level declarations

Sequence type that represents lazily evaluated collections. Top-level functions for instantiating sequences and extension functions for sequences.

Classification of sequences

The sequence operations can be classified into the following groups regarding their state requirements:

  • stateless – operations which require no state and process each element independently like kotlin.sequences.Sequence.map, kotlin.sequences.Sequence.filter, or require a small constant amount of state to process an element, for example kotlin.sequences.Sequence.take or kotlin.sequences.Sequence.drop;

  • stateful – operations which require a significant amount of state, usually proportional to the number of elements in a sequence.

If the sequence operation returns another sequence, which is produced lazily, it's called intermediate, and otherwise the operation is terminal. Examples of terminal operations are kotlin.sequences.Sequence.toList, kotlin.sequences.Sequence.max.

Sequences can be iterated multiple times, however some sequence implementations might constrain themselves to be iterated only once. That is mentioned specifically in their documentation (e.g. kotlin.sequences.generateSequence overload). The latter sequences throw an exception on an attempt to iterate them the second time.

Functions

Link copied to clipboard
inline fun <T> Enumeration<T>.asSequence(): Sequence<T>

Creates a sequence that returns all values from this enumeration. The sequence is constrained to be iterated only once.

Since Kotlin 1.0
Link copied to clipboard
fun <R> Sequence<*>.filterIsInstance(klass: Class<R>): Sequence<R>

Returns a sequence containing all elements that are instances of specified class.

Since Kotlin 1.0
Link copied to clipboard
fun <C : MutableCollection<in R>, R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class<R>): C

Appends all elements that are instances of specified class to the given destination.

Since Kotlin 1.0
Link copied to clipboard
fun <T : Comparable<T>> Sequence<T>.max(): T?
Since Kotlin 1.0
Link copied to clipboard
inline fun <T, R : Comparable<R>> Sequence<T>.maxBy(selector: (T) -> R): T?
Since Kotlin 1.0
Link copied to clipboard
fun <T> Sequence<T>.maxWith(comparator: Comparator<in T>): T?
Since Kotlin 1.0
Link copied to clipboard
fun <T : Comparable<T>> Sequence<T>.min(): T?
Since Kotlin 1.0
Link copied to clipboard
inline fun <T, R : Comparable<R>> Sequence<T>.minBy(selector: (T) -> R): T?
Since Kotlin 1.0
Link copied to clipboard
fun <T> Sequence<T>.minWith(comparator: Comparator<in T>): T?
Since Kotlin 1.0
Link copied to clipboard
fun <T : Comparable<T>> Sequence<T>.toSortedSet(): SortedSet<T>
fun <T> Sequence<T>.toSortedSet(comparator: Comparator<in T>): SortedSet<T>

Returns a new SortedSet of all elements.

Since Kotlin 1.0