Kotlin集合—Map/MutableMap

Map:

package com.init.demo.collection

fun main(args: Array<String>) {

    /**
     * Map的定义:mapof<key,value>(Pair(key,value),Pair(key,value),Pair(key,value),...)
     * <key,value> 键值对类型 不建议省略 防止圆括号里面的元组Pair初始化时误输入不同的类型
     * 常规属性: size-元素计数  isEmpty -空否
     * 专有属性:entires  代表Map中所有条目组成的set 每一条类型都是Map.Entry
     * Map中的Entry类型定义一个接口(interface) 借口描述了一个如何实现key和value的标准
     * Pair 具体实现需要符合这个接口标准的实体类型
     */
    val airPorts = mapOf<String,String>(Pair("name","小明"),Pair("age","18")
            ,Pair("gender","男"),Pair("birthday","19930708"),Pair("job","computer")
            ,Pair("city","重庆"),Pair("country","china"),Pair("high","180"),Pair("weight","135"))
    println("获取某个key对应的value:get getOrderDefault 下标方法")
    println(airPorts.get("name"))
    println(airPorts.getOrDefault("sec","您获取错误"))
    println(airPorts["name"])
    println(airPorts["gender"])
    //getOrDefault key有可能不存在或者类型输入错误 查询不到对应的值,就可以输入提示信息
    println("获取所有的key:keys属性")
    println(airPorts.keys)
    println("获取所有的value:values属性")
    println(airPorts.values)
    println("获取所有的条目:entires属性")
    println(airPorts.entries)
    println("检查key和value存在情况:containsKey  containsValue")
    println(airPorts.containsKey("job"))
    println(airPorts.containsValue("computer"))
    //在获取具体的可以和value时,可应用containsKey  containsValue方法先行判断存在性
    //既可以在数组中的高阶函数中使用,也可以在Map中使用
    println("筛选器:filter filterValues filterValues")
    println(airPorts.filter { it.value.contains("computer") })
    println(airPorts.filterValues { it.contains("computer") })
    println(airPorts.filterValues { it.contains("city") })
    //filterNot是filter的变种 不符合条件的筛选出来
    //filterTo  filterNotTo 可以把结果map直接存储到另一个map中
    println(airPorts.filterNot { it.value.contains("computer") })
    println("变形的函数:map")
    val newPorts = airPorts.map { "个性特征:"+it.key +",特征值:"+it.value }
    println(newPorts)
    println("变形的key:mapKeys")
    val newPortsKey = airPorts.mapKeys { "个性特征:"+it.key  }
    println(newPortsKey)
    println("变形的value:mapValues")
    val newPortsValue = airPorts.mapValues { "特征值:"+it.value }
    println(newPortsValue)
    println("极值函数最大值:maxBy")
    println(airPorts.maxBy { it.value.length })
    println("极值函数最小值:mapValues")
    println(airPorts.minBy { it.key.length })
    println("排序 默认按key(字母)来排序:toSortedMap")
    println(airPorts.toSortedMap())
    println("转换成其他集合:toList")
    println(airPorts.toList())
    println("转换成其他集合:toMutableMap")
    println(airPorts.toMutableMap())

    //输出:
    /*获取某个key对应的value:get getOrderDefault 下标方法
    小明
    您获取错误
    小明
    获取所有的key:keys属性
    [name, age, gender, birthday, job, city, country, high, weight]
    获取所有的value:values属性
    [小明, 18, 男, 19930708, computer, 重庆, china, 180, 135]
    获取所有的条目:entires属性
    [name=小明, age=18, gender=男, birthday=19930708, job=computer, city=重庆, country=china, high=180, weight=135]
    检查key和value存在情况:containsKey  containsValue
    true
    true
    筛选器:filter filterValues filterValues
    {job=computer}
    {job=computer}
    {}
    {name=小明, age=18, gender=男, birthday=19930708, city=重庆, country=china, high=180, weight=135}
    变形的函数:map
    [个性特征:name,特征值:小明, 个性特征:age,特征值:18, 个性特征:gender,特征值:男, 个性特征:birthday,
    特征值:19930708, 个性特征:job,特征值:computer, 个性特征:city,特征值:重庆, 个性特征:country,特征值:china,
     个性特征:high,特征值:180, 个性特征:weight,特征值:135]
    变形的key:mapKeys
    {个性特征:name=小明, 个性特征:age=18, 个性特征:gender=男, 个性特征:birthday=19930708, 个性特征:job=computer,
     个性特征:city=重庆, 个性特征:country=china, 个性特征:high=180, 个性特征:weight=135}
    变形的value:mapValues
    {name=特征值:小明, age=特征值:18, gender=特征值:男, birthday=特征值:19930708, job=特征值:computer, city=特征值
    :重庆, country=特征值:china, high=特征值:180, weight=特征值:135}
    极值函数最大值:maxBy
    birthday=19930708
    极值函数最小值:mapValues
    age=18
    排序 默认按key(字母)来排序:toSortedMap
            {age=18, birthday=19930708, city=重庆, country=china, gender=男, high=180, job=computer, name=小明, weight=135}
    转换成其他集合:toList
    [(name, 小明), (age, 18), (gender, 男), (birthday, 19930708), (job, computer),
     (city, 重庆), (country, china), (high, 180), (weight, 135)]
    转换成其他集合:toMutableMap
    {name=小明, age=18, gender=男, birthday=19930708, job=computer, city=重庆, country=china, high=180, weight=135}*/
}

