toNotEmptyMap

@Since(version = KotoolsTypesVersion.V4_0_0)
fun <K, V> Map<K, V>.toNotEmptyMap(): Result<NotEmptyMap<K, V>>

Returns an encapsulated NotEmptyMap containing all the entries of this map, or returns an encapsulated IllegalArgumentException if this map is empty.

Here's a simple usage example:

var map: Map<Char, Int> = mapOf('a' to 1, 'b' to 2)
var result: Result<NotEmptyMap<Char, Int>> = map.toNotEmptyMap()
println(result) // Success({a=1, b=2})

map = emptyMap()
result = map.toNotEmptyMap()
println(result) // Failure(IllegalArgumentException)

Please note that changes made to the original map will not be reflected on the resulting NotEmptyMap.

val original: MutableMap<Char, Int> = mutableMapOf('a' to 1, 'b' to 2)
val notEmptyMap: NotEmptyMap<Char, Int> = original.toNotEmptyMap()
.getOrThrow()
println(original) // {a=1, b=2}
println(notEmptyMap) // {a=1, b=2}

original.clear()
println(original) // {}
println(notEmptyMap) // {a=1, b=2}