# Tranforming Structure of Collections

**URL:** <https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090>\
**Category:** Support\
**Created:** [March 25, 2019, 12:07pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090 "2019-03-25T12:07:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sombrastudios](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@sombrastudios](https://discuss.kotlinlang.org/u/sombrastudios)\
**Post date:** [March 25, 2019, 12:07pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/1 "2019-03-25T12:07:07Z")

</div>

Hello there and thank you for clicking here.

I want to tranform my Data Structure in a functional way.  
I start out having a `List<Map<String, T>>` and I need a `Map<String, List<T>>`

Background:  
The Entries of my List often share the same Key, and I want to accumulate all Entries with the Same Key in a List. That List should still be mapped by the key.

I couldn’t find an elegant solution, this is how I solved it for now:

```kotlin
            val splitted: List<Map, String, T> = ...
            // Map from ID to List of Entities
            val entities: MutableMap<String, MutableList<T>> = mapOf()

            splitted.forEach { entries ->
                entries.forEach { key, value ->
                    entities.getOrPut(key) { mutableListOf() }.add(value)
                }
            }

```

Hope you can help me out a little

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 25, 2019, 1:11pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/2 "2019-03-25T13:11:38Z")

</div>

Something like this would work I think

```nohighlight
val splitted: List<Map<String, T>> = ...
splitted.flatMap{ it.entries }.groupBy{ it.key }

```

---

<div class="post-metadata">

**Author:** ![sombrastudios](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@sombrastudios](https://discuss.kotlinlang.org/u/sombrastudios)\
**Post date:** [March 25, 2019, 1:16pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/3 "2019-03-25T13:16:31Z")

</div>

thank you, I’ll test it out. I don’t use flatMap very often, for I often fail to see its potential.  
This seems to be a really helpful way of using it

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [March 25, 2019, 1:17pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/4 "2019-03-25T13:17:10Z")

</div>

Actually, this is what you want:

```
splitted.flatMap {
    it.entries
}.groupBy(
    keySelector = { it.key },
    valueTransform = { it.value }
)

```

@Wasabi375 I was there 5 minutes ago, too. But I took the time to check correctness 😛

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 25, 2019, 1:20pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/5 "2019-03-25T13:20:43Z")

</div>

> [@fatjoe79](#):
>
> I was there 5 minutes ago, too. But I took the time to check correctness 😛

Yeah, maybe I should have done that as well 😉 I normally do, but I did not have an IDE open atm and that’s the easiest way of checking your types are correct. And let’s be honest, waiting for an IDE to start just to answer a simple question like that **normally** is not worth the time. Should have done it though.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [March 25, 2019, 1:22pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/6 "2019-03-25T13:22:49Z")

</div>

Your IDE is not always open? 😱

Just kidding, indeed I was already working in the IDE. I wouldn’t have bothered opening it otherwise.

---

<div class="post-metadata">

**Author:** ![sombrastudios](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@sombrastudios](https://discuss.kotlinlang.org/u/sombrastudios)\
**Post date:** [March 25, 2019, 1:24pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/7 "2019-03-25T13:24:23Z")

</div>

@fatjoe79 @Wasabi375 I’m buffled at how much effort you two put into this, thank you so much!  
I’m not even done checking and understanding the code completly 😄

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [March 25, 2019, 1:25pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/8 "2019-03-25T13:25:43Z")

</div>

Let the IDE show you the type of each (sub)expression and identifier in that code. It is very hard to understand without the help of IDE, especially when you are new to Kotlin.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 25, 2019, 1:26pm UTC](https://discuss.kotlinlang.org/t/tranforming-structure-of-collections/12090/9 "2019-03-25T13:26:54Z")

</div>

Actually, it’s not that much effort. You just write the code snippet in the IDE and then copy it here. And after you spent enough time with kotlin you know the standard library well enough so that you can answer questions like this easily.

As for why I spent time here at all. When I learned to program I had a lot of help from a similar forum like this so I just think it’s fair giving back to the community. And every once in a while there are some interesting discussions here about language design.
