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