「Kotlin」isNotEmptyメソッドでmapの要素が空であるかを判定するサンプル

環境
Windows11 pro 64bit
Java 17.0.2
kotlin 1.6.10

書式
val map名 = mutableMapOf(要素1, 要素2,…)
map名.isNotEmpty()
isNotEmptyメソッドを利用してmapの要素が空であるかを判定します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
fun main() {
val cftA = mutableMapOf('s' to 22, 't' to 33)
val cftB: Map<Char, Int> = mutableMapOf()
val cftC: Map<Char?, Int?> = mutableMapOf(null to null)
println( cftA.isNotEmpty())
println( cftB.isNotEmpty())
println( cftC.isNotEmpty() )
}
fun main() { val cftA = mutableMapOf('s' to 22, 't' to 33) val cftB: Map<Char, Int> = mutableMapOf() val cftC: Map<Char?, Int?> = mutableMapOf(null to null) println( cftA.isNotEmpty()) println( cftB.isNotEmpty()) println( cftC.isNotEmpty() ) }
fun main() { 
     val cftA = mutableMapOf('s' to 22, 't' to 33)
    val cftB: Map<Char, Int>  = mutableMapOf()
    val cftC: Map<Char?, Int?>  = mutableMapOf(null to null)
    
    println( cftA.isNotEmpty()) 
    println( cftB.isNotEmpty()) 
    println( cftC.isNotEmpty() ) 
}

実行結果
true
false
true

Kotlin

Posted by arkgame