<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] Kotlin 標準庫提供用于對集合元素進行分組的擴展函數。基本函數 [`groupBy()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/group-by.html) 使用一個 lambda 函數并返回一個 `Map`。在此 Map 中,每個鍵都是 lambda 結果,而對應的值是返回此結果的元素 `List`。 例如,可以使用此函數將 `String` 列表按首字母分組。 還可以使用第二個 lambda 參數(值轉換函數)調用 `groupBy()`。在帶有兩個 lambda 的 `groupBy()` 結果 Map 中,由 `keySelector` 函數生成的鍵映射到值轉換函數的結果,而不是原始元素。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four", "five") println(numbers.groupBy { it.first().toUpperCase() }) println(numbers.groupBy(keySelector = { it.first() }, valueTransform = { it.toUpperCase() })) //sampleEnd } ``` 如果要對元素進行分組,然后一次將操作應用于所有分組,請使用 [`groupingBy()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/grouping-by.html) 函數。它返回一個 [`Grouping`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-grouping/index.html) 類型的實例。通過 `Grouping` 實例,可以以一種惰性的方式將操作應用于所有組:這些分組實際上是剛好在執行操作前構建的。 即,`Grouping` 支持以下操作: * [`eachCount()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/each-count.html) 計算每個組中的元素。 * [`fold()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.html) 與 [`reduce()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reduce.html) 對每個組分別執行 [fold 與 reduce](collection-aggregate.html#fold-與-reduce) 操作,作為一個單獨的集合并返回結果。 * [`aggregate()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/aggregate.html) 隨后將給定操作應用于每個組中的所有元素并返回結果。這是對 `Grouping` 執行任何操作的通用方法。當折疊或縮小不夠時,可使用它來實現自定義操作。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four", "five", "six") println(numbers.groupingBy { it.first() }.eachCount()) //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>

                              哎呀哎呀视频在线观看