I'm sure there is an elegant way to convert this `...
# getting-started
d
I'm sure there is an elegant way to convert this
Copy code
val 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 value
Copy code
catalogues.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)
Copy code
catalogues.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 know
k
_catalogues_._flatMap_ *{* (k, values) *->* values._map_ *{ it* _to_ k *} }*._groupBy_(*{ it*.first *}*, *{ it*.second *}*)
d
that will produce
Map<Long,List<String>>
instead of
Map<Long, String>
k
Ah that's easier:
_reversed_ = _catalogues_._flatMap_ *{* (k, values) *->* values._map_ *{ it* _to_ k *} }*._toMap_()
d
values.map { it to k }
would do exactly the same as
it.associateWith { k }
k
No? Map returns a list while associate returns a map.
d
that's why i said "i always thought" 😉
k
Just blame slack!