df
12/05/2019, 5:13 PMval catalogues = mapOf(
"de-catalogue" to listOf(29L, 10010L, 10012L, 10013L),
"fr-catalogue" to listOf(10007L, 10019L, 10020L),
"nl-catalogue" to listOf(10008L, 10023L, 10024L),
"it-catalogue" to listOf(10021L),
"es-catalogue" to listOf(10022L)
into a Map<Long,String> where each list value becomes the key and the key (from the original map) becomes the valuedf
12/05/2019, 5:38 PMcatalogues.map { entry ->
entry.value.associateWith { entry.key }
}
gives me a List<Map<Long,String>> but obviously I don't want to have a list of maps but only one map with all keys / values (bearbeitet)df
12/05/2019, 5:38 PMcatalogues.flatMap { entry ->
entry.value.associateWith { entry.key }.asIterable()
}
.associate { it.key to it.value }
produces the expected result... but if someone has a cleaner approach, feel free to let me knowkarelpeeters
12/05/2019, 5:38 PM_catalogues_._flatMap_ *{* (k, values) *->* values._map_ *{ it* _to_ k *} }*._groupBy_(*{ it*.first *}*, *{ it*.second *}*)
df
12/05/2019, 5:41 PMMap<Long,List<String>>
instead of Map<Long, String>
karelpeeters
12/05/2019, 5:42 PM_reversed_ = _catalogues_._flatMap_ *{* (k, values) *->* values._map_ *{ it* _to_ k *} }*._toMap_()
df
12/05/2019, 5:44 PMvalues.map { it to k }
would do exactly the same as it.associateWith { k }
karelpeeters
12/05/2019, 5:45 PMdf
12/05/2019, 5:45 PMkarelpeeters
12/05/2019, 5:46 PM