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