Java: groupingBy subvalue as value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












Let's say, I have an object Person with fields of type FirstName and LastName. Now I also have a List<Person> and I like to use streams.



Now I want to generate a Map<FirstName, List<LastName>> in order to group people with the same first name. How do I go about this without writing much code? My approach so far is



personList
.stream()
.collect(Collectors.groupingBy(
Person::getFirstName,
person -> person.getLastName() // this seems to be wrong
));


but it seems this is the wrong way to assign the value of the map. What should I change? Or should I perhaps use .reduce with new HashMap<FirstName, List<LastName>>() as initial value and then aggregate to it by putting elements inside?










share|improve this question



























    up vote
    6
    down vote

    favorite
    1












    Let's say, I have an object Person with fields of type FirstName and LastName. Now I also have a List<Person> and I like to use streams.



    Now I want to generate a Map<FirstName, List<LastName>> in order to group people with the same first name. How do I go about this without writing much code? My approach so far is



    personList
    .stream()
    .collect(Collectors.groupingBy(
    Person::getFirstName,
    person -> person.getLastName() // this seems to be wrong
    ));


    but it seems this is the wrong way to assign the value of the map. What should I change? Or should I perhaps use .reduce with new HashMap<FirstName, List<LastName>>() as initial value and then aggregate to it by putting elements inside?










    share|improve this question

























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      Let's say, I have an object Person with fields of type FirstName and LastName. Now I also have a List<Person> and I like to use streams.



      Now I want to generate a Map<FirstName, List<LastName>> in order to group people with the same first name. How do I go about this without writing much code? My approach so far is



      personList
      .stream()
      .collect(Collectors.groupingBy(
      Person::getFirstName,
      person -> person.getLastName() // this seems to be wrong
      ));


      but it seems this is the wrong way to assign the value of the map. What should I change? Or should I perhaps use .reduce with new HashMap<FirstName, List<LastName>>() as initial value and then aggregate to it by putting elements inside?










      share|improve this question















      Let's say, I have an object Person with fields of type FirstName and LastName. Now I also have a List<Person> and I like to use streams.



      Now I want to generate a Map<FirstName, List<LastName>> in order to group people with the same first name. How do I go about this without writing much code? My approach so far is



      personList
      .stream()
      .collect(Collectors.groupingBy(
      Person::getFirstName,
      person -> person.getLastName() // this seems to be wrong
      ));


      but it seems this is the wrong way to assign the value of the map. What should I change? Or should I perhaps use .reduce with new HashMap<FirstName, List<LastName>>() as initial value and then aggregate to it by putting elements inside?







      java java-stream






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago

























      asked 2 hours ago









      Phil

      1,75811628




      1,75811628






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          personList.stream()
          .collect(Collectors.groupingBy(
          Person::getFirstName,
          Collectors.mapping(Person::getLastName, Collectors.toList())));


          You are looking for a downstream collector with groupingBy






          share|improve this answer





























            up vote
            5
            down vote













            This should work for you :



            Map<String, List<String>> map = personList.stream()
            .collect(Collectors.groupingBy(Person::getFirstName,
            Collectors.mapping(Person::getLastName, Collectors.toList())));





            share|improve this answer



























              up vote
              0
              down vote













              I think you are looking for something like this:



              Map<String, Map<String, List>> map = personList.stream()
              .collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));


              The double grouping gives you a map of a map. That's the trick.






              share|improve this answer
















              • 1




                Thank you, but the value was supposed to be a simple list, not another map.
                – Phil
                2 hours ago






              • 1




                I see it. I voted up nullpointer's response. That's the one.
                – Perimosh
                2 hours ago











              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: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              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%2f52879392%2fjava-groupingby-subvalue-as-value%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote



              accepted










              personList.stream()
              .collect(Collectors.groupingBy(
              Person::getFirstName,
              Collectors.mapping(Person::getLastName, Collectors.toList())));


              You are looking for a downstream collector with groupingBy






              share|improve this answer


























                up vote
                5
                down vote



                accepted










                personList.stream()
                .collect(Collectors.groupingBy(
                Person::getFirstName,
                Collectors.mapping(Person::getLastName, Collectors.toList())));


                You are looking for a downstream collector with groupingBy






                share|improve this answer
























                  up vote
                  5
                  down vote



                  accepted







                  up vote
                  5
                  down vote



                  accepted






                  personList.stream()
                  .collect(Collectors.groupingBy(
                  Person::getFirstName,
                  Collectors.mapping(Person::getLastName, Collectors.toList())));


                  You are looking for a downstream collector with groupingBy






                  share|improve this answer














                  personList.stream()
                  .collect(Collectors.groupingBy(
                  Person::getFirstName,
                  Collectors.mapping(Person::getLastName, Collectors.toList())));


                  You are looking for a downstream collector with groupingBy







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 56 mins ago

























                  answered 2 hours ago









                  Eugene

                  64.4k989152




                  64.4k989152






















                      up vote
                      5
                      down vote













                      This should work for you :



                      Map<String, List<String>> map = personList.stream()
                      .collect(Collectors.groupingBy(Person::getFirstName,
                      Collectors.mapping(Person::getLastName, Collectors.toList())));





                      share|improve this answer
























                        up vote
                        5
                        down vote













                        This should work for you :



                        Map<String, List<String>> map = personList.stream()
                        .collect(Collectors.groupingBy(Person::getFirstName,
                        Collectors.mapping(Person::getLastName, Collectors.toList())));





                        share|improve this answer






















                          up vote
                          5
                          down vote










                          up vote
                          5
                          down vote









                          This should work for you :



                          Map<String, List<String>> map = personList.stream()
                          .collect(Collectors.groupingBy(Person::getFirstName,
                          Collectors.mapping(Person::getLastName, Collectors.toList())));





                          share|improve this answer












                          This should work for you :



                          Map<String, List<String>> map = personList.stream()
                          .collect(Collectors.groupingBy(Person::getFirstName,
                          Collectors.mapping(Person::getLastName, Collectors.toList())));






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 hours ago









                          nullpointer

                          32.1k1064133




                          32.1k1064133




















                              up vote
                              0
                              down vote













                              I think you are looking for something like this:



                              Map<String, Map<String, List>> map = personList.stream()
                              .collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));


                              The double grouping gives you a map of a map. That's the trick.






                              share|improve this answer
















                              • 1




                                Thank you, but the value was supposed to be a simple list, not another map.
                                – Phil
                                2 hours ago






                              • 1




                                I see it. I voted up nullpointer's response. That's the one.
                                – Perimosh
                                2 hours ago















                              up vote
                              0
                              down vote













                              I think you are looking for something like this:



                              Map<String, Map<String, List>> map = personList.stream()
                              .collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));


                              The double grouping gives you a map of a map. That's the trick.






                              share|improve this answer
















                              • 1




                                Thank you, but the value was supposed to be a simple list, not another map.
                                – Phil
                                2 hours ago






                              • 1




                                I see it. I voted up nullpointer's response. That's the one.
                                – Perimosh
                                2 hours ago













                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              I think you are looking for something like this:



                              Map<String, Map<String, List>> map = personList.stream()
                              .collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));


                              The double grouping gives you a map of a map. That's the trick.






                              share|improve this answer












                              I think you are looking for something like this:



                              Map<String, Map<String, List>> map = personList.stream()
                              .collect(groupingBy(Person::getFirstName, groupingBy(Person::getLastName)));


                              The double grouping gives you a map of a map. That's the trick.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 2 hours ago









                              Perimosh

                              158211




                              158211







                              • 1




                                Thank you, but the value was supposed to be a simple list, not another map.
                                – Phil
                                2 hours ago






                              • 1




                                I see it. I voted up nullpointer's response. That's the one.
                                – Perimosh
                                2 hours ago













                              • 1




                                Thank you, but the value was supposed to be a simple list, not another map.
                                – Phil
                                2 hours ago






                              • 1




                                I see it. I voted up nullpointer's response. That's the one.
                                – Perimosh
                                2 hours ago








                              1




                              1




                              Thank you, but the value was supposed to be a simple list, not another map.
                              – Phil
                              2 hours ago




                              Thank you, but the value was supposed to be a simple list, not another map.
                              – Phil
                              2 hours ago




                              1




                              1




                              I see it. I voted up nullpointer's response. That's the one.
                              – Perimosh
                              2 hours ago





                              I see it. I voted up nullpointer's response. That's the one.
                              – Perimosh
                              2 hours ago


















                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52879392%2fjava-groupingby-subvalue-as-value%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              Installing NextGIS Connect into QGIS 3?

                              One-line joke