<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國際加速解決方案。 廣告
                # 集合操作概述 [TOC] Kotlin 標準庫提供了用于對集合執行操作的多種函數。這包括簡單的操作,例如獲取或添加元素,以及更復雜的操作,包括搜索、排序、過濾、轉換等。 ## 擴展與成員函數 集合操作在標準庫中以兩種方式聲明:集合接口的[成員函數](classes.html#類成員)和[擴展函數](extensions.html#擴展函數)。 成員函數定義了對于集合類型是必不可少的操作。例如,[`Collection`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html) 包含函數 [`isEmpty()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html) 來檢查其是否為空; [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html) 包含用于對元素進行索引訪問的 [`get()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/get.html),等等。 創建自己的集合接口實現時,必須實現其成員函數。為了使新實現的創建更加容易,請使用標準庫中集合接口的框架實現:[`AbstractCollection`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-collection/index.html)、[`AbstractList`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-list/index.html)、[`AbstractSet`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-set/index.html)、[`AbstractMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-map/index.html) 及其相應可變抽象類。 其他集合操作被聲明為擴展函數。這些是過濾、轉換、排序和其他集合處理功能。 ## 公共操作 公共操作可用于[只讀集合與可變集合](collections-overview.html#集合類型)。 常見操作分為以下幾類: * [集合轉換](collection-transformations.html) * [集合過濾](collection-filtering.html) * [`plus` 與 `minus` 操作符](collection-plus-minus.html) * [分組](collection-grouping.html) * [取集合的一部分](collection-parts.html) * [取單個元素](collection-elements.html) * [集合排序](collection-ordering.html) * [集合聚合操作](collection-aggregate.html) 這些頁面中描述的操作將返回其結果,而不會影響原始集合。例如,一個過濾操作產生一個_新集合_,其中包含與過濾謂詞匹配的所有元素。 此類操作的結果應存儲在變量中,或以其他方式使用,例如,傳到其他函數中。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") numbers.filter { it.length > 3 } // `numbers` 沒有任何改變,結果丟失 println("numbers are still $numbers") val longerThan3 = numbers.filter { it.length > 3 } // 結果存儲在 `longerThan3` 中 println("numbers longer than 3 chars are $longerThan3") //sampleEnd } ``` 對于某些集合操作,有一個選項可以指定 _目標_ 對象。目標是一個可變集合,該函數將其結果項附加到該可變對象中,而不是在新對象中返回它們。對于執行帶有目標的操作,有單獨的函數,其名稱中帶有 `To` 后綴,例如,用 [`filterTo()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-to.html) 代替 [`filter()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter.html) 以及用 [`associateTo()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate-to.html) 代替 [`associate()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/associate.html)。這些函數將目標集合作為附加參數。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val filterResults = mutableListOf<String>() // 目標對象 numbers.filterTo(filterResults) { it.length > 3 } numbers.filterIndexedTo(filterResults) { index, _ -> index == 0 } println(filterResults) // 包含兩個操作的結果 //sampleEnd } ``` 為了方便起見,這些函數將目標集合返回了,因此您可以在函數調用的相應參數中直接創建它: ```kotlin fun main() { val numbers = listOf("one", "two", "three", "four") //sampleStart // 將數字直接過濾到新的哈希集中, // 從而消除結果中的重復項 val result = numbers.mapTo(HashSet()) { it.length } println("distinct item lengths are $result") //sampleEnd } ``` 具有目標的函數可用于過濾、關聯、分組、展平以及其他操作。 有關目標操作的完整列表,請參見 [Kotlin collections reference](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index.html)。 ## 寫操作 對于可變集合,還存在可更改集合狀態的 _寫操作_ 。這些操作包括添加、刪除和更新元素。寫操作在[集合寫操作](collection-write.html)以及 [List 寫操作](list-operations.html#list-寫操作)與 [Map 寫操作](map-operations.html#map-寫操作)的相應部分中列出。 對于某些操作,有成對的函數可以執行相同的操作:一個函數就地應用該操作,另一個函數將結果作為單獨的集合返回。 例如, [`sort()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sort.html) 就地對可變集合進行排序,因此其狀態發生了變化; [`sorted()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sorted.html) 創建一個新集合,該集合包含按排序順序相同的元素。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf("one", "two", "three", "four") val sortedNumbers = numbers.sorted() println(numbers == sortedNumbers) // false numbers.sort() println(numbers == sortedNumbers) // true //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>

                              哎呀哎呀视频在线观看