isNotEmpty所在位置是kotlin.collections.isNotEmpty,其相关用法介绍如下。

用法一

fun <T> Array<out T>.isNotEmpty(): Boolean
fun ByteArray.isNotEmpty(): Boolean
fun ShortArray.isNotEmpty(): Boolean
fun IntArray.isNotEmpty(): Boolean
fun LongArray.isNotEmpty(): Boolean
fun FloatArray.isNotEmpty(): Boolean
fun DoubleArray.isNotEmpty(): Boolean
fun BooleanArray.isNotEmpty(): Boolean
fun CharArray.isNotEmpty(): Boolean

如果数组不为空,则返回 true

用法二

fun <T> Collection<T>.isNotEmpty(): Boolean

如果集合不为空,则返回 true

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val empty = emptyList<Any>()
println("empty.isNotEmpty() is ${empty.isNotEmpty()}") // false

val collection = listOf('a', 'b', 'c')
println("collection.isNotEmpty() is ${collection.isNotEmpty()}") // true
//sampleEnd
}

输出:

empty.isNotEmpty() is false
collection.isNotEmpty() is true

用法三

fun <K, V> Map<out K, V>.isNotEmpty(): Boolean

如果此映射不为空,则返回 true

例子:

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
}

输出:

<No values>
Total: [1787]