I have the following map
val mapToTransform = mapOf(1 to 4, 2 to 5, 4 to 4, 6 to 3, 9 to 3)
I want to transform it to the map
val transformedMap = mapOf(3 to listOf(6,9), 4 to listOf(1,4), 5 to listOf(2))
My attempt is the following:
val attempt = mapToTransform.map{ it.value to it.key}.groupBy{it.first}
that gives {4=[(4, 1), (4, 4)], 5=[(5, 2)], 3=[(3, 6), (3, 9)]}
.
Any help?
question from:
https://stackoverflow.com/questions/66050404/how-to-reverse-a-map-and-group-values-in-a-list 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…