<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Map 相關操作 [TOC] 在 [map](collections-overview.html#map) 中,鍵和值的類型都是用戶定義的。對基于鍵的訪問啟用了各種特定于 map 的處理函數,從鍵獲取值到對鍵和值進行單獨過濾。在此頁面上,我們提供了來自標準庫的 map 處理功能的描述。 ## 取鍵與值 要從 Map 中檢索值,必須提供其鍵作為 [`get()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/get.html) 函數的參數。還支持簡寫 `[key]` 語法。 如果找不到給定的鍵,則返回 `null` 。還有一個函數 [`getValue()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-value.html) ,它的行為略有不同:如果在 Map 中找不到鍵,則拋出異常。此外,還有兩個選項可以解決鍵缺失的問題: * [`getOrElse()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-else.html) 與 list 的工作方式相同:對于不存在的鍵,其值由給定的 lambda 表達式返回。 * [`getOrDefault()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-default.html) 如果找不到鍵,則返回指定的默認值。 ```kotlin fun main() { //sampleStart val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3) println(numbersMap.get("one")) println(numbersMap["one"]) println(numbersMap.getOrDefault("four", 10)) println(numbersMap["five"]) // null //numbersMap.getValue("six") // exception! //sampleEnd } ``` 要對 map 的所有鍵或所有值執行操作,可以從屬性 `keys` 和 `values` 中相應地檢索它們。 `keys` 是 Map 中所有鍵的集合, `values` 是 Map 中所有值的集合。 ```kotlin fun main() { //sampleStart val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3) println(numbersMap.keys) println(numbersMap.values) //sampleEnd } ``` ## 過濾 可以使用 [`filter()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter.html) 函數來[過濾](collection-filtering.html) map 或其他集合。對 map 使用 `filter()` 函數時, `Pair` 將作為參數的謂詞傳遞給它。它將使用謂詞同時過濾其中的鍵和值。 ```kotlin fun main() { //sampleStart val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11) val filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10} println(filteredMap) //sampleEnd } ``` 還有兩種用于過濾 map 的特定函數:按鍵或按值。 這兩種方式,都有對應的函數: [`filterKeys()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-keys.html) 和 [`filterValues()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-values.html) 。兩者都將返回一個新 Map ,其中包含與給定謂詞相匹配的條目。`filterKeys()` 的謂詞僅檢查元素鍵, `filterValues()` 的謂詞僅檢查值。 ```kotlin fun main() { //sampleStart val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11) val filteredKeysMap = numbersMap.filterKeys { it.endsWith("1") } val filteredValuesMap = numbersMap.filterValues { it < 10 } println(filteredKeysMap) println(filteredValuesMap) //sampleEnd } ``` ## `plus` 與 `minus` 操作 由于需要訪問元素的鍵,[`plus`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus.html)(`+`)與 [`minus`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus.html)(`-`)運算符對 map 的作用與其他集合不同。`plus` 返回包含兩個操作數元素的 `Map` :左側的 Map 與右側的 Pair 或另一個 Map 。當右側操作數中有左側 `Map` 中已存在的鍵時,該條目將使用右側的值。 ```kotlin fun main() { //sampleStart val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3) println(numbersMap + Pair("four", 4)) println(numbersMap + Pair("one", 10)) println(numbersMap + mapOf("five" to 5, "one" to 11)) //sampleEnd } ``` `minus` 將根據左側 `Map` 條目創建一個新 `Map` ,右側操作數帶有鍵的條目將被剔除。因此,右側操作數可以是單個鍵或鍵的集合: list 、 set 等。 ```kotlin fun main() { //sampleStart val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3) println(numbersMap - "one") println(numbersMap - listOf("two", "four")) //sampleEnd } ``` 關于在可變 Map 中使用 [`plusAssign`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus-assign.html)(`+=`)與 [`minusAssign`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus-assign.html)(`-=`)運算符的詳細信息,請參見 [Map 寫操作](#map-寫操作) 。 ## Map 寫操作 [Mutable](collections-overview.html#集合類型) Map (可變 Map )提供特定的 Map 寫操作。這些操作使你可以使用鍵來訪問或更改 Map 值。 Map 寫操作的一些規則: * 值可以更新。 反過來,鍵也永遠不會改變:添加條目后,鍵是不變的。 * 每個鍵都有一個與之關聯的值。也可以添加和刪除整個條目。 下面是對可變 Map 中可用寫操作的標準庫函數的描述。 ### 添加與更新條目 要將新的鍵值對添加到可變 Map ,請使用 [`put()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/put.html) 。將新條目放入 `LinkedHashMap` (Map的默認實現)后,會添加該條目,以便在 Map 迭代時排在最后。在 Map 類中,新元素的位置由其鍵順序定義。 ```kotlin fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2) numbersMap.put("three", 3) println(numbersMap) //sampleEnd } ``` 要一次添加多個條目,請使用 [`putAll()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/put-all.html) 。它的參數可以是 `Map` 或一組 `Pair` : `Iterable` 、 `Sequence` 或 `Array` 。 ```kotlin fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3) numbersMap.putAll(setOf("four" to 4, "five" to 5)) println(numbersMap) //sampleEnd } ``` 如果給定鍵已存在于 Map 中,則 `put()` 與 `putAll()` 都將覆蓋值。 因此,可以使用它們來更新 Map 條目的值。 ```kotlin fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2) val previousValue = numbersMap.put("one", 11) println("value associated with 'one', before: $previousValue, after: ${numbersMap["one"]}") println(numbersMap) //sampleEnd } ``` 還可以使用快速操作符將新條目添加到 Map 。 有兩種方式: * [`plusAssign`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus-assign.html) (`+=`) 操作符。 * `[]` 操作符為 `put()` 的別名。 ```kotlin fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2) numbersMap["three"] = 3 // 調用 numbersMap.put("three", 3) numbersMap += mapOf("four" to 4, "five" to 5) println(numbersMap) //sampleEnd } ``` 使用 Map 中存在的鍵進行操作時,將覆蓋相應條目的值。 ### 刪除條目 要從可變 Map 中刪除條目,請使用 [`remove()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/remove.html) 函數。調用 `remove()` 時,可以傳遞鍵或整個鍵值對。如果同時指定鍵和值,則僅當鍵值都匹配時,才會刪除此的元素。 ```kotlin fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3) numbersMap.remove("one") println(numbersMap) numbersMap.remove("three", 4) //不會刪除任何條目 println(numbersMap) //sampleEnd } ``` 還可以通過鍵或值從可變 Map 中刪除條目。在 Map 的 `.keys` 或 `.values` 中調用 `remove()` 并提供鍵或值來刪除條目。在 `.values` 中調用時, `remove()` 僅刪除給定值匹配到的的第一個條目。 ```kotlin fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "threeAgain" to 3) numbersMap.keys.remove("one") println(numbersMap) numbersMap.values.remove(3) println(numbersMap) //sampleEnd } ``` [`minusAssign`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus-assign.html) (`-=`) 操作符也可用于可變 Map 。 ```kotlin fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3) numbersMap -= "two" println(numbersMap) numbersMap -= "five" //不會刪除任何條目 println(numbersMap) //sampleEnd } ```
                  <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>

                              哎呀哎呀视频在线观看