Aggregate operation is an operation that is performed on a data structure, such as an array, as a whole rather than performed on an individual element.
Functions that used aggregate operations are as follows:
- count() function is used to returns the number of elements.
- sum() function is used to returns the sum of elements in the collection of numbers.
- min() functions are used to return the smallest element.
- max() functions are used to return the largest element.
- average() function is used to returns the average value of elements in the collection of numbers.
- maxBy() function returns a Collector that produces the maximal element according to a given .
- minBy() function returns a Collector that produces the minimal element according to a given .
- maxWith() function take a Comparator object and generally return the largest element.
- minWith() function take a Comparator object and generally return the smallest element.
- sumBy() performs an efficient and optionally weighted by-group summation by using linear algebra and the Matrix package capabilities..
- sumByDouble() Returns the sum of all values produced by selector function applied to each element in the array..
Kotlin program of using some of aggregate functions –
Count the number of element in the list: 8 Sum of the elements in the list: 201 Min value in the list: 4 Max value in the list: 78 Average of all elements in the list: 25.125
Kotlin program of using sumBy() and sumByDouble() functions-
Multiply each element with 3 and sum: 603 100.5
Kotlin program of using minBy() and maxWith() functions –
Minimum remainder returned by: 12 String with max length is: Football
Fold and Reduce Functions
- fold() function: It takes an associative binary operator function as parameter and will use it to collapse elements from the collection. .
- foldRight() function :It work in a way similar to fold(). The order for traversing the elements in the collection is from right to left.
- foldIndexed() function : It is used to get access of the current index while iterating.
- foldRightIndexed() function: It work in a way similar to foldIndexed(). The order for traversing the elements in the collection is from right to left
- reduce() function reduces the array to a single value. It executes a provided function for each value of the array.
- reduceRight() function :It work in a way similar to reduce(). The order for removing of the elements in the collection is from right to left.
- reduceIndexed() function accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element with its index in the given collection.
- reduceRightIndexed() function: It work in a way similar to reduceIndexed(). The order in the collection is from right to left.
Kotlin program of using fold() and reduce() functions –
The sum of all elements in list 203 Multiply each element with 3 and sum 609
Kotlin program of using foldRight() function –
The sum of the numbers from the right side: 615
Kotlin program of using foldIndexed() and foldRightIndexed() –
Sum of even numbers: 126 Sum of even numbers from Right side: 126
- Kotlin Collection Write operations
- Kotlin - Collection Operations Overview
- RadioButton in Kotlin
- Kotlin Inheritance
- Kotlin mutableListOf()
- Kotlin mutableMapOf()
- Kotlin generics
- Kotlin hashMapOf()
- Kotlin annotations
- TextView in Kotlin
- ProgressBar in Kotlin
- Switch in Kotlin
- TimePicker in Kotlin
- TextClock in Kotlin
- DatePicker in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.