christoph.pickl
02/10/2017, 3:04 PMassociateBy { }
which creates a list for all values with the same key... came up with this (any opinions/ideas on how to even improve?):
fun <K, V> List<V>.associateByList(transform: (V) -> K): Map<K, List<V>> {
val map = mutableMapOf<K, MutableList<V>>()
this.forEach { value ->
val key = transform(value)
if (!map.containsKey(key)) {
map.put(key, mutableListOf())
}
map[key]!! += value
}
return map
}