Kotlin Set union Example

The union operation returns a set containing all distinct elements from both collections.

Kotlin Set union Example

In the example, we have two sets of integers. We join the sets with the union() method.
package net.sourcecodeexamples.kotlin

fun main(args: Array<String>) {

    val nums = setOf(1, 2, 3, 4,5)
    val nums2 = setOf(3, 4, 5,6,7,8,9)

    val nums3 = nums.union(nums2)

    println(nums3)
}
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]


Comments