Sequences
Sequences are a type in Kotlin that provide similar features to other collections, but process data in a lazy and efficient manner. This lazy approach can be useful for processing collections, particularly when working with large amounts of data.
Unlike a List
or an Array
, which store all their elements in memory, a Sequence
computes elements on-the-fly as they are requested.
Syntax
Sequences can be created by multiple methods, the primary options are:
val numbers = sequenceOf(2, 8, 3, 7, 10)
val letters = listOf("A", "B", "C")
val lettersSequence = letters.asSequence()
val squaresSequence = generateSequence(2) { if(it < 20) it * it else null}
- A sequence can be created by passing elements to the
sequenceOf()
method. - An existing collection, such as an array or a list, can be converted into a sequence using the
asSequence()
extension function. - A sequence can also be generated using the
generateSequence()
function. This function takes a starting value or seed, and a lambda function.
Note: In the example above the lambda expression terminates the sequence using
null
. A sequence generated withoutnull
is infinite and will result in an error if a function is called on the entire sequence.
Example
In this example, generateSequence()
function starts with an initial value of 2 and repeatedly applies a function that increments the value by 2 in each iteration. As a result, an infinite sequence of even numbers can be generated. However, to print the sequence’s initial ten elements to the console, these elements are extracted using the take(10)
function and stored in the variable firstTenEven
.
fun main() {val evenNumbers = generateSequence(2) { it + 2 }val firstTenEven = evenNumbers.take(10).toList()println(firstTenEven)}
The example will result in the following output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours