This article explores different ways to find the average of all items in a List in Kotlin.

1. Using average() function

The recommended solution to find an average value of elements in the collection is using the Iterable<T>.average() function. It can be called upon Int, Long, Double, Float, Byte, and Short data types. The following code example shows invocation for this function:

Download Code

2. Using SummaryStatistics

If you don’t mind using the Java Stream API, you can get the arithmetic mean of the elements of the primitive stream using the summaryStatistics() function. It can be used to get other stats about the elements of this stream like min, max, sum, etc. The following example demonstrates this:

Download Code

3. Using Loop

Finally, you can write our custom routine for this trivial task using a loop:

Download Code

That’s all about finding the average of all items in a List in Kotlin.