The Sequence class in Kotlin is part of the kotlin.sequences package. It provides a way to create lazy, one-time iterable collections. Sequences are particularly useful when you have a large data set and you want to avoid the overhead of storing the entire collection in memory.
This guide covers various functions available in the Kotlin Sequence class. Each function is described in simple terms to help beginners understand how to use them. These functions enable you to perform operations such as transforming, filtering, and collecting elements in a lazy fashion, which can help improve performance in certain scenarios.
Kotlin Sequence Functions
The table below contains various functions of the Kotlin Sequence class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.
| Method | Description |
|---|---|
| Kotlin Sequence asSequence Function | Converts an Iterable into a Sequence. |
| Kotlin Sequence map Function | Transforms each element in the sequence using a specified function. |
| Kotlin Sequence filter Function | Returns a sequence containing only elements matching the given predicate. |
| Kotlin Sequence flatMap Function | Transforms each element into a sequence of results and flattens the sequences into a single sequence. |
| Kotlin Sequence toList Function | Converts the sequence into a list. |
| Kotlin Sequence generateSequence Function | Generates a sequence starting from the given initial value and applying the given function to the previous element to produce the next element. |
| Kotlin Sequence sequenceOf Function | Creates a sequence of the specified elements. |
| Kotlin Sequence sorted Function | Returns a sequence containing all elements sorted according to their natural sort order. |
| Kotlin Sequence sortedBy Function | Returns a sequence containing all elements sorted according to the specified selector function. |
| Kotlin Sequence sortedByDescending Function | Returns a sequence containing all elements sorted in descending order according to the specified selector function. |
| Kotlin Sequence sortedWith Function | Returns a sequence containing all elements sorted according to the specified comparator. |
| Kotlin Sequence distinct Function | Returns a sequence containing only distinct elements from the given sequence. |
| Kotlin Sequence distinctBy Function | Returns a sequence containing only elements from the given sequence having distinct keys returned by the specified selector function. |
| Kotlin Sequence take Function | Returns a sequence containing the first n elements. |
| Kotlin Sequence takeWhile Function | Returns a sequence containing the first elements that satisfy the given predicate. |
| Kotlin Sequence drop Function | Returns a sequence containing all elements except the first n elements. |
| Kotlin Sequence dropWhile Function | Returns a sequence containing the elements starting from the first element that does not satisfy the given predicate. |
| Kotlin Sequence toSet Function | Converts the sequence into a set. |
| Kotlin Sequence toMutableList Function | Converts the sequence into a mutable list. |
| Kotlin Sequence associate Function | Returns a map containing key-value pairs provided by transform function applied to elements of the given sequence. |
| Kotlin Sequence associateBy Function | Returns a map containing the values provided by valueTransform and indexed by the specified selector function applied to elements of the given sequence. |
| Kotlin Sequence groupBy Function | Returns a map of the elements in the sequence grouped by the key returned by the specified selector function . |
| Kotlin Sequence partition Function | Splits the original sequence into a pair of lists where the first list contains elements for which the predicate yielded true, while the second list contains elements for which the predicate yielded false. |
| Kotlin Sequence plus Function | Returns a sequence of all elements from the original sequence and the given elements. |
| Kotlin Sequence filterNot Function | Returns a sequence containing only elements matching the given predicate. |
| Kotlin Sequence reduce Function | Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element. |
| Kotlin Sequence count Function | Returns the number of elements matching the given predicate. |
| Kotlin Sequence fold Function | Accumulates value starting with the specified initial value and applying operation from left to right to current accumulator value and each element. |
| Kotlin Sequence sumBy Function | Returns the sum of all values produced by selector function applied to each element in the sequence. |
| Kotlin Sequence average Function | Returns the average of all values produced by selector function applied to each element in the sequence. |
| Kotlin Sequence max Function | Returns the largest element or null if there are no elements. |
| Kotlin Sequence maxBy Function | Returns the first element yielding the largest value of the given function or null if there are no elements. |
| Kotlin Sequence min Function | Returns the smallest element or null if there are no elements. |
| Kotlin Sequence first Function | Returns the first element matching the given predicate, or null if no such element was found. |
| Kotlin Sequence last Function | Returns the last element matching the given predicate, or null if no such element was found. |
| Kotlin Sequence single Function | Returns the single element matching the given predicate, or null if the element was not found or more than one element was found. |
| Kotlin Sequence all Function | Returns true if all elements match the given predicate. |
| Kotlin Sequence any Function | Returns true if at least one element matches the given predicate. |
| Kotlin Sequence indexOf Function | Returns the index of the first element matching the given predicate, or -1 if the element was not found. |
| Kotlin Sequence elementAt Function | Returns the element at the specified index in the sequence or throws an exception if the index is out of bounds. |
| Kotlin Sequence elementAtOrElse Function | Returns the element at the specified index in the sequence or the result of the default value function if the index is out of bounds. |
| Kotlin Sequence toHashSet Function | Returns a HashSet of all elements in the sequence. |
| Kotlin Sequence toSortedSet Function | Returns a SortedSet of all elements in the sequence. |
| Kotlin Sequence toCollection Function | Appends all elements to the given destination. |
| Kotlin Sequence toMutableSet Function | Returns a MutableSet of all elements in the sequence. |
| Kotlin Sequence toMap Function | Returns a map containing key-value pairs provided by transform function applied to elements of the given sequence. |
| Kotlin Sequence toSortedMap Function | Returns a SortedMap containing key-value pairs provided by transform function applied to elements of the given sequence. |
| Kotlin Sequence toMutableMap Function | Returns a MutableMap containing key-value pairs provided by transform function applied to elements of the given sequence. |
| Kotlin Sequence joinToString Function | Creates a string from all the elements separated using the specified separator and using the given prefix and postfix if supplied. |
| Kotlin Sequence toArray Function | Returns an array containing all elements in the sequence. |
| Kotlin Sequence forEach Function | Performs the given action on each element. |
| Kotlin exitProcess Function | Terminates the currently running Java Virtual Machine. |
| Kotlin getTimeNanos Function | Returns the current time in nanoseconds. |
| Kotlin getTimeMillis Function | Returns the current time in milliseconds. |
| Kotlin measureTimeMillis Function | Measures the time it takes to execute the specified block of code in milliseconds. |
| Kotlin measureNanoTime Function | Measures the time it takes to execute the specified block of code in nanoseconds. |
| Kotlin measureTime Function | Measures the time it takes to execute the specified block of code and returns the result as a Duration. |