Kotlin Tutorials

Kotlin Set toUIntArray()
Syntax & Examples

Set.toUIntArray() extension function

The toUIntArray() extension function in Kotlin returns an array of UInt containing all of the elements of the collection.


Syntax of Set.toUIntArray()

The syntax of Set.toUIntArray() extension function is:

fun Collection<UInt>.toUIntArray(): UIntArray

This toUIntArray() extension function of Set returns an array of UInt containing all of the elements of this collection.

Return Type

Set.toUIntArray() returns value of type UIntArray.



✐ Examples

1 Converting a set of UInt values to a UIntArray

Using toUIntArray() to convert a set of UInt values to a UIntArray.

For example,

  1. Create a set of UInt values.
  2. Use toUIntArray() to convert the set to a UIntArray.
  3. Print the resulting array.

Kotlin Program

fun main() {
    val uintSet = setOf<UInt>(1u, 2u, 3u)
    val uintArray = uintSet.toUIntArray()
    println(uintArray.joinToString())
}

Output

1, 2, 3

2 Handling an empty set of UInt values

Using toUIntArray() to handle an empty set of UInt values.

For example,

  1. Create an empty set of UInt values.
  2. Use toUIntArray() to convert the empty set to a UIntArray.
  3. Print the resulting array.

Kotlin Program

fun main() {
    val emptySet = emptySet<UInt>()
    val uintArray = emptySet.toUIntArray()
    println(uintArray.joinToString())
}

Output


3 Converting a set of mixed UInt values to a UIntArray

Using toUIntArray() to convert a set of mixed UInt values to a UIntArray.

For example,

  1. Create a set of mixed UInt values.
  2. Use toUIntArray() to convert the set to a UIntArray.
  3. Print the resulting array.

Kotlin Program

fun main() {
    val mixedSet = setOf<UInt>(1u, 2u, 255u, 128u)
    val uintArray = mixedSet.toUIntArray()
    println(uintArray.joinToString())
}

Output

1, 2, 255, 128

Summary

In this Kotlin tutorial, we learned about toUIntArray() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.