create
@ExperimentalSince(version = KotoolsTypesVersion.V4_5_0)
Creates a NotEmptySet containing all the elements of the specified collection, or throws an IllegalArgumentException if the collection is empty.
Here's an example of calling this function from Kotlin code:
val collection: Collection<Int> = setOf(1, 2, 3)
val elements: NotEmptySet<Int> = NotEmptySet.create(collection)
println(elements) // [1, 2, 3]
Content copied to clipboard
The NotEmptySet type being an inline value class, this function is not available yet for Java users.
Please note that changes made to the original collection will not be reflected on the resulting NotEmptySet.
val original: MutableCollection<Int> = mutableSetOf(1, 2, 3)
val integers: NotEmptySet<Int> = NotEmptySet.create(original)
println(original) // [1, 2, 3]
println(integers) // [1, 2, 3]
original.clear()
println(original) // []
println(integers) // [1, 2, 3]
Content copied to clipboard
You can use the NotEmptySet.Companion.createOrNull function for returning null
instead of throwing an exception in case of invalid collection.