create
@ExperimentalSince(version = KotoolsTypesVersion.V4_5_0)
Creates a NotEmptyMap containing all the entries of the specified map, or throws an IllegalArgumentException if the map is empty.
Calling from Kotlin
Here's an example for calling this method from Kotlin code:
val map: Map<Char, Int> = mapOf('a' to 1, 'b' to 2)
val isSuccess: Boolean = try {
NotEmptyMap.create(map)
true
} catch (exception: IllegalArgumentException) {
false
}
assertTrue(isSuccess)
Content copied to clipboard
The NotEmptyMap type being an inline value class, this method is not available yet for Java users.
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> = NotEmptyMap.create(original)
assertEquals(expected = "$original", actual = "$notEmptyMap")
original.clear()
assertNotEquals(illegal = "$original", actual = "$notEmptyMap")
Content copied to clipboard
You can use the NotEmptyMap.Companion.createOrNull method for returning null
instead of throwing an exception in case of invalid map.