Kotlin - from a list of Maps, to a map grouped by key












0















I have a List<Map<Branch,Pair<String, Any>>> that I would like to convert in a single Map<Branch,List<Pair<String, Any>>> .



So if I have an initial list with simply 2 elements :



List



1. branch1 -> Pair(key1,value1)

branch2 -> Pair(key2,value2)


2. branch1 -> Pair(key1a,value1a)


I want to end up with :



Map



branch1 -> Pair(key1,value1)

Pair(key1a,value1a)

branch2 -> Pair(key2,value2)


so a kind of groupBy, using all the values of the keys in the initially nested maps..



I have tried with



list.groupBy{it-> it.keys.first()} 


but obviously it doesn't work, as it uses only the first key. I want the same, but using all keys as individual values.



What is the most idiomatic way of doing this in Kotlin ? I have an ugly looking working version in Java, but I am quite sure Kotlin has a nice way of doing it.. it's just that I am not finding it so far !



Any idea ?



Thanks










share|improve this question



























    0















    I have a List<Map<Branch,Pair<String, Any>>> that I would like to convert in a single Map<Branch,List<Pair<String, Any>>> .



    So if I have an initial list with simply 2 elements :



    List



    1. branch1 -> Pair(key1,value1)

    branch2 -> Pair(key2,value2)


    2. branch1 -> Pair(key1a,value1a)


    I want to end up with :



    Map



    branch1 -> Pair(key1,value1)

    Pair(key1a,value1a)

    branch2 -> Pair(key2,value2)


    so a kind of groupBy, using all the values of the keys in the initially nested maps..



    I have tried with



    list.groupBy{it-> it.keys.first()} 


    but obviously it doesn't work, as it uses only the first key. I want the same, but using all keys as individual values.



    What is the most idiomatic way of doing this in Kotlin ? I have an ugly looking working version in Java, but I am quite sure Kotlin has a nice way of doing it.. it's just that I am not finding it so far !



    Any idea ?



    Thanks










    share|improve this question

























      0












      0








      0








      I have a List<Map<Branch,Pair<String, Any>>> that I would like to convert in a single Map<Branch,List<Pair<String, Any>>> .



      So if I have an initial list with simply 2 elements :



      List



      1. branch1 -> Pair(key1,value1)

      branch2 -> Pair(key2,value2)


      2. branch1 -> Pair(key1a,value1a)


      I want to end up with :



      Map



      branch1 -> Pair(key1,value1)

      Pair(key1a,value1a)

      branch2 -> Pair(key2,value2)


      so a kind of groupBy, using all the values of the keys in the initially nested maps..



      I have tried with



      list.groupBy{it-> it.keys.first()} 


      but obviously it doesn't work, as it uses only the first key. I want the same, but using all keys as individual values.



      What is the most idiomatic way of doing this in Kotlin ? I have an ugly looking working version in Java, but I am quite sure Kotlin has a nice way of doing it.. it's just that I am not finding it so far !



      Any idea ?



      Thanks










      share|improve this question














      I have a List<Map<Branch,Pair<String, Any>>> that I would like to convert in a single Map<Branch,List<Pair<String, Any>>> .



      So if I have an initial list with simply 2 elements :



      List



      1. branch1 -> Pair(key1,value1)

      branch2 -> Pair(key2,value2)


      2. branch1 -> Pair(key1a,value1a)


      I want to end up with :



      Map



      branch1 -> Pair(key1,value1)

      Pair(key1a,value1a)

      branch2 -> Pair(key2,value2)


      so a kind of groupBy, using all the values of the keys in the initially nested maps..



      I have tried with



      list.groupBy{it-> it.keys.first()} 


      but obviously it doesn't work, as it uses only the first key. I want the same, but using all keys as individual values.



      What is the most idiomatic way of doing this in Kotlin ? I have an ugly looking working version in Java, but I am quite sure Kotlin has a nice way of doing it.. it's just that I am not finding it so far !



      Any idea ?



      Thanks







      kotlin






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 14:28









      Vincent FVincent F

      2,0181329




      2,0181329
























          4 Answers
          4






          active

          oldest

          votes


















          2














          The following:



          val result =
          listOfMaps.asSequence()
          .flatMap {
          it.asSequence()
          }.groupBy({ it.key }, { it.value })


          will give you the result of type Map<Branch,List<Pair<String, Any>>> with the contents you requested.






          share|improve this answer
























          • Thanks, it works. FYI I've made the change here : github.com/societe-generale/github-crawler/blob/…

            – Vincent F
            Nov 22 '18 at 16:23













          • well... you could have said, that it actually ends up in a Map<Branch, Map<String, Any>>... that .mapValues could also already be simplified to: .mapValues { it.value.toMap() }

            – Roland
            Nov 22 '18 at 16:43



















          1














          I've managed to write this.



          data class Branch(val name: String)
          data class Key(val name: String)
          data class Value(val name: String)


          val sharedBranch = Branch("1")
          val listOfMaps: List<Map<Branch, Pair<Key, Value>>> = listOf(
          mapOf(sharedBranch to Pair(Key("1"), Value("1")),
          Branch("2") to Pair(Key("2"), Value("2"))),
          mapOf(sharedBranch to Pair(Key("1a"), Value("1a")))
          )

          val mapValues: Map<Branch, List<Pair<Key, Value>>> = listOfMaps.asSequence()
          .flatMap { map -> map.entries.asSequence() }
          .groupBy(Map.Entry<Branch, Pair<Key, Value>>::key)
          .mapValues { it.value.map(Map.Entry<Branch, Pair<Key, Value>>::value) }

          println(mapValues)


          Is it appliable for your needs?






          share|improve this answer
























          • what do you make of marko's comment?

            – Tim Castelijns
            Nov 22 '18 at 15:06











          • @TimCastelijns, Kotlin's Map and MutableMap interfaces do not contain this method

            – Andrey Ilyunin
            Nov 22 '18 at 15:44











          • On the JVM, this is legal Kotlin: val map = mutableMapOf(1 to "a", 2 to "b"); map.merge(1, "c") { v0, v1 -> v0 + v1}

            – Marko Topolnik
            Nov 22 '18 at 16:07











          • @TimCastelijns I tried to write it, but merge is not a good fit, you can do it with compute. It's not a great win over groupBy solutions, though. val result = mutableMapOf<Branch, MutableList<Pair<String, Any>>>(); listOfMaps.forEach { map -> map.forEach { k, v -> result.compute(k) { _, v0 -> v0?.also { it.add(v) } ?: mutableListOf(v) } } }

            – Marko Topolnik
            Nov 22 '18 at 16:08











          • @MarkoTopolnik yes, but it is if you have mutable map returned to you as an argument. otherwise you are forced to create an excessive copy. I didn't write is is illegal, I have wrote that Kotlin's Map interface does not have this method

            – Andrey Ilyunin
            Nov 22 '18 at 16:09



















          0














          Extract it to an extension function



          private fun <K, V> List<Map<K, V>>.group(): Map<K, List<V>> =
          asSequence().flatMap { it.asSequence() }.groupBy({ it.key }, { it.value })


          Use it like so:



          val list = yourListOfMaps
          val grouped = list.group()





          share|improve this answer































            0














            val list: List>> = listOf()
            val map = list.flatMap { it.entries }.groupBy { it.key }.mapValues { entry -> entry.value.map { it.value } }






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433108%2fkotlin-from-a-list-of-maps-to-a-map-grouped-by-key%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              The following:



              val result =
              listOfMaps.asSequence()
              .flatMap {
              it.asSequence()
              }.groupBy({ it.key }, { it.value })


              will give you the result of type Map<Branch,List<Pair<String, Any>>> with the contents you requested.






              share|improve this answer
























              • Thanks, it works. FYI I've made the change here : github.com/societe-generale/github-crawler/blob/…

                – Vincent F
                Nov 22 '18 at 16:23













              • well... you could have said, that it actually ends up in a Map<Branch, Map<String, Any>>... that .mapValues could also already be simplified to: .mapValues { it.value.toMap() }

                – Roland
                Nov 22 '18 at 16:43
















              2














              The following:



              val result =
              listOfMaps.asSequence()
              .flatMap {
              it.asSequence()
              }.groupBy({ it.key }, { it.value })


              will give you the result of type Map<Branch,List<Pair<String, Any>>> with the contents you requested.






              share|improve this answer
























              • Thanks, it works. FYI I've made the change here : github.com/societe-generale/github-crawler/blob/…

                – Vincent F
                Nov 22 '18 at 16:23













              • well... you could have said, that it actually ends up in a Map<Branch, Map<String, Any>>... that .mapValues could also already be simplified to: .mapValues { it.value.toMap() }

                – Roland
                Nov 22 '18 at 16:43














              2












              2








              2







              The following:



              val result =
              listOfMaps.asSequence()
              .flatMap {
              it.asSequence()
              }.groupBy({ it.key }, { it.value })


              will give you the result of type Map<Branch,List<Pair<String, Any>>> with the contents you requested.






              share|improve this answer













              The following:



              val result =
              listOfMaps.asSequence()
              .flatMap {
              it.asSequence()
              }.groupBy({ it.key }, { it.value })


              will give you the result of type Map<Branch,List<Pair<String, Any>>> with the contents you requested.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 '18 at 15:21









              RolandRoland

              10k11241




              10k11241













              • Thanks, it works. FYI I've made the change here : github.com/societe-generale/github-crawler/blob/…

                – Vincent F
                Nov 22 '18 at 16:23













              • well... you could have said, that it actually ends up in a Map<Branch, Map<String, Any>>... that .mapValues could also already be simplified to: .mapValues { it.value.toMap() }

                – Roland
                Nov 22 '18 at 16:43



















              • Thanks, it works. FYI I've made the change here : github.com/societe-generale/github-crawler/blob/…

                – Vincent F
                Nov 22 '18 at 16:23













              • well... you could have said, that it actually ends up in a Map<Branch, Map<String, Any>>... that .mapValues could also already be simplified to: .mapValues { it.value.toMap() }

                – Roland
                Nov 22 '18 at 16:43

















              Thanks, it works. FYI I've made the change here : github.com/societe-generale/github-crawler/blob/…

              – Vincent F
              Nov 22 '18 at 16:23







              Thanks, it works. FYI I've made the change here : github.com/societe-generale/github-crawler/blob/…

              – Vincent F
              Nov 22 '18 at 16:23















              well... you could have said, that it actually ends up in a Map<Branch, Map<String, Any>>... that .mapValues could also already be simplified to: .mapValues { it.value.toMap() }

              – Roland
              Nov 22 '18 at 16:43





              well... you could have said, that it actually ends up in a Map<Branch, Map<String, Any>>... that .mapValues could also already be simplified to: .mapValues { it.value.toMap() }

              – Roland
              Nov 22 '18 at 16:43













              1














              I've managed to write this.



              data class Branch(val name: String)
              data class Key(val name: String)
              data class Value(val name: String)


              val sharedBranch = Branch("1")
              val listOfMaps: List<Map<Branch, Pair<Key, Value>>> = listOf(
              mapOf(sharedBranch to Pair(Key("1"), Value("1")),
              Branch("2") to Pair(Key("2"), Value("2"))),
              mapOf(sharedBranch to Pair(Key("1a"), Value("1a")))
              )

              val mapValues: Map<Branch, List<Pair<Key, Value>>> = listOfMaps.asSequence()
              .flatMap { map -> map.entries.asSequence() }
              .groupBy(Map.Entry<Branch, Pair<Key, Value>>::key)
              .mapValues { it.value.map(Map.Entry<Branch, Pair<Key, Value>>::value) }

              println(mapValues)


              Is it appliable for your needs?






              share|improve this answer
























              • what do you make of marko's comment?

                – Tim Castelijns
                Nov 22 '18 at 15:06











              • @TimCastelijns, Kotlin's Map and MutableMap interfaces do not contain this method

                – Andrey Ilyunin
                Nov 22 '18 at 15:44











              • On the JVM, this is legal Kotlin: val map = mutableMapOf(1 to "a", 2 to "b"); map.merge(1, "c") { v0, v1 -> v0 + v1}

                – Marko Topolnik
                Nov 22 '18 at 16:07











              • @TimCastelijns I tried to write it, but merge is not a good fit, you can do it with compute. It's not a great win over groupBy solutions, though. val result = mutableMapOf<Branch, MutableList<Pair<String, Any>>>(); listOfMaps.forEach { map -> map.forEach { k, v -> result.compute(k) { _, v0 -> v0?.also { it.add(v) } ?: mutableListOf(v) } } }

                – Marko Topolnik
                Nov 22 '18 at 16:08











              • @MarkoTopolnik yes, but it is if you have mutable map returned to you as an argument. otherwise you are forced to create an excessive copy. I didn't write is is illegal, I have wrote that Kotlin's Map interface does not have this method

                – Andrey Ilyunin
                Nov 22 '18 at 16:09
















              1














              I've managed to write this.



              data class Branch(val name: String)
              data class Key(val name: String)
              data class Value(val name: String)


              val sharedBranch = Branch("1")
              val listOfMaps: List<Map<Branch, Pair<Key, Value>>> = listOf(
              mapOf(sharedBranch to Pair(Key("1"), Value("1")),
              Branch("2") to Pair(Key("2"), Value("2"))),
              mapOf(sharedBranch to Pair(Key("1a"), Value("1a")))
              )

              val mapValues: Map<Branch, List<Pair<Key, Value>>> = listOfMaps.asSequence()
              .flatMap { map -> map.entries.asSequence() }
              .groupBy(Map.Entry<Branch, Pair<Key, Value>>::key)
              .mapValues { it.value.map(Map.Entry<Branch, Pair<Key, Value>>::value) }

              println(mapValues)


              Is it appliable for your needs?






              share|improve this answer
























              • what do you make of marko's comment?

                – Tim Castelijns
                Nov 22 '18 at 15:06











              • @TimCastelijns, Kotlin's Map and MutableMap interfaces do not contain this method

                – Andrey Ilyunin
                Nov 22 '18 at 15:44











              • On the JVM, this is legal Kotlin: val map = mutableMapOf(1 to "a", 2 to "b"); map.merge(1, "c") { v0, v1 -> v0 + v1}

                – Marko Topolnik
                Nov 22 '18 at 16:07











              • @TimCastelijns I tried to write it, but merge is not a good fit, you can do it with compute. It's not a great win over groupBy solutions, though. val result = mutableMapOf<Branch, MutableList<Pair<String, Any>>>(); listOfMaps.forEach { map -> map.forEach { k, v -> result.compute(k) { _, v0 -> v0?.also { it.add(v) } ?: mutableListOf(v) } } }

                – Marko Topolnik
                Nov 22 '18 at 16:08











              • @MarkoTopolnik yes, but it is if you have mutable map returned to you as an argument. otherwise you are forced to create an excessive copy. I didn't write is is illegal, I have wrote that Kotlin's Map interface does not have this method

                – Andrey Ilyunin
                Nov 22 '18 at 16:09














              1












              1








              1







              I've managed to write this.



              data class Branch(val name: String)
              data class Key(val name: String)
              data class Value(val name: String)


              val sharedBranch = Branch("1")
              val listOfMaps: List<Map<Branch, Pair<Key, Value>>> = listOf(
              mapOf(sharedBranch to Pair(Key("1"), Value("1")),
              Branch("2") to Pair(Key("2"), Value("2"))),
              mapOf(sharedBranch to Pair(Key("1a"), Value("1a")))
              )

              val mapValues: Map<Branch, List<Pair<Key, Value>>> = listOfMaps.asSequence()
              .flatMap { map -> map.entries.asSequence() }
              .groupBy(Map.Entry<Branch, Pair<Key, Value>>::key)
              .mapValues { it.value.map(Map.Entry<Branch, Pair<Key, Value>>::value) }

              println(mapValues)


              Is it appliable for your needs?






              share|improve this answer













              I've managed to write this.



              data class Branch(val name: String)
              data class Key(val name: String)
              data class Value(val name: String)


              val sharedBranch = Branch("1")
              val listOfMaps: List<Map<Branch, Pair<Key, Value>>> = listOf(
              mapOf(sharedBranch to Pair(Key("1"), Value("1")),
              Branch("2") to Pair(Key("2"), Value("2"))),
              mapOf(sharedBranch to Pair(Key("1a"), Value("1a")))
              )

              val mapValues: Map<Branch, List<Pair<Key, Value>>> = listOfMaps.asSequence()
              .flatMap { map -> map.entries.asSequence() }
              .groupBy(Map.Entry<Branch, Pair<Key, Value>>::key)
              .mapValues { it.value.map(Map.Entry<Branch, Pair<Key, Value>>::value) }

              println(mapValues)


              Is it appliable for your needs?







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 '18 at 14:52









              Andrey IlyuninAndrey Ilyunin

              1,286220




              1,286220













              • what do you make of marko's comment?

                – Tim Castelijns
                Nov 22 '18 at 15:06











              • @TimCastelijns, Kotlin's Map and MutableMap interfaces do not contain this method

                – Andrey Ilyunin
                Nov 22 '18 at 15:44











              • On the JVM, this is legal Kotlin: val map = mutableMapOf(1 to "a", 2 to "b"); map.merge(1, "c") { v0, v1 -> v0 + v1}

                – Marko Topolnik
                Nov 22 '18 at 16:07











              • @TimCastelijns I tried to write it, but merge is not a good fit, you can do it with compute. It's not a great win over groupBy solutions, though. val result = mutableMapOf<Branch, MutableList<Pair<String, Any>>>(); listOfMaps.forEach { map -> map.forEach { k, v -> result.compute(k) { _, v0 -> v0?.also { it.add(v) } ?: mutableListOf(v) } } }

                – Marko Topolnik
                Nov 22 '18 at 16:08











              • @MarkoTopolnik yes, but it is if you have mutable map returned to you as an argument. otherwise you are forced to create an excessive copy. I didn't write is is illegal, I have wrote that Kotlin's Map interface does not have this method

                – Andrey Ilyunin
                Nov 22 '18 at 16:09



















              • what do you make of marko's comment?

                – Tim Castelijns
                Nov 22 '18 at 15:06











              • @TimCastelijns, Kotlin's Map and MutableMap interfaces do not contain this method

                – Andrey Ilyunin
                Nov 22 '18 at 15:44











              • On the JVM, this is legal Kotlin: val map = mutableMapOf(1 to "a", 2 to "b"); map.merge(1, "c") { v0, v1 -> v0 + v1}

                – Marko Topolnik
                Nov 22 '18 at 16:07











              • @TimCastelijns I tried to write it, but merge is not a good fit, you can do it with compute. It's not a great win over groupBy solutions, though. val result = mutableMapOf<Branch, MutableList<Pair<String, Any>>>(); listOfMaps.forEach { map -> map.forEach { k, v -> result.compute(k) { _, v0 -> v0?.also { it.add(v) } ?: mutableListOf(v) } } }

                – Marko Topolnik
                Nov 22 '18 at 16:08











              • @MarkoTopolnik yes, but it is if you have mutable map returned to you as an argument. otherwise you are forced to create an excessive copy. I didn't write is is illegal, I have wrote that Kotlin's Map interface does not have this method

                – Andrey Ilyunin
                Nov 22 '18 at 16:09

















              what do you make of marko's comment?

              – Tim Castelijns
              Nov 22 '18 at 15:06





              what do you make of marko's comment?

              – Tim Castelijns
              Nov 22 '18 at 15:06













              @TimCastelijns, Kotlin's Map and MutableMap interfaces do not contain this method

              – Andrey Ilyunin
              Nov 22 '18 at 15:44





              @TimCastelijns, Kotlin's Map and MutableMap interfaces do not contain this method

              – Andrey Ilyunin
              Nov 22 '18 at 15:44













              On the JVM, this is legal Kotlin: val map = mutableMapOf(1 to "a", 2 to "b"); map.merge(1, "c") { v0, v1 -> v0 + v1}

              – Marko Topolnik
              Nov 22 '18 at 16:07





              On the JVM, this is legal Kotlin: val map = mutableMapOf(1 to "a", 2 to "b"); map.merge(1, "c") { v0, v1 -> v0 + v1}

              – Marko Topolnik
              Nov 22 '18 at 16:07













              @TimCastelijns I tried to write it, but merge is not a good fit, you can do it with compute. It's not a great win over groupBy solutions, though. val result = mutableMapOf<Branch, MutableList<Pair<String, Any>>>(); listOfMaps.forEach { map -> map.forEach { k, v -> result.compute(k) { _, v0 -> v0?.also { it.add(v) } ?: mutableListOf(v) } } }

              – Marko Topolnik
              Nov 22 '18 at 16:08





              @TimCastelijns I tried to write it, but merge is not a good fit, you can do it with compute. It's not a great win over groupBy solutions, though. val result = mutableMapOf<Branch, MutableList<Pair<String, Any>>>(); listOfMaps.forEach { map -> map.forEach { k, v -> result.compute(k) { _, v0 -> v0?.also { it.add(v) } ?: mutableListOf(v) } } }

              – Marko Topolnik
              Nov 22 '18 at 16:08













              @MarkoTopolnik yes, but it is if you have mutable map returned to you as an argument. otherwise you are forced to create an excessive copy. I didn't write is is illegal, I have wrote that Kotlin's Map interface does not have this method

              – Andrey Ilyunin
              Nov 22 '18 at 16:09





              @MarkoTopolnik yes, but it is if you have mutable map returned to you as an argument. otherwise you are forced to create an excessive copy. I didn't write is is illegal, I have wrote that Kotlin's Map interface does not have this method

              – Andrey Ilyunin
              Nov 22 '18 at 16:09











              0














              Extract it to an extension function



              private fun <K, V> List<Map<K, V>>.group(): Map<K, List<V>> =
              asSequence().flatMap { it.asSequence() }.groupBy({ it.key }, { it.value })


              Use it like so:



              val list = yourListOfMaps
              val grouped = list.group()





              share|improve this answer




























                0














                Extract it to an extension function



                private fun <K, V> List<Map<K, V>>.group(): Map<K, List<V>> =
                asSequence().flatMap { it.asSequence() }.groupBy({ it.key }, { it.value })


                Use it like so:



                val list = yourListOfMaps
                val grouped = list.group()





                share|improve this answer


























                  0












                  0








                  0







                  Extract it to an extension function



                  private fun <K, V> List<Map<K, V>>.group(): Map<K, List<V>> =
                  asSequence().flatMap { it.asSequence() }.groupBy({ it.key }, { it.value })


                  Use it like so:



                  val list = yourListOfMaps
                  val grouped = list.group()





                  share|improve this answer













                  Extract it to an extension function



                  private fun <K, V> List<Map<K, V>>.group(): Map<K, List<V>> =
                  asSequence().flatMap { it.asSequence() }.groupBy({ it.key }, { it.value })


                  Use it like so:



                  val list = yourListOfMaps
                  val grouped = list.group()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 16:08









                  Frank NeblungFrank Neblung

                  956417




                  956417























                      0














                      val list: List>> = listOf()
                      val map = list.flatMap { it.entries }.groupBy { it.key }.mapValues { entry -> entry.value.map { it.value } }






                      share|improve this answer




























                        0














                        val list: List>> = listOf()
                        val map = list.flatMap { it.entries }.groupBy { it.key }.mapValues { entry -> entry.value.map { it.value } }






                        share|improve this answer


























                          0












                          0








                          0







                          val list: List>> = listOf()
                          val map = list.flatMap { it.entries }.groupBy { it.key }.mapValues { entry -> entry.value.map { it.value } }






                          share|improve this answer













                          val list: List>> = listOf()
                          val map = list.flatMap { it.entries }.groupBy { it.key }.mapValues { entry -> entry.value.map { it.value } }







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 23 '18 at 8:27









                          Constantin ChernishovConstantin Chernishov

                          20417




                          20417






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433108%2fkotlin-from-a-list-of-maps-to-a-map-grouped-by-key%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Costa Masnaga

                              Fotorealismo

                              Sidney Franklin