generating map from list of objects having a map using java lambda8











up vote
8
down vote

favorite












I've an object,



class Object2{
String name;
String id;
Map<String, String> customData;
}

class Object1{
List<Object2> obj1List;
}


I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.










share|improve this question









New contributor




sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    8
    down vote

    favorite












    I've an object,



    class Object2{
    String name;
    String id;
    Map<String, String> customData;
    }

    class Object1{
    List<Object2> obj1List;
    }


    I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.










    share|improve this question









    New contributor




    sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      8
      down vote

      favorite









      up vote
      8
      down vote

      favorite











      I've an object,



      class Object2{
      String name;
      String id;
      Map<String, String> customData;
      }

      class Object1{
      List<Object2> obj1List;
      }


      I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.










      share|improve this question









      New contributor




      sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I've an object,



      class Object2{
      String name;
      String id;
      Map<String, String> customData;
      }

      class Object1{
      List<Object2> obj1List;
      }


      I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.







      java lambda java-8 java-stream






      share|improve this question









      New contributor




      sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 6 hours ago









      Aomine

      34.1k62859




      34.1k62859






      New contributor




      sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 hours ago









      sar

      411




      411




      New contributor




      sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      sar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          down vote













          Here's a way with lambdas and Java 8:



          Map<String, String> map = new LinkedHashMap<>();
          object1List.forEach(o1 ->
          o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));





          share|improve this answer





















          • weird naming convention in the question, but I guess you meant getObject1List()
            – nullpointer
            1 hour ago


















          up vote
          3
          down vote













          Use flatMap and toMap as follows:



          List<Object1> source = ...
          Map<String, String> result =
          source.stream()
          .flatMap(e -> e.getObj1List().stream()
          .flatMap(a -> a.getCustomData().entrySet().stream()))
          .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));


          or if you're dealing with a single Object1 object:



          Object1 myObj = ...
          Map<String, String> result =
          myObj.getObj1List()
          .stream()
          .flatMap(a -> a.getCustomData().entrySet().stream())
          .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));





          share|improve this answer






























            up vote
            3
            down vote













            Alternatively, you can perform Stream.flatMap and then use Map.putAll as



            List<Object1> object1s = new ArrayList<>(); // initialise as you would
            Map<String, String> finalCustomData = new LinkedHashMap<>();
            object1s.stream() // Stream<Object1>
            .flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
            .map(Object2::getCustomData) // Stream<Map<String, String>>
            .forEach(finalCustomData::putAll);





            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',
              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
              });


              }
              });






              sar is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53642421%2fgenerating-map-from-list-of-objects-having-a-map-using-java-lambda8%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote













              Here's a way with lambdas and Java 8:



              Map<String, String> map = new LinkedHashMap<>();
              object1List.forEach(o1 ->
              o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));





              share|improve this answer





















              • weird naming convention in the question, but I guess you meant getObject1List()
                – nullpointer
                1 hour ago















              up vote
              5
              down vote













              Here's a way with lambdas and Java 8:



              Map<String, String> map = new LinkedHashMap<>();
              object1List.forEach(o1 ->
              o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));





              share|improve this answer





















              • weird naming convention in the question, but I guess you meant getObject1List()
                – nullpointer
                1 hour ago













              up vote
              5
              down vote










              up vote
              5
              down vote









              Here's a way with lambdas and Java 8:



              Map<String, String> map = new LinkedHashMap<>();
              object1List.forEach(o1 ->
              o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));





              share|improve this answer












              Here's a way with lambdas and Java 8:



              Map<String, String> map = new LinkedHashMap<>();
              object1List.forEach(o1 ->
              o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 5 hours ago









              Federico Peralta Schaffner

              21.7k33369




              21.7k33369












              • weird naming convention in the question, but I guess you meant getObject1List()
                – nullpointer
                1 hour ago


















              • weird naming convention in the question, but I guess you meant getObject1List()
                – nullpointer
                1 hour ago
















              weird naming convention in the question, but I guess you meant getObject1List()
              – nullpointer
              1 hour ago




              weird naming convention in the question, but I guess you meant getObject1List()
              – nullpointer
              1 hour ago












              up vote
              3
              down vote













              Use flatMap and toMap as follows:



              List<Object1> source = ...
              Map<String, String> result =
              source.stream()
              .flatMap(e -> e.getObj1List().stream()
              .flatMap(a -> a.getCustomData().entrySet().stream()))
              .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));


              or if you're dealing with a single Object1 object:



              Object1 myObj = ...
              Map<String, String> result =
              myObj.getObj1List()
              .stream()
              .flatMap(a -> a.getCustomData().entrySet().stream())
              .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));





              share|improve this answer



























                up vote
                3
                down vote













                Use flatMap and toMap as follows:



                List<Object1> source = ...
                Map<String, String> result =
                source.stream()
                .flatMap(e -> e.getObj1List().stream()
                .flatMap(a -> a.getCustomData().entrySet().stream()))
                .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));


                or if you're dealing with a single Object1 object:



                Object1 myObj = ...
                Map<String, String> result =
                myObj.getObj1List()
                .stream()
                .flatMap(a -> a.getCustomData().entrySet().stream())
                .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));





                share|improve this answer

























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Use flatMap and toMap as follows:



                  List<Object1> source = ...
                  Map<String, String> result =
                  source.stream()
                  .flatMap(e -> e.getObj1List().stream()
                  .flatMap(a -> a.getCustomData().entrySet().stream()))
                  .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));


                  or if you're dealing with a single Object1 object:



                  Object1 myObj = ...
                  Map<String, String> result =
                  myObj.getObj1List()
                  .stream()
                  .flatMap(a -> a.getCustomData().entrySet().stream())
                  .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));





                  share|improve this answer














                  Use flatMap and toMap as follows:



                  List<Object1> source = ...
                  Map<String, String> result =
                  source.stream()
                  .flatMap(e -> e.getObj1List().stream()
                  .flatMap(a -> a.getCustomData().entrySet().stream()))
                  .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));


                  or if you're dealing with a single Object1 object:



                  Object1 myObj = ...
                  Map<String, String> result =
                  myObj.getObj1List()
                  .stream()
                  .flatMap(a -> a.getCustomData().entrySet().stream())
                  .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 6 hours ago

























                  answered 6 hours ago









                  Aomine

                  34.1k62859




                  34.1k62859






















                      up vote
                      3
                      down vote













                      Alternatively, you can perform Stream.flatMap and then use Map.putAll as



                      List<Object1> object1s = new ArrayList<>(); // initialise as you would
                      Map<String, String> finalCustomData = new LinkedHashMap<>();
                      object1s.stream() // Stream<Object1>
                      .flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
                      .map(Object2::getCustomData) // Stream<Map<String, String>>
                      .forEach(finalCustomData::putAll);





                      share|improve this answer

























                        up vote
                        3
                        down vote













                        Alternatively, you can perform Stream.flatMap and then use Map.putAll as



                        List<Object1> object1s = new ArrayList<>(); // initialise as you would
                        Map<String, String> finalCustomData = new LinkedHashMap<>();
                        object1s.stream() // Stream<Object1>
                        .flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
                        .map(Object2::getCustomData) // Stream<Map<String, String>>
                        .forEach(finalCustomData::putAll);





                        share|improve this answer























                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                          Alternatively, you can perform Stream.flatMap and then use Map.putAll as



                          List<Object1> object1s = new ArrayList<>(); // initialise as you would
                          Map<String, String> finalCustomData = new LinkedHashMap<>();
                          object1s.stream() // Stream<Object1>
                          .flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
                          .map(Object2::getCustomData) // Stream<Map<String, String>>
                          .forEach(finalCustomData::putAll);





                          share|improve this answer












                          Alternatively, you can perform Stream.flatMap and then use Map.putAll as



                          List<Object1> object1s = new ArrayList<>(); // initialise as you would
                          Map<String, String> finalCustomData = new LinkedHashMap<>();
                          object1s.stream() // Stream<Object1>
                          .flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
                          .map(Object2::getCustomData) // Stream<Map<String, String>>
                          .forEach(finalCustomData::putAll);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 1 hour ago









                          nullpointer

                          37.4k1072145




                          37.4k1072145






















                              sar is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              sar is a new contributor. Be nice, and check out our Code of Conduct.













                              sar is a new contributor. Be nice, and check out our Code of Conduct.












                              sar is a new contributor. Be nice, and check out our Code of Conduct.
















                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53642421%2fgenerating-map-from-list-of-objects-having-a-map-using-java-lambda8%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

                              Contact image not getting when fetch all contact list from iPhone by CNContact

                              count number of partitions of a set with n elements into k subsets

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks