Home > Software engineering >  Group and calculate values in list
Group and calculate values in list

Time:04-29

I have a list of classes (Kotlin)

class ChallengeRecord(
val sc: String,
val participant: String,
val hr: String,
val point: Int) 

how can I group by participant and sum points by them?

I want to found kotlin analogue for java

Collectors.groupingBy() or Collectors.toMap()

CodePudding user response:

.groupingBy { it.participant }
.fold(0){accumulator, element -> accumulator   element.point }

CodePudding user response:

.groupBy { it.participant }
.mapValues { (_, challengeRecords) -> challengeRecords.sumOf { it.point } }
  • Related