MutableMap

package com.init.demo.collection

fun main(args: Array<String>) {
    /**
     * MutableMap<String,String>
     *  定义:mutableMapOf<key,value>(Pair(key,value),Pair(key,value),Pair(key,value),...)
     */
    val airPorts = mapOf<String,String>(Pair("name","小明"),Pair("age","18")
            ,Pair("gender","男"),Pair("birthday","19930708"),Pair("job","computer")
            ,Pair("city","重庆"),Pair("country","china"),Pair("high","180"),Pair("weight","135"))
    val muAirPorts = airPorts.toMutableMap()

    println("添加或更新:put 下标")
    muAirPorts["kk"]= "kkhh"
    muAirPorts["AVG"]= "1823"
    muAirPorts.put("TURN","OFF")
    muAirPorts.putIfAbsent("TURN1","ON")
    println(muAirPorts)
    println("添加其他Map:putAll 或 += 两者等价")
    val airPorts1 = mapOf<String,String>(Pair("name1","小明"),Pair("age1","18")
            ,Pair("gender1","男"),Pair("birthday1","19930708"),Pair("job1","computer"))
    muAirPorts.putAll(airPorts1)
    println(muAirPorts)
    val airPorts2 = mapOf<String,String>(Pair("name2","小明"),Pair("age2","18")
            ,Pair("gender2","男"),Pair("birthday2","19930708"))
    muAirPorts += airPorts2
    println(muAirPorts)
    println("移除键值对: remove")
    muAirPorts.remove("name2")
    println(muAirPorts)
    println("清空Map: clear")
    muAirPorts.clear()
    println(muAirPorts)

    //输出:
    /*添加或更新:put 下标
    {name=小明, age=18, gender=男, birthday=19930708, job=computer, city=重庆, country=china, high=180, weight=135,
     kk=kkhh, AVG=1823, TURN=OFF, TURN1=ON}
    添加其他Map:putAll 或 += 两者等价
    {name=小明, age=18, gender=男, birthday=19930708, job=computer, city=重庆, country=china, high=180, weight=135,
     kk=kkhh, AVG=1823, TURN=OFF, TURN1=ON, name1=小明, age1=18, gender1=男, birthday1=19930708, job1=computer}
    {name=小明, age=18, gender=男, birthday=19930708, job=computer, city=重庆, country=china, high=180, weight=135,
    kk=kkhh, AVG=1823, TURN=OFF, TURN1=ON, name1=小明, age1=18, gender1=男, birthday1=19930708, job1=computer,
     name2=小明, age2=18, gender2=男, birthday2=19930708}
    移除键值对: remove
    {name=小明, age=18, gender=男, birthday=19930708, job=computer, city=重庆, country=china, high=180, weight=135,
    kk=kkhh, AVG=1823, TURN=OFF, TURN1=ON, name1=小明, age1=18, gender1=男, birthday1=19930708, job1=computer,
    age2=18, gender2=男, birthday2=19930708}
    清空Map: clear
    {}*/


}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值