Kotlin Tutorials

Kotlin Set toULongArray()
Syntax & Examples

Set.toULongArray() extension function

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


Syntax of Set.toULongArray()

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

fun Collection<ULong>.toULongArray(): ULongArray

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

Return Type

Set.toULongArray() returns value of type ULongArray.



✐ Examples

1 Converting a set of ULong values to a ULongArray

Using toULongArray() to convert a set of ULong values to a ULongArray.

For example,

  1. Create a set of ULong values.
  2. Use toULongArray() to convert the set to a ULongArray.
  3. Print the resulting array.

Kotlin Program

fun main() {
    val ulongSet = setOf<ULong>(1uL, 2uL, 3uL)
    val ulongArray = ulongSet.toULongArray()
    println(ulongArray.joinToString())
}

Output

1, 2, 3

2 Handling an empty set of ULong values

Using toULongArray() to handle an empty set of ULong values.

For example,

  1. Create an empty set of ULong values.
  2. Use toULongArray() to convert the empty set to a ULongArray.
  3. Print the resulting array.

Kotlin Program

fun main() {
    val emptySet = emptySet<ULong>()
    val ulongArray = emptySet.toULongArray()
    println(ulongArray.joinToString())
}

Output


3 Converting a set of mixed ULong values to a ULongArray

Using toULongArray() to convert a set of mixed ULong values to a ULongArray.

For example,

  1. Create a set of mixed ULong values.
  2. Use toULongArray() to convert the set to a ULongArray.
  3. Print the resulting array.

Kotlin Program

fun main() {
    val mixedSet = setOf<ULong>(1uL, 2uL, 18446744073709551615uL)
    val ulongArray = mixedSet.toULongArray()
    println(ulongArray.joinToString())
}

Output

1, 2, 18446744073709551615

Summary

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