Kotlin的可变与不可变Map
2019-12-08 本文已影响0人
爱学习的蹭蹭
- mapOf 不可变map
- mutableMapOf可变map
package com.kotlin.flong.base
import java.util.HashMap
/*
* map的使用
*/
fun main(args : Array<String>){
mapOf1()
}
fun mapOf1(){
//不可变
var map1 = mapOf("age" to 29,"userName" to "寅务")
val mapSet1 = map1.keys
//循环遍历
mapSet1.forEach{
println(it)
}
val mapSet2 = map1.values;
//循环遍历
mapSet2.forEach{
println(it)
}
val mapSet3 = map1.entries
//val是关键字故要用·引号
map1.forEach { key,`val` ->
//println("key=$key,value=$`val`")
}
//循环遍历
for((key,value) in map1){
println("key=$key,value=$`value`")
}
//可变
//var map2 = mutableMapOf();
//java 的map
var map3 = HashMap<String,String>()
map3.put("age","23");
map3.put("userName","寅务");
//循环遍历
for(m3 in map3){
println(m3.key + "," + m3.value)
}
}