Kotlin List asIterable()
Syntax & Examples
Syntax of List.asIterable()
The syntax of List.asIterable() extension function is:
fun <T> Iterable<T>.asIterable(): Iterable<T>
This asIterable() extension function of List returns this collection as an Iterable.
✐ Example
1 Example
In this example,
- We create a list containing integers from 1 to 5.
- We use the 'asIterable' function to convert the list into an Iterable.
- We iterate over the Iterable using a for loop and print each element.
- The output will be the elements of the list printed one by one.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5)
val iterable = list.asIterable()
for (item in iterable) {
println(item)
}
}
Output
1 2 3 4 5
Summary
In this Kotlin tutorial, we learned about asIterable() extension function of List: the syntax and few working examples with output and detailed explanation for each example.