The asIterable() extension function for sets in Kotlin returns the set itself as an Iterable, allowing for use in contexts that require an Iterable.
The syntax of Set.asIterable() extension function is:
fun <T> Set<T>.asIterable(): Iterable<T>
This asIterable() extension function of Set returns this set as an Iterable.
Set.asIterable() returns value of type Iterable
.
In Kotlin, we can use the asIterable()
function to treat a set as an Iterable.
For example,
asIterable()
function to get an Iterable from the set.println
function.fun main(args: Array<String>) {
val numbers = setOf(1, 2, 3, 4, 5)
val iterable: Iterable<Int> = numbers.asIterable()
for (number in iterable) {
println(number)
}
}
1 2 3 4 5
In Kotlin, we can use the asIterable()
function to treat a set of strings as an Iterable.
For example,
asIterable()
function to get an Iterable from the set.println
function.fun main(args: Array<String>) {
val fruits = setOf("apple", "banana", "cherry")
val iterable: Iterable<String> = fruits.asIterable()
for (fruit in iterable) {
println(fruit)
}
}
apple banana cherry
In Kotlin, we can use the asIterable()
function to treat an empty set as an Iterable.
For example,
asIterable()
function to get an Iterable from the empty set.println
function.fun main(args: Array<String>) {
val emptySet = emptySet<Int>()
val iterable: Iterable<Int> = emptySet.asIterable()
for (number in iterable) {
println(number)
}
}
In this Kotlin tutorial, we learned about asIterable() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.