The toBooleanArray() extension function in Kotlin returns an array of Boolean containing all of the elements of the collection.
The syntax of Set.toBooleanArray() extension function is:
fun Collection<Boolean>.toBooleanArray(): BooleanArray
This toBooleanArray() extension function of Set returns an array of Boolean containing all of the elements of this collection.
Set.toBooleanArray() returns value of type BooleanArray
.
Using toBooleanArray() to convert a set of Boolean values to a BooleanArray.
For example,
fun main() {
val booleanSet = setOf(true, false, true)
val booleanArray = booleanSet.toBooleanArray()
println(booleanArray.joinToString())
}
true, false, true
Using toBooleanArray() to handle an empty set of Boolean values.
For example,
fun main() {
val emptySet = emptySet<Boolean>()
val booleanArray = emptySet.toBooleanArray()
println(booleanArray.joinToString())
}
Using toBooleanArray() to convert a set of mixed Boolean values to a BooleanArray.
For example,
fun main() {
val mixedSet = setOf(true, false, true, false)
val booleanArray = mixedSet.toBooleanArray()
println(booleanArray.joinToString())
}
true, false, true, false
In this Kotlin tutorial, we learned about toBooleanArray() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.