<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之旅 廣告
                # Kotlin 映射 > 原文: [http://zetcode.com/kotlin/maps/](http://zetcode.com/kotlin/maps/) Kotlin 映射教程顯示了如何在 Kotlin 中使用映射。 映射是對象對的集合。 Kotlin 區分只讀映射和可變映射。 使用`mapOf()`創建只讀映射,并使用`mutableMapOf()`創建可變映射。 ## Kotlin 映射 映射是保存成對對象的集合。 每對都包含一個鍵和一個值。 映射鍵是唯一的; 該映射的每個鍵僅包含一個值。 ## Kotlin `mapOf()` `mapOf()`方法創建具有指定內容的只讀映射,并以成對的列表的形式給出,其中第一個值為鍵,第二個為值。 `KotlinMapOf.kt` ```kt package com.zetcode fun main() { val chars = mapOf(97 to "a", 98 to "b", 120 to "x") println(chars) val user = mapOf("name" to "Luke", "age" to "23") println(user) } ``` 該示例創建了兩個映射。 ```kt {97=a, 98=b, 120=x} {name=Luke, age=23} ``` 這是輸出。 ## Kotlin 哈希映射 可以從 Java 的`HashMap`創建映射。 `KotlinHashMap.kt` ```kt package com.zetcode fun main() { val items = HashMap<String, Int>() items["A"] = 90 items["B"] = 80 items["C"] = 70 for ((k, v) in items) { println("$k = $v") } } ``` 該示例使用 Java 的`HashMap`創建映射,并將值和對打印到控制臺。 ## Kotlin 映射大小 映射的大小(對數)可以使用`size`屬性和`count()`方法確定。 `KotlinMapSize.kt` ```kt package com.zetcode fun main() { val items = mapOf("coins" to 12, "books" to 45, "cups" to 33, "pens" to 2) println("Map has ${items.size} items") println("Map has ${items.count()} items") val n = items.count { it.value > 10 } println("# of values greater that 10: $n") } ``` 該示例計算映射對的數量。 ```kt val n = items.count { it.value > 10 } ``` 使用`count()`方法,我們可以計算出大于十的值。 ```kt Map has 4 items Map has 4 items # of values greater that 10: 3 ``` 這是輸出。 ## Kotlin 條目,鍵,值 Kotlin 映射具有可獲取所有條目,鍵和值的屬性。 `KotlinEntriesKeysValues.kt` ```kt package com.zetcode fun main() { val items = mapOf("coins" to 12, "books" to 45, "cups" to 33) println("Entries: " + items.entries) println("Keys:" + items.keys) println("Values:" + items.values) } ``` 該示例使用`entries`,`keys`和`values`屬性。 ```kt Entries: [coins=12, books=45, cups=33] Keys:[coins, books, cups] Values:[12, 45, 33] ``` 這是輸出。 ## Kotlin `mutableMapOf` 使用`mutableMapOf()`創建一個可變映射。 `KotlinMutableMap.kt` ```kt package com.zetcode fun main() { val user = mutableMapOf("name" to "John Doe", "occupation" to "programmer") println(user) // user.put("location", "USA") user["location"] = "USA" println(user) user.remove("occupation") println(user) user.clear() println(user) if (user.isEmpty()) { println("empty") } else { println("not empty") } } ``` 該示例創建一個可變映射并介紹其方法。 ```kt // user.put("location", "USA") user["location"] = "USA" println(user) ``` 新對將添加到映射。 IntelliJ IDEA 建議分配操作。 ```kt user.remove("occupation") ``` 用`remove()`刪除一對。 ```kt user.clear() ``` 用`clear()`刪除所有對。 ```kt if (user.isEmpty()) { println("empty") } else { println("not empty") } ``` `isEmpty()`方法檢查映射是否為空。 ```kt {name=John Doe, occupation=programmer} {name=John Doe, occupation=programmer, location=USA} {name=John Doe, location=USA} {} empty ``` 這是輸出。 ## Kotlin 獲取值 有幾種方法可以從 Kotlin 映射中檢索值。 `KotlinMapGet.kt` ```kt package com.zetcode fun main() { val items = mapOf("coins" to 12, "books" to 45, "cups" to 33, "pens" to 2) // println(items.get("coins")) println(items["coins"]) println(items.getValue("coins")) println(items.getOrDefault("pens", 0)) println(items.getOrDefault("pencils", 0)) val nOfPencils = items.getOrElse("pencils", { 0 }) println(nOfPencils) } ``` 該示例從映射獲取值。 ```kt // println(items.get("coins")) println(items["coins"]) ``` IntelliJ IDEA 建議使用索引操作代替`get()`。 ```kt println(items.getOrDefault("pens", 0)) ``` `getOrDefault()`返回與鍵對應的值,或者如果不存在鍵,則返回指定的默認值。 ```kt val nOfPencils = items.getOrElse("pencils", { 0 }) ``` `getOrElse()`返回給定鍵的值,或者如果沒有給定鍵的條目,則返回指定函數的結果。 ## Kotlin 包含鍵/值 `containsKey()`檢查映射是否包含鍵。 `containsValue()`檢查映射是否包含值。 `KotlinMapContains.kt` ```kt package com.zetcode fun main() { val items = mapOf("coins" to 12, "books" to 45, "cups" to 33, "pens" to 2) if (items.containsKey("cups")) { println("contains cups") } else { println("does not contain cups") } val value = 25 if (items.containsValue(value)) { println("contains value $value") } else { println("does not contain value $value") } } ``` 該示例檢查映射是否包含鍵`"cups"`和值 25。 ## Kotlin 映射遍歷 使用`forEach()`,我們可以遍歷映射。 `KotlinMapForEach.kt` ```kt package com.zetcode fun main() { val items = mapOf("coins" to 12, "books" to 45, "cups" to 33, "pens" to 2) items.forEach { (k, v) -> println("There are $v $k") } } ``` 該示例使用`forEach()`遍歷映射。 ```kt There are 12 coins There are 45 books There are 33 cups There are 2 pens ``` 這是輸出。 ## Kotlin 映射過濾器 我們可以使用`filterKeys()`,`filterValues()`和`filter()`過濾映射。 `KotlinMapFilter.kt` ```kt package com.zetcode fun main() { val items = mapOf("A" to 90, "B" to 80, "C" to 70, "D" to 60, "E" to 50) val filtered = items.filterKeys { it == "A" || it == "C" } println(filtered) val filtered2 = items.filterValues { it >= 70 } println(filtered2) val filtered3 = items.filter { it.key == "A" || it.value == 50 } println(filtered3) } ``` 該示例過濾映射。 ```kt val filtered = items.filterKeys { it == "A" || it == "C" } ``` 我們過濾出所有與指定鍵匹配的對。 ```kt val filtered2 = items.filterValues { it >= 70 } ``` 我們過濾出所有與指定值匹配的對。 ```kt val filtered3 = items.filter { it.key == "A" || it.value == 50 } ``` 在這里,我們過濾出與給定鍵或值匹配的所有對。 ```kt {A=90, C=70} {A=90, B=80, C=70} {A=90, E=50} ``` 這是輸出。 ## Kotlin 有序映射 使用`sortedMapOf()`創建排序的映射。 `KotlinSortedMap.kt` ```kt package com.zetcode fun main() { val items = mapOf("coins" to 12, "books" to 45, "cups" to 33, "pens" to 2) println(items) val sortedItems = sortedMapOf("coins" to 12, "books" to 45, "cups" to 33, "pens" to 2) println(sortedItems) } ``` 該示例打印未排序和排序的映射。 ```kt {coins=12, books=45, cups=33, pens=2} {books=45, coins=12, cups=33, pens=2} ``` 這些對按鍵排序。 ## Kotlin 映射`any()` 如果至少一個條目與給定謂詞匹配,則`any()`方法返回`true`。 `KotlinMapAny.kt` ```kt package com.zetcode fun main() { val items = mapOf("coins" to 12, "books" to 45, "cups" to 33, "pens" to 2) val value = 12 val hasValue = items.any { it.value == value } if (hasValue) { println("The map has value $value") } else { println("The map does not have value $value") } val isEven: (Int) -> Boolean = { it % 2 == 0 } val hasEvenValue = items.any { isEven(it.value) } if (hasEvenValue) { println("The map has even value(s)") } else { println("The map does not have even value(s)") } } ``` 在該示例中,我們檢查映射是否包含至少一個值 12,以及是否存在至少一個偶數值。 ```kt The map has value 12 The map has even value(s) ``` 這是輸出。 在本教程中,我們介紹了 Kotlin 映射。 您可能也對相關的 Kotlin 教程感興趣: [Kotlin 設置教程](/kotlin/sets/), [Kotlin 數組教程](/kotlin/arrays/)或列出[所有 Kotlin 教程](/all/#kotlin)。
                  <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>

                              哎呀哎呀视频在线观看