<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [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允許重復。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看