The toFloatArray() extension function in Kotlin returns an array of Float containing all of the elements of the collection.
The syntax of Set.toFloatArray() extension function is:
fun Collection<Float>.toFloatArray(): FloatArray
This toFloatArray() extension function of Set returns an array of Float containing all of the elements of this collection.
Set.toFloatArray() returns value of type FloatArray
.
Using toFloatArray() to convert a set of Float values to a FloatArray.
For example,
fun main() {
val floatSet = setOf(1.1f, 2.2f, 3.3f)
val floatArray = floatSet.toFloatArray()
println(floatArray.joinToString())
}
1.1, 2.2, 3.3
Using toFloatArray() to handle an empty set of Float values.
For example,
fun main() {
val emptySet = emptySet<Float>()
val floatArray = emptySet.toFloatArray()
println(floatArray.joinToString())
}
Using toFloatArray() to convert a set of mixed Float values to a FloatArray.
For example,
fun main() {
val mixedSet = setOf(1.1f, -2.2f, 3.3f, -4.4f)
val floatArray = mixedSet.toFloatArray()
println(floatArray.joinToString())
}
1.1, -2.2, 3.3, -4.4
In this Kotlin tutorial, we learned about toFloatArray() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.