美文网首页
每天学一点 Kotlin -- 集合:Map

每天学一点 Kotlin -- 集合:Map

作者: 冯可乐同学 | 来源:发表于2021-11-08 14:24 被阅读0次

----《第一季Kotlin崛起:次世代Android开发 》学习笔记

总目录:每天学一点 Kotlin ---- 目录
上一篇:每天学一点 Kotlin -- 集合:MutableSet
下一篇:每天学一点 Kotlin -- 集合:MutableMap

1. 集合类型

1.1 根据集合类型的整体特性,Kotlin 中分为3种集合:
(1) 有序可重复: Array -- 数组
(2) 有序不重复: Set
(3) 无序不重复: Map

2. Map

2.1 定义:

mapOf<Key, Value>(Pair(key, value), ...)

这里的<Key, Value>是指定 Map 的键值对类型,跟定义数组和集合不一样,定义 Map 时并不建议<Key, Value>,这样可以防止后面跟着的圆括号里面的 Pair 元组对初始化时,错误出入不同类型的值。

2.2 内置方法:
(1) 获取某个 key 对应的 value: get, getOrDefault, 或者下标方法
(2) 获取所有的 keys: keys 属性
(3) 获取所有的 values: valus 属性
(4) 获取所有的条目: entires 属性
(5) 变形函数:map 方法

2.3 举个栗子:

fun main() {
//    testMap01()
//    testMap02()
//    testMap03()
    testMap04()
}

fun testMap04() {
    val map3 = mapOf<String, String>(
        Pair("key1", "value1"),
        Pair("key2", "value2"),
        Pair("k3", "V3"),
        Pair("k4", "V4")
    )
    println(map3.toSortedMap())

    print("map3.toList() = ")
    println(map3.toList())

    print("map3.toMutableMap() = ")
    println(map3.toMutableMap())
}

fun testMap03() {
    val map3 = mapOf<String, String>(
        Pair("key1", "value1"),
        Pair("key2", "value2"),
        Pair("k3", "V3"),
        Pair("k4", "V4")
    )

    println(map3.map { "键:" + it.key + "-值:" + it.value })
    println(map3.mapKeys { "键:" + it.key })
    println(map3.mapValues { "值:" + it.value })

    print("最大值依据:")
    println(map3.maxByOrNull { it.value.length })
    print("最小值依据:")
    println(map3.minByOrNull { it.value.length })
}

fun testMap02() {
    // 筛选
    val map2 = mapOf<String, String>(
        Pair("key1", "value1"),
        Pair("key2", "value2"),
        Pair("k3", "V3"),
        Pair("k4", "V4")
    )
    print("map2.filter: ")
    println(map2.filter { it.value.contains("value2") })

    print("map2.filterKeys: ")
    println(map2.filterKeys { it.contains("key2") })

    print("map2.filterValues: ")
    println(map2.filterValues { it.contains("key2") })

    print("map2.filterNot: ")
    println(map2.filterNot { it.value.contains("V") })
}

fun testMap01() {
    val map1 = mapOf<String, String>(
        Pair("key1", "value1"),
        Pair("key2", "value2"),
        Pair("key3", "value3"),
        Pair("key4", "value4")
    )

    println(map1["key3"])
    println(map1.get("key2"))
    println(map1.getOrDefault("key6", "defaultValue"))

    println(map1.keys)
    println(map1.values)
    println(map1.entries)

    println(map1.containsKey("key3"))
    println(map1.containsValue("value5"))
}
相关代码:https://gitee.com/fzq.com/test-demo

相关文章

  • 每天学一点 Kotlin -- 集合:Map

    1. 集合类型 1.1 根据集合类型的整体特性,Kotlin 中分为3种集合:(1) 有序可重复: Array -...

  • 四、Kotlin集合

    "集合:List、Set、Map" 集合:List、Set、Map 与大多数语言不同,Kotlin 区分可变集合和...

  • Kotlin——集合

    Kotlin——集合 Kotlin的集合类由两个接口派生:Collection和Map。Collection和Ma...

  • Kotlin集合——Set集合

    Kotlin的集合类由两个接口派生:Collection和Map。 Kotlin的集合分为两大类:可变集合和不可变...

  • [Kotlin Tutorials 7] Kotlin集合

    Kotlin集合 集合创建 Kotlin标准库提供了set, list和map这三种基本集合类型的实现, 每种类型...

  • Kotlin集合——Map集合

    Kotlin的Map集合用于保存key-value对,其也被分为可变的和不可变的。 一、声明和创建Map集合 Ko...

  • Kotlin 集合

    引言 Kotlin的集合类由两个接口派生:Collection和Map(可以参考kotlin集合派生图),Coll...

  • Kotlin Map集合

    1.声明和创建Map集合 Kotlin提供了如下函数来创建Map集合: 1.MapOf():该函数返回不可变的Ma...

  • 8.集合类型(CollectionType)

    kotlin_集合类型 Array_数组 Set Map swift_集合类型 Array_数组

  • 每天学一点 Kotlin -- 集合:MutableMap

    1. 集合类型 1.1 根据集合类型的整体特性,Kotlin 中分为3种集合:(1) 有序可重复: Array -...

网友评论

      本文标题:每天学一点 Kotlin -- 集合:Map

      本文链接:https://www.haomeiwen.com/subject/bxytaltx.html