0Pricing
Kotlin Academy · Lesson

Creating Sequences

From collections and generators.

Creating Sequences

There are several ways to create a Sequence: from existing collections, from elements, with a generator function, or with a builder. This lesson covers each.

From a Collection with asSequence

The most common approach is to call asSequence() on an existing collection to process it lazily.

fun main() {
    val seq = listOf(1, 2, 3, 4).asSequence()
    val result = seq.map { it * 2 }.toList()
    println(result)
}

All lessons in this course

  1. Why Sequences
  2. Creating Sequences
  3. Lazy Operations
  4. Sequences vs Collections
← Back to Kotlin Academy