Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get average of the specific value from list in Kotlin? (same as maxOf)

Tags:

kotlin

I have list of Location. Something like this:

[{"latitude": "45.42123", "longitude": "44.32132", "speed": 4.3}, {"latitude": "46.212", "longitude": "45.4334", "speed": 5.2}]

We have extensions in Kotlin so i can easily get max value of speed like this:

myList.maxOf { it.speed }

But how can i get average value of speed?

P.S. i found the same question in Java. I need solution in Kotlin.

like image 815
SoulReaver313 Avatar asked Sep 02 '25 03:09

SoulReaver313


1 Answers

Try this:

val average = myList.map { it.speed }.average()
like image 148
Arpit Shukla Avatar answered Sep 04 '25 20:09

Arpit Shukla