Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

Try this:

val average = myList.map { it.speed }.average()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading