How to reverse a Map in Kotlin?
If you need to reverse a multimap like m: Map<K, List<V>>
to a Map<V, List<K>>
you can do
m
.flatMap { it.value.map { oneValue -> oneValue to it.key } }
.groupBy({ it.first }, { it.second })
.toMap()
In sequence,
mapOf('a' to listOf('b', 'c'), 'd' to listOf('b'))
gets flat mapped to a sequence likelistOf('b' to 'a', 'c' to 'a', 'b' to 'd')
which gets grouped tolistOf('b' to listOf('a', 'd'), 'c' to listOf('a'))
which then gets converted to a map.
This probably creates intermediate objects.
Here is a simple extension function that reverse a map - without generating unneeded garbage (like pairs, intermediate data structures and unnecessary closures )
fun <K, V> Map<K, V>.reversed() = HashMap<V, K>().also { newMap ->
entries.forEach { newMap.put(it.value, it.key) }
}
note that apply
is inlined, and entries.forEach
is also inlined (which is not the same for Map::forEach
)
Since the Map
consists of Entry
s and it is not Iterable
you can use Map#entries instead. It will be mapped to Map#entrySet to create a backed view of Set<Entry>
, for example:
val reversed = map.entries.associateBy({ it.value }) { it.key }
OR use Iterable#associate, which will create additional Pair
s.
val reversed = map.entries.associate{(k,v)-> v to k}
OR using Map#forEach:
val reversed = mutableMapOf<Int, String>().also {
// v-- use `forEach` here
map.forEach { (k, v) -> it.put(v, k) }
}.toMap()
// ^--- you can add `toMap()` to create an immutable Map.
In case your map is not a 1-1 mapping and you want the inversion to be a list of values:
mapOf(1 to "AAA", 2 to "BBB", 3 to "BBB").toList()
.groupBy { pair -> pair.second } // Pair<Int, String>
.mapValues { entry ->
entry.value.map { it.first } // Entry<String, List<Pair<Int, String>>
}