Returns true
if the array is not empty.
Returns true
if the collection is not empty.
Returns true
if this map is not empty.
import kotlin.test.*
import java.util.*
fun main(args: Array<String>) {
//sampleStart
fun totalValue(statisticsMap: Map<String, Int>): String =
when {
statisticsMap.isNotEmpty() -> {
val total = statisticsMap.values.sum()
"Total: [$total]"
}
else -> "<No values>"
}
val emptyStats: Map<String, Int> = mapOf()
println(totalValue(emptyStats)) // <No values>
val stats: Map<String, Int> = mapOf("Store #1" to 1247, "Store #2" to 540)
println(totalValue(stats)) // Total: [1787]
//sampleEnd
}
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/is-not-empty.html