https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

Shumilin Alexandr

04/29/2022, 8:01 PM
Hi everyone! please help me, how can i get first 5 elements from from Map<String, Double> ?
j

Joffrey

04/29/2022, 8:02 PM
Maps don't have order. What do you mean by the first 5 elements?
☝🏼 3
☝🏾 1
☝️ 2
s

Shumilin Alexandr

04/29/2022, 8:03 PM
Copy code
csvService.read(fileList)
.groupBy { it.participant }
.mapValues { (_, challengeRecords) -> challengeRecords.sumOf { it.point } }
.toList()
.sortedByDescending { (k, v) -> v }
.toMap()
i have a sroted map by value, and i need take to first 5 elements
p

phldavies

04/29/2022, 8:03 PM
You could use
.take(5)
before the
.toMap()
👌🏼 1
👌 1
👌🏻 1
❤️ 1
s

Shumilin Alexandr

04/29/2022, 8:05 PM
Yes it works! thank you!
m

Matteo Mirk

05/02/2022, 3:00 PM
classic case of https://xyproblem.info/ 😉
2 Views