kotlin mutableMapの値を追加するサンプル

環境
Windows11 pro 64bit
java 19.0.1
kotlin 1.7.20-release-201

構文
map名[key] = 値
or
map名.put = ( key , 値 )
mutableMapの値を追加します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
fun main() {
val m = mutableMapOf('a' to 3, 'b' to 6, 'c' to 9)
m['d'] = 4
m.put( 'e', 5 )
println(m)
}
fun main() { val m = mutableMapOf('a' to 3, 'b' to 6, 'c' to 9) m['d'] = 4 m.put( 'e', 5 ) println(m) }
fun main() {

    val m = mutableMapOf('a' to 3, 'b' to 6, 'c' to 9)

    m['d'] = 4

    m.put( 'e', 5 )

    println(m) 
    
}

実行結果
{a=3, b=6, c=9, d=4, e=5}

複数の値を追加する場合は、同じmapを演算子で追加します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
fun main() {
val m = mutableMapOf('a' to 21, 'b' to 42, 'c' to 53)
m += mutableMapOf('d' to 31, 'e' to 22, 'f' to 23)
println(m)
}
fun main() { val m = mutableMapOf('a' to 21, 'b' to 42, 'c' to 53) m += mutableMapOf('d' to 31, 'e' to 22, 'f' to 23) println(m) }
fun main() {

     val m = mutableMapOf('a' to 21, 'b' to 42, 'c' to 53)

    m += mutableMapOf('d' to 31, 'e' to 22, 'f' to 23)

    println(m) 
    
}

実行結果
{a=21, b=42, c=53, d=31, e=22, f=23}

Kotlin

Posted by arkgame