toNotEmptySet

@SinceKotoolsTypes(version = "4.0")
fun <E> Collection<E>.toNotEmptySet(): Result<NotEmptySet<E>>

Returns an encapsulated NotEmptySet containing all the elements of this collection, or returns an encapsulated IllegalArgumentException if this collection is empty.

Here's a simple usage example:

var collection: Collection<Int> = setOf(1, 2, 3, 1)
var result: Result<NotEmptySet<Int>> = collection.toNotEmptySet()
println(result) // Success([1, 2, 3])

collection = emptySet()
result = collection.toNotEmptySet()
println(result) // Failure(IllegalArgumentException)

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 notEmptySet: NotEmptySet<Int> = original.toNotEmptySet()
.getOrThrow()
println(original) // [1, 2, 3]
println(notEmptySet) // [1, 2, 3]

original.clear()
println(original) // []
println(notEmptySet) // [1, 2, 3]