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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 集合聚合操作 [TOC] Kotlin 集合包含用于常用的 _聚合操作_ (基于集合內容返回單個值的操作)的函數 。其中大多數是眾所周知的,并且其工作方式與在其他語言中相同。 * [`min()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/min.html) 與 [`max()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/max.html) 分別返回最小和最大的元素; * [`average()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/average.html) 返回數字集合中元素的平均值; * [`sum()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sum.html) 返回數字集合中元素的總和; * [`count()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html) 返回集合中元素的數量; ```kotlin fun main() { //sampleStart val numbers = listOf(6, 42, 10, 4) println("Count: ${numbers.count()}") println("Max: ${numbers.max()}") println("Min: ${numbers.min()}") println("Average: ${numbers.average()}") println("Sum: ${numbers.sum()}") //sampleEnd } ``` 還有一些通過某些選擇器函數或自定義 [`Comparator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-comparator/index.html) 來檢索最小和最大元素的函數。 * [`maxBy()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/max-by.html)/[`minBy()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/min-by.html) 接受一個選擇器函數并返回使選擇器返回最大或最小值的元素。 * [`maxWith()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/max-with.html)/[`minWith()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/min-with.html) 接受一個 `Comparator` 對象并且根據此 `Comparator` 對象返回最大或最小元素。 ```kotlin fun main() { //sampleStart val numbers = listOf(5, 42, 10, 4) val min3Remainder = numbers.minBy { it % 3 } println(min3Remainder) val strings = listOf("one", "two", "three", "four") val longestString = strings.maxWith(compareBy { it.length }) println(longestString) //sampleEnd } ``` 此外,有一些高級的求和函數,它們接受一個函數并返回對所有元素調用此函數的返回值的總和: * [`sumBy()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sum-by.html) 使用對集合元素調用返回 `Int` 值的函數。 * [`sumByDouble()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sum-by-double.html) 與返回 `Double` 的函數一起使用。 ```kotlin fun main() { //sampleStart val numbers = listOf(5, 42, 10, 4) println(numbers.sumBy { it * 2 }) println(numbers.sumByDouble { it.toDouble() / 2 }) //sampleEnd } ``` ## Fold 與 reduce 對于更特定的情況,有函數 [`reduce()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reduce.html) 和 [`fold()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.html),它們依次將所提供的操作應用于集合元素并返回累積的結果。操作有兩個參數:先前的累積值和集合元素。 這兩個函數的區別在于:`fold()` 接受一個初始值并將其用作第一步的累積值,而 `reduce()` 的第一步則將第一個和第二個元素作為第一步的操作參數。 ```kotlin fun main() { //sampleStart val numbers = listOf(5, 2, 10, 4) val sum = numbers.reduce { sum, element -> sum + element } println(sum) val sumDoubled = numbers.fold(0) { sum, element -> sum + element * 2 } println(sumDoubled) //val sumDoubledReduce = numbers.reduce { sum, element -> sum + element * 2 } //錯誤:第一個元素在結果中沒有加倍 //println(sumDoubledReduce) //sampleEnd } ``` 上面的實例展示了區別:`fold()` 用于計算加倍的元素之和。如果將相同的函數傳給 `reduce()`,那么它會返回另一個結果,因為在第一步中它將列表的第一個和第二個元素作為參數,所以第一個元素不會被加倍。 如需將函數以相反的順序應用于元素,可以使用函數 [`reduceRight()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reduce-right.html) 和 [`foldRight()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-right.html)。它們的工作方式類似于 `fold()` 和 `reduce()`,但從最后一個元素開始,然后再繼續到前一個元素。記住,在使用 foldRight 或 reduceRight 時,操作參數會更改其順序:第一個參數變為元素,然后第二個參數變為累積值。 ```kotlin fun main() { //sampleStart val numbers = listOf(5, 2, 10, 4) val sumDoubledRight = numbers.foldRight(0) { element, sum -> sum + element * 2 } println(sumDoubledRight) //sampleEnd } ``` 你還可以使用將元素索引作為參數的操作。為此,使用函數 [`reduceIndexed()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reduce-indexed.html) 和 [`foldIndexed()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-indexed.html) 傳遞元素索引作為操作的第一個參數。 最后,還有將這些操作從右到左應用于集合元素的函數——[`reduceRightIndexed()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reduce-right-indexed.html) 與 [`foldRightIndexed()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-right-indexed.html)。 ```kotlin fun main() { //sampleStart val numbers = listOf(5, 2, 10, 4) val sumEven = numbers.foldIndexed(0) { idx, sum, element -> if (idx % 2 == 0) sum + element else sum } println(sumEven) val sumEvenRight = numbers.foldRightIndexed(0) { idx, element, sum -> if (idx % 2 == 0) sum + element else sum } println(sumEvenRight) //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>

                              哎呀哎呀视频在线观看