Creating an ArrayList from a method which returns a String












-1















I have an custom class InfoAQ which has a method called public String getSeqInf(). Now I have an ArrayList<InfoAQ> infList and
I need an ArrayList<String>strList = new ArrayList<String>with the content from getSeqInf()for each element.



This is the way Im doing it right now ...



for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());


Is there an alternative way to make it ? Maybe a faster one or one liner ?










share|improve this question


















  • 1





    Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers

    – Sven Hakvoort
    Nov 21 '18 at 13:14











  • Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());

    – Suryavel TR
    Nov 21 '18 at 13:15








  • 1





    What you wrote can be one line. Just delete the new line you added.

    – matt
    Nov 21 '18 at 13:21
















-1















I have an custom class InfoAQ which has a method called public String getSeqInf(). Now I have an ArrayList<InfoAQ> infList and
I need an ArrayList<String>strList = new ArrayList<String>with the content from getSeqInf()for each element.



This is the way Im doing it right now ...



for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());


Is there an alternative way to make it ? Maybe a faster one or one liner ?










share|improve this question


















  • 1





    Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers

    – Sven Hakvoort
    Nov 21 '18 at 13:14











  • Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());

    – Suryavel TR
    Nov 21 '18 at 13:15








  • 1





    What you wrote can be one line. Just delete the new line you added.

    – matt
    Nov 21 '18 at 13:21














-1












-1








-1








I have an custom class InfoAQ which has a method called public String getSeqInf(). Now I have an ArrayList<InfoAQ> infList and
I need an ArrayList<String>strList = new ArrayList<String>with the content from getSeqInf()for each element.



This is the way Im doing it right now ...



for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());


Is there an alternative way to make it ? Maybe a faster one or one liner ?










share|improve this question














I have an custom class InfoAQ which has a method called public String getSeqInf(). Now I have an ArrayList<InfoAQ> infList and
I need an ArrayList<String>strList = new ArrayList<String>with the content from getSeqInf()for each element.



This is the way Im doing it right now ...



for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());


Is there an alternative way to make it ? Maybe a faster one or one liner ?







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 13:11









Ahmet KazamanAhmet Kazaman

352324




352324








  • 1





    Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers

    – Sven Hakvoort
    Nov 21 '18 at 13:14











  • Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());

    – Suryavel TR
    Nov 21 '18 at 13:15








  • 1





    What you wrote can be one line. Just delete the new line you added.

    – matt
    Nov 21 '18 at 13:21














  • 1





    Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers

    – Sven Hakvoort
    Nov 21 '18 at 13:14











  • Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());

    – Suryavel TR
    Nov 21 '18 at 13:15








  • 1





    What you wrote can be one line. Just delete the new line you added.

    – matt
    Nov 21 '18 at 13:21








1




1





Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers

– Sven Hakvoort
Nov 21 '18 at 13:14





Possible duplicate of Lambda expression to convert array/List of String to array/List of Integers

– Sven Hakvoort
Nov 21 '18 at 13:14













Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());

– Suryavel TR
Nov 21 '18 at 13:15







Try this --> List<String> outputList = list.stream().map(it -> it.getSeqInf()).collect(Collectors.toList());

– Suryavel TR
Nov 21 '18 at 13:15






1




1





What you wrote can be one line. Just delete the new line you added.

– matt
Nov 21 '18 at 13:21





What you wrote can be one line. Just delete the new line you added.

– matt
Nov 21 '18 at 13:21












5 Answers
5






active

oldest

votes


















2














Yes, there is:



strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());


The map step can be also written in another way:



strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());


which is know as method reference passing. Those two solutions are equivalent.






share|improve this answer





















  • 4





    InfoAQ::getSeqInf ?

    – matt
    Nov 21 '18 at 13:15






  • 1





    It is an equivalent way for map. Good suggestion.

    – Lorelorelore
    Nov 21 '18 at 13:18



















0














Also maybe this one:



List<String> strList = new ArrayList<String>();
infList.forEach(e -> strList.add(e.getSeqInf()));





share|improve this answer































    0














    And there is another one (-liner, if you format it in a single line):



    infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});


    while I would prefer a formatting in more lines:



    infList.forEach(currentInf -> {
    strList.add(currentInf.getSeqInf());
    });





    share|improve this answer































      0














      Using streams



      infList.stream()
      .map(InfoAQ::getSeqInf)
      .collect(Collectors.toCollection(ArrayList::new))


      Using Collectors.toCollection here to create an ArrayList that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList() does not guarantee this)



      May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance






      share|improve this answer

































        0














        This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.


        `List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`

        or

        `
        ArrayList<String> listString = new ArrayList<>();
        for(int i = 0; i < infoAq.size(); i++) {
        listString.add(infoAq.get(i).getSeqInf());
        }`





        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%2f53412813%2fcreating-an-arraylist-from-a-method-which-returns-a-string%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Yes, there is:



          strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());


          The map step can be also written in another way:



          strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());


          which is know as method reference passing. Those two solutions are equivalent.






          share|improve this answer





















          • 4





            InfoAQ::getSeqInf ?

            – matt
            Nov 21 '18 at 13:15






          • 1





            It is an equivalent way for map. Good suggestion.

            – Lorelorelore
            Nov 21 '18 at 13:18
















          2














          Yes, there is:



          strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());


          The map step can be also written in another way:



          strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());


          which is know as method reference passing. Those two solutions are equivalent.






          share|improve this answer





















          • 4





            InfoAQ::getSeqInf ?

            – matt
            Nov 21 '18 at 13:15






          • 1





            It is an equivalent way for map. Good suggestion.

            – Lorelorelore
            Nov 21 '18 at 13:18














          2












          2








          2







          Yes, there is:



          strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());


          The map step can be also written in another way:



          strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());


          which is know as method reference passing. Those two solutions are equivalent.






          share|improve this answer















          Yes, there is:



          strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());


          The map step can be also written in another way:



          strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());


          which is know as method reference passing. Those two solutions are equivalent.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 13:19

























          answered Nov 21 '18 at 13:14









          LoreloreloreLorelorelore

          2,02761527




          2,02761527








          • 4





            InfoAQ::getSeqInf ?

            – matt
            Nov 21 '18 at 13:15






          • 1





            It is an equivalent way for map. Good suggestion.

            – Lorelorelore
            Nov 21 '18 at 13:18














          • 4





            InfoAQ::getSeqInf ?

            – matt
            Nov 21 '18 at 13:15






          • 1





            It is an equivalent way for map. Good suggestion.

            – Lorelorelore
            Nov 21 '18 at 13:18








          4




          4





          InfoAQ::getSeqInf ?

          – matt
          Nov 21 '18 at 13:15





          InfoAQ::getSeqInf ?

          – matt
          Nov 21 '18 at 13:15




          1




          1





          It is an equivalent way for map. Good suggestion.

          – Lorelorelore
          Nov 21 '18 at 13:18





          It is an equivalent way for map. Good suggestion.

          – Lorelorelore
          Nov 21 '18 at 13:18













          0














          Also maybe this one:



          List<String> strList = new ArrayList<String>();
          infList.forEach(e -> strList.add(e.getSeqInf()));





          share|improve this answer




























            0














            Also maybe this one:



            List<String> strList = new ArrayList<String>();
            infList.forEach(e -> strList.add(e.getSeqInf()));





            share|improve this answer


























              0












              0








              0







              Also maybe this one:



              List<String> strList = new ArrayList<String>();
              infList.forEach(e -> strList.add(e.getSeqInf()));





              share|improve this answer













              Also maybe this one:



              List<String> strList = new ArrayList<String>();
              infList.forEach(e -> strList.add(e.getSeqInf()));






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 21 '18 at 13:18









              PulszarPulszar

              44426




              44426























                  0














                  And there is another one (-liner, if you format it in a single line):



                  infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});


                  while I would prefer a formatting in more lines:



                  infList.forEach(currentInf -> {
                  strList.add(currentInf.getSeqInf());
                  });





                  share|improve this answer




























                    0














                    And there is another one (-liner, if you format it in a single line):



                    infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});


                    while I would prefer a formatting in more lines:



                    infList.forEach(currentInf -> {
                    strList.add(currentInf.getSeqInf());
                    });





                    share|improve this answer


























                      0












                      0








                      0







                      And there is another one (-liner, if you format it in a single line):



                      infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});


                      while I would prefer a formatting in more lines:



                      infList.forEach(currentInf -> {
                      strList.add(currentInf.getSeqInf());
                      });





                      share|improve this answer













                      And there is another one (-liner, if you format it in a single line):



                      infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});


                      while I would prefer a formatting in more lines:



                      infList.forEach(currentInf -> {
                      strList.add(currentInf.getSeqInf());
                      });






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 21 '18 at 13:19









                      deHaardeHaar

                      2,45451628




                      2,45451628























                          0














                          Using streams



                          infList.stream()
                          .map(InfoAQ::getSeqInf)
                          .collect(Collectors.toCollection(ArrayList::new))


                          Using Collectors.toCollection here to create an ArrayList that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList() does not guarantee this)



                          May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance






                          share|improve this answer






























                            0














                            Using streams



                            infList.stream()
                            .map(InfoAQ::getSeqInf)
                            .collect(Collectors.toCollection(ArrayList::new))


                            Using Collectors.toCollection here to create an ArrayList that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList() does not guarantee this)



                            May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance






                            share|improve this answer




























                              0












                              0








                              0







                              Using streams



                              infList.stream()
                              .map(InfoAQ::getSeqInf)
                              .collect(Collectors.toCollection(ArrayList::new))


                              Using Collectors.toCollection here to create an ArrayList that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList() does not guarantee this)



                              May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance






                              share|improve this answer















                              Using streams



                              infList.stream()
                              .map(InfoAQ::getSeqInf)
                              .collect(Collectors.toCollection(ArrayList::new))


                              Using Collectors.toCollection here to create an ArrayList that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList() does not guarantee this)



                              May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 21 '18 at 13:21

























                              answered Nov 21 '18 at 13:15









                              user7user7

                              9,55932443




                              9,55932443























                                  0














                                  This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.


                                  `List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`

                                  or

                                  `
                                  ArrayList<String> listString = new ArrayList<>();
                                  for(int i = 0; i < infoAq.size(); i++) {
                                  listString.add(infoAq.get(i).getSeqInf());
                                  }`





                                  share|improve this answer




























                                    0














                                    This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.


                                    `List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`

                                    or

                                    `
                                    ArrayList<String> listString = new ArrayList<>();
                                    for(int i = 0; i < infoAq.size(); i++) {
                                    listString.add(infoAq.get(i).getSeqInf());
                                    }`





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.


                                      `List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`

                                      or

                                      `
                                      ArrayList<String> listString = new ArrayList<>();
                                      for(int i = 0; i < infoAq.size(); i++) {
                                      listString.add(infoAq.get(i).getSeqInf());
                                      }`





                                      share|improve this answer













                                      This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.


                                      `List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`

                                      or

                                      `
                                      ArrayList<String> listString = new ArrayList<>();
                                      for(int i = 0; i < infoAq.size(); i++) {
                                      listString.add(infoAq.get(i).getSeqInf());
                                      }`






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 21 '18 at 13:28









                                      Marcelo Lubanco ThoméMarcelo Lubanco Thomé

                                      111




                                      111






























                                          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%2f53412813%2fcreating-an-arraylist-from-a-method-which-returns-a-string%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

                                          MongoDB - Not Authorized To Execute Command

                                          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                          How to fix TextFormField cause rebuild widget in Flutter