The toLongArray() extension function in Kotlin returns an array of Long containing all of the elements of the collection.
The syntax of Set.toLongArray() extension function is:
fun Collection<Long>.toLongArray(): LongArray
This toLongArray() extension function of Set returns an array of Long containing all of the elements of this collection.
Set.toLongArray() returns value of type LongArray
.
Using toLongArray() to convert a set of Long values to a LongArray.
For example,
fun main() {
val longSet = setOf(1L, 2L, 3L)
val longArray = longSet.toLongArray()
println(longArray.joinToString())
}
1, 2, 3
Using toLongArray() to handle an empty set of Long values.
For example,
fun main() {
val emptySet = emptySet<Long>()
val longArray = emptySet.toLongArray()
println(longArray.joinToString())
}
Using toLongArray() to convert a set of mixed Long values to a LongArray.
For example,
fun main() {
val mixedSet = setOf(1L, -2L, 3L, -4L)
val longArray = mixedSet.toLongArray()
println(longArray.joinToString())
}
1, -2, 3, -4
In this Kotlin tutorial, we learned about toLongArray() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.