toNotEmptySet
@Since(version = KotoolsTypesVersion.V4_0_0)
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)
Content copied to clipboard
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]
Content copied to clipboard