Vinicius Araujo
10/29/2019, 3:01 PMkarelpeeters
10/29/2019, 7:31 PMSizedIterable
or an inner join?Vinicius Araujo
10/29/2019, 8:30 PMfun main() {
val a = Person("jhon", "arm")
val b = Person("jhon", "shoulder")
val c = Person("marie", "leg")
val list = listOf(a, b, c) //this is what I initially have
val groupedLis = list.groupBy {
it.name
}
val aggregation = mutableListOf<UniquePerson>() // this is what I want
groupedLis.forEach { t, u ->
val tattos = mutableListOf<String>()
u.forEach {
tattos.add(it.tatto)
}
val uniquePerson = UniquePerson(t, tattos)
aggregation.add(uniquePerson)
}
}
data class Person(val name: String, val tatto: String)
data class UniquePerson(val name: String, val tattos: List<String>)
karelpeeters
10/29/2019, 8:36 PMval aggregation = list.groupBy { it.name }.map { (name, persons) -> UniquePerson(name, persons.map { it.tatto }) }
karelpeeters
10/29/2019, 8:37 PMVinicius Araujo
10/29/2019, 9:02 PMkarelpeeters
10/29/2019, 9:03 PMkarelpeeters
10/29/2019, 9:05 PMgroupBy
simlar to sequences but for maps: groupingBy
which returns a Grouping
. It doesn't have a map
function defined on it though, because you need the intermediate collection anyway.