Kotlin Set toDoubleArray()
Syntax & Examples
Set.toDoubleArray() extension function
The toDoubleArray() extension function in Kotlin returns an array of Double containing all of the elements of the collection.
Syntax of Set.toDoubleArray()
The syntax of Set.toDoubleArray() extension function is:
fun Collection<Double>.toDoubleArray(): DoubleArray
This toDoubleArray() extension function of Set returns an array of Double containing all of the elements of this collection.
Return Type
Set.toDoubleArray() returns value of type DoubleArray
.
✐ Examples
1 Converting a set of Double values to a DoubleArray
Using toDoubleArray() to convert a set of Double values to a DoubleArray.
For example,
- Create a set of Double values.
- Use toDoubleArray() to convert the set to a DoubleArray.
- Print the resulting array.
Kotlin Program
fun main() {
val doubleSet = setOf(1.1, 2.2, 3.3)
val doubleArray = doubleSet.toDoubleArray()
println(doubleArray.joinToString())
}
Output
1.1, 2.2, 3.3
2 Handling an empty set of Double values
Using toDoubleArray() to handle an empty set of Double values.
For example,
- Create an empty set of Double values.
- Use toDoubleArray() to convert the empty set to a DoubleArray.
- Print the resulting array.
Kotlin Program
fun main() {
val emptySet = emptySet<Double>()
val doubleArray = emptySet.toDoubleArray()
println(doubleArray.joinToString())
}
Output
3 Converting a set of mixed Double values to a DoubleArray
Using toDoubleArray() to convert a set of mixed Double values to a DoubleArray.
For example,
- Create a set of mixed Double values.
- Use toDoubleArray() to convert the set to a DoubleArray.
- Print the resulting array.
Kotlin Program
fun main() {
val mixedSet = setOf(1.1, -2.2, 3.3, -4.4)
val doubleArray = mixedSet.toDoubleArray()
println(doubleArray.joinToString())
}
Output
1.1, -2.2, 3.3, -4.4
Summary
In this Kotlin tutorial, we learned about toDoubleArray() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.