Terje Andersen
11/11/2019, 8:25 AMList<Map<String, List<T>>>
to Map<String, List<T>>
. If the original list of maps contain two maps containing the same String key, the lists should be merged / concatenated. I finding it hard to achieve this, could anyone nudge me in the right direction?Alowaniak
11/11/2019, 8:36 AMa
as mutableMap and then forEach
entry in map b
merge
it onto map a
.
https://pl.kotl.in/0ZLLykh1nmolikuner
11/11/2019, 8:56 AMfun <T> List<Map<String, List<T>>>.flatten(): Map<String, List<T>> {
return flatMap { it.entries } // List<Map.Entry<String, List<T>>
.groupBy { it.key } // Map<String, List<Map.Entry<String, List<T>>>
.mapValues { (_, value: List<Map.Entry<String, List<T>>>) ->
value.flatMap { it.value } // List<T>
}
}
Feel free to remove some type annotations/commentsTerje Andersen
11/11/2019, 9:07 AM