Kotlin List.asSequence()

The Kotlin List.asSequence() function is used to create a sequence from given list of elements.

Syntax

The syntax of List.asSequence() function is

</>
Copy
list.asSequence()

Return Value

Sequence object.

Examples

In the following program, we

Main.kt

</>
Copy
fun main(args: Array<String>) {
    var list = listOf(25, 50, 75, 100)
    var seq = list.asSequence()
    seq.forEach {
        println(it)
    }
}

Output

25
50
75
100

Conclusion

In this Kotlin Tutorial, we learned how to convert a list to sequence, using Kotlin List.asSequence() function.