🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## Map接口 [`Map<K, V>`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html)不是`Collection`接口的继承者;但是它也是 Kotlin 的一种集合类型。`Map`存储*键-值*对(或*条目*);键是唯一的,但是不同的键可以与相同的值配对。无论键值对的顺序如何,包含相同键值对的两个`Map`是相等的。[`MutableMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/index.html)是一个具有写操作的`Map`接口,可以使用该接口添加一个新的键值对或更新给定键的值。`Map`的默认实现 –[`LinkedHashMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-linked-hash-map/index.html)– 迭代 Map 时保留元素插入的顺序。 反之,另一种实现 –[`HashMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/index.html)– 不声明元素的顺序。 ``` interface Map<K, out V> ``` ### Map接口简介 在一个公司中,每个员工都有唯一的工号,通过工号可以查询到这个员工的信息,这两者是一对一的关系。在应用程序中,如果想储存这种具有对应关系的数据,则需要使用Kotlin中提供的Map接口。Map接口是一种双列集合,它的每个元素都包含一个键对象Key和一个值对象Value,键和值对象之间存在一种对应关系,称为映射。从Map集合中访问元素时,只要指定了Key,就能找到对应的Value。 Map集合中的元素是无序可重复的,Map集合与List、Set集合类似,同样分为不可变集合Map和可变集合MutableMap两种,其中可变集合MutableMap可以对集合中的元素进行增加和删除的操作,不可变集合Map对集合中的元素仅提供只读操作。 ### Map接口的继承结构 参考官网[Inheritors](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html#inheritors) #### AbstractMap [AbstractMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-map/index.html) ``` abstract class AbstractMap<K, out V> : Map<K, V> ``` ##### AbstractMap的继承AbstractMutableMap 参考官网[Inheritors](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-map/index.html#inheritors) * [AbstractMutableMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-mutable-map/index.html) ``` abstract class AbstractMutableMap<K, V> : MutableMap<K, V> ``` 抽象类AbstractMutableMap的继承[(Inheritors)](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-mutable-map/index.html#inheritors)之[HashMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/index.html#hashmap) ``` class HashMap<K, V> : MutableMap<K, V> ``` 抽象类HashMap的继承([Inheritors](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/index.html#inheritors))之[LinkedHashMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-linked-hash-map/index.html) ``` typealias LinkedHashMap<K, V> = HashMap<K, V> ``` #### MutableMap [MutableMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/index.html) ``` interface MutableMap<K, V> : Map<K, V> ``` ##### MutableMap的继承AbstractMutableMap、HashMap和LinkedHashMap 参考官网简介继承[Inheritors](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/index.html#inheritors) 各自的结构可查看上面的介绍 ### 不可变Map ``` val map = mapOf(1 to "江小白", 2 to "小小白", 3 to "江小小") ``` 上述代码中创建了一个不可变的Map集合,其中“1、2、3”为Key值,“江小白、小小白、江小小”为Value值,“to”为Key/Value映射关系的指向,该集合中的操作主要有查询操作,接下来我们针对这个操作进行详细讲解。 #### 查询操作 不可变Map集合的查询操作主要有判断集合是否为空、获取集合中元素的数量、判断集合中是否包含指定的键、判断集合中是否包含指定的值以及根据key(键)获取value(值),如表所示。 ![](https://img.kancloud.cn/ee/e9/eee98869c047ce860fd67eb406fd5551_1377x362.png) #### 遍历操作 Map集合也经常需要进行遍历操作,不过由于该集合中存储的是键值映射关系,所以在遍历时,与List、Set集合有些区别。 ### 可变MutableMap 可变MutableMap集合是使用mutableMapOf ()函数来创建的,具体代码如下: ``` val mMap = mutableMapOf(1 to "1", 2 to "2", 3 to "3") ``` 上述代码中创建了一个可变的MutableMap集合,该集合中的操作主要有修改操作与批量操作,MutableMap集合中的查询操作与不可变集合Map中的查询操作一样。接下来,针对MutableMap集合中的修改操作与批量操作进行详细讲解。 #### 修改操作 MutableMap集合可以对该集合中的元素进行修改操作,这些修改操作主要有向集合中添加元素与移除元素,如表所示。 ![](https://img.kancloud.cn/b1/a1/b1a13f099d8b63d45f1434d310511cdc_1363x199.png) #### 批量操作 MutableMap集合中的批量操作的方法有putAll(from:Map<out K, V>)和clear(),这两个方法的含义分别是向集合中添加一个集合与清空集合中的映射。 >[success]注意: 无论是Map还是MutableMap,获取到的键、值或者键/值对的Set都是只读的,即便是MutableMap获取的MutableSet也是只读的,因为在Map或者MutableMap中,将这些Set设置为只读常量。使用keySet()函数抽取key序列,将map中的所有keys生成一个Set。使用values()函数抽取value序列,将map中的所有values生成一个Collection。一个生成Set,另一个生成Collection的原因是key是独一无二的,而value允许重复。