Aggregate operations
Kotlin collections contain functions for commonly used aggregate operations – operations that return a single value based on the collection content. Most of them are well known and work the same way as they do in other languages:
minOrNull()
andmaxOrNull()
return the smallest and the largest element respectively. On empty collections, they returnnull
.average()
returns the average value of elements in the collection of numbers.sum()
returns the sum of elements in the collection of numbers.count()
returns the number of elements in a collection.
There are also functions for retrieving the smallest and the largest elements by certain selector function or custom Comparator
:
maxByOrNull()
andminByOrNull()
take a selector function and return the element for which it returns the largest or the smallest value.maxWithOrNull()
andminWithOrNull()
take aComparator
object and return the largest or smallest element according to thatComparator
.maxOfOrNull()
andminOfOrNull()
take a selector function and return the largest or the smallest return value of the selector itself.maxOfWithOrNull()
andminOfWithOrNull()
take aComparator
object and return the largest or smallest selector return value according to thatComparator
.
These functions return null
on empty collections. There are also alternatives – maxOf
, minOf
, maxOfWith
, and minOfWith
– which do the same as their counterparts but throw a NoSuchElementException
on empty collections.
Besides regular sum()
, there is an advanced summation function sumOf()
that takes a selector function and returns the sum of its application to all collection elements. Selector can return different numeric types: Int
, Long
, Double
, UInt
, and ULong
(also BigInteger
and BigDecimal
on the JVM).
Fold and reduce
For more specific cases, there are the functions reduce()
and fold()
that apply the provided operation to the collection elements sequentially and return the accumulated result. The operation takes two arguments: the previously accumulated value and the collection element.
The difference between the two functions is that fold()
takes an initial value and uses it as the accumulated value on the first step, whereas the first step of reduce()
uses the first and the second elements as operation arguments on the first step.
The example above shows the difference: fold()
is used for calculating the sum of doubled elements. If you pass the same function to reduce()
, it will return another result because it uses the list's first and second elements as arguments on the first step, so the first element won't be doubled.
To apply a function to elements in the reverse order, use functions reduceRight()
and foldRight()
. They work in a way similar to fold()
and reduce()
but start from the last element and then continue to previous. Note that when folding or reducing right, the operation arguments change their order: first goes the element, and then the accumulated value.
You can also apply operations that take element indices as parameters. For this purpose, use functions reduceIndexed()
and foldIndexed()
passing element index as the first argument of the operation.
Finally, there are functions that apply such operations to collection elements from right to left - reduceRightIndexed()
and foldRightIndexed()
.
All reduce operations throw an exception on empty collections. To receive null
instead, use their *OrNull()
counterparts:
For cases where you want to save intermediate accumulator values, there are functions runningFold()
(or its synonym scan()
) and runningReduce()
.
If you need an index in the operation parameter, use runningFoldIndexed()
or runningReduceIndexed()
.