Convert an array to list with specific range in Java 8












6














I want to convert one string array to list with specific range. In my case I always want from index 1 to last index. I don't need the index 0 value included in the list. Is there any direct method that I can use to filter and convert to the list as I need ?



public class test1 {

public static void main(String args) {
String optArr = {"start", "map1", "map2", "map3"};
List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
System.out.println(list);
}
}









share|improve this question


















  • 1




    ArrayList class have a subList method, which can be used to slice the list.
    – raviraja
    1 hour ago






  • 1




    Aside: rename your class/variables to follow proper naming conventions and have meaningful names. e.g. ArrayToListConversionTest is also a better name
    – nullpointer
    1 hour ago


















6














I want to convert one string array to list with specific range. In my case I always want from index 1 to last index. I don't need the index 0 value included in the list. Is there any direct method that I can use to filter and convert to the list as I need ?



public class test1 {

public static void main(String args) {
String optArr = {"start", "map1", "map2", "map3"};
List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
System.out.println(list);
}
}









share|improve this question


















  • 1




    ArrayList class have a subList method, which can be used to slice the list.
    – raviraja
    1 hour ago






  • 1




    Aside: rename your class/variables to follow proper naming conventions and have meaningful names. e.g. ArrayToListConversionTest is also a better name
    – nullpointer
    1 hour ago
















6












6








6


3





I want to convert one string array to list with specific range. In my case I always want from index 1 to last index. I don't need the index 0 value included in the list. Is there any direct method that I can use to filter and convert to the list as I need ?



public class test1 {

public static void main(String args) {
String optArr = {"start", "map1", "map2", "map3"};
List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
System.out.println(list);
}
}









share|improve this question













I want to convert one string array to list with specific range. In my case I always want from index 1 to last index. I don't need the index 0 value included in the list. Is there any direct method that I can use to filter and convert to the list as I need ?



public class test1 {

public static void main(String args) {
String optArr = {"start", "map1", "map2", "map3"};
List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
System.out.println(list);
}
}






java java-8






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









manjunath ramigani

501415




501415








  • 1




    ArrayList class have a subList method, which can be used to slice the list.
    – raviraja
    1 hour ago






  • 1




    Aside: rename your class/variables to follow proper naming conventions and have meaningful names. e.g. ArrayToListConversionTest is also a better name
    – nullpointer
    1 hour ago
















  • 1




    ArrayList class have a subList method, which can be used to slice the list.
    – raviraja
    1 hour ago






  • 1




    Aside: rename your class/variables to follow proper naming conventions and have meaningful names. e.g. ArrayToListConversionTest is also a better name
    – nullpointer
    1 hour ago










1




1




ArrayList class have a subList method, which can be used to slice the list.
– raviraja
1 hour ago




ArrayList class have a subList method, which can be used to slice the list.
– raviraja
1 hour ago




1




1




Aside: rename your class/variables to follow proper naming conventions and have meaningful names. e.g. ArrayToListConversionTest is also a better name
– nullpointer
1 hour ago






Aside: rename your class/variables to follow proper naming conventions and have meaningful names. e.g. ArrayToListConversionTest is also a better name
– nullpointer
1 hour ago














7 Answers
7






active

oldest

votes


















6














You can use Stream.skip():



List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());





share|improve this answer





















  • the only challenge with skip would be selecting initial elements instead of skipping
    – nullpointer
    1 hour ago






  • 1




    @nullpointer I think we can all safely agree that for the given use case, your answer is the superior one. Have an upvote.
    – Robby Cornelissen
    1 hour ago



















6














You can also use the overloaded method Arrays.stream​(T array, int startInclusive, int endExclusive) as :



List<String> list = Arrays.stream(optArr, 1, optArr.length)
.collect(Collectors.toList());



Returns a sequential Stream with the specified range of the specified
array as its source
.






Alternatively(non Java-8), using the subList is an option, but I would prefer chaining it in one-line instead of creating a new object as:



List<String> list = Arrays.asList(optArr).subList(1, optArr.length);





share|improve this answer































    1














    One non Java 8 option might be to just create a view on top of your current list which omits the first element:



    List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
    List<String> viewList = list.subList(1, list.size());


    This would mean though that the underlying data structure is still the original list, but one extra element in memory does not seem like a big penalty.






    share|improve this answer























    • @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.
      – Tim Biegeleisen
      1 hour ago



















    0














    List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());


    I think it must work.



    See docs here : https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html






    share|improve this answer








    New contributor




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


























      0














      One method use List.sublist(int,int)



      List<String> list = Arrays.asList(optArr).subList(1,optArr.length);
      System.out.println(list);


      Second method use Stream



      List<String> list = Arrays.stream(optArr)
      .skip(1)
      .collect(Collectors.toList());
      System.out.println(list);





      share|improve this answer





























        0














        It should be like this.



        List<String> al = new ArrayList<>(Arrays.asList(optArr));
        List actualList = list.subList(1, al.size());





        share|improve this answer





























          0














          Looking at your data you might consider :



          List<String> list = Stream.of(optArr).filter(s -> s.startsWith("map")).collect(toList());


          But if you just want to filter the first index sublist is the way to go.
          You do not need to use stream whatever of are you can.






          share|improve this answer



















          • 1




            not content based, but index based
            – nullpointer
            1 hour 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',
          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%2f53940628%2fconvert-an-array-to-list-with-specific-range-in-java-8%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          You can use Stream.skip():



          List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());





          share|improve this answer





















          • the only challenge with skip would be selecting initial elements instead of skipping
            – nullpointer
            1 hour ago






          • 1




            @nullpointer I think we can all safely agree that for the given use case, your answer is the superior one. Have an upvote.
            – Robby Cornelissen
            1 hour ago
















          6














          You can use Stream.skip():



          List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());





          share|improve this answer





















          • the only challenge with skip would be selecting initial elements instead of skipping
            – nullpointer
            1 hour ago






          • 1




            @nullpointer I think we can all safely agree that for the given use case, your answer is the superior one. Have an upvote.
            – Robby Cornelissen
            1 hour ago














          6












          6








          6






          You can use Stream.skip():



          List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());





          share|improve this answer












          You can use Stream.skip():



          List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Robby Cornelissen

          43.2k126789




          43.2k126789












          • the only challenge with skip would be selecting initial elements instead of skipping
            – nullpointer
            1 hour ago






          • 1




            @nullpointer I think we can all safely agree that for the given use case, your answer is the superior one. Have an upvote.
            – Robby Cornelissen
            1 hour ago


















          • the only challenge with skip would be selecting initial elements instead of skipping
            – nullpointer
            1 hour ago






          • 1




            @nullpointer I think we can all safely agree that for the given use case, your answer is the superior one. Have an upvote.
            – Robby Cornelissen
            1 hour ago
















          the only challenge with skip would be selecting initial elements instead of skipping
          – nullpointer
          1 hour ago




          the only challenge with skip would be selecting initial elements instead of skipping
          – nullpointer
          1 hour ago




          1




          1




          @nullpointer I think we can all safely agree that for the given use case, your answer is the superior one. Have an upvote.
          – Robby Cornelissen
          1 hour ago




          @nullpointer I think we can all safely agree that for the given use case, your answer is the superior one. Have an upvote.
          – Robby Cornelissen
          1 hour ago













          6














          You can also use the overloaded method Arrays.stream​(T array, int startInclusive, int endExclusive) as :



          List<String> list = Arrays.stream(optArr, 1, optArr.length)
          .collect(Collectors.toList());



          Returns a sequential Stream with the specified range of the specified
          array as its source
          .






          Alternatively(non Java-8), using the subList is an option, but I would prefer chaining it in one-line instead of creating a new object as:



          List<String> list = Arrays.asList(optArr).subList(1, optArr.length);





          share|improve this answer




























            6














            You can also use the overloaded method Arrays.stream​(T array, int startInclusive, int endExclusive) as :



            List<String> list = Arrays.stream(optArr, 1, optArr.length)
            .collect(Collectors.toList());



            Returns a sequential Stream with the specified range of the specified
            array as its source
            .






            Alternatively(non Java-8), using the subList is an option, but I would prefer chaining it in one-line instead of creating a new object as:



            List<String> list = Arrays.asList(optArr).subList(1, optArr.length);





            share|improve this answer


























              6












              6








              6






              You can also use the overloaded method Arrays.stream​(T array, int startInclusive, int endExclusive) as :



              List<String> list = Arrays.stream(optArr, 1, optArr.length)
              .collect(Collectors.toList());



              Returns a sequential Stream with the specified range of the specified
              array as its source
              .






              Alternatively(non Java-8), using the subList is an option, but I would prefer chaining it in one-line instead of creating a new object as:



              List<String> list = Arrays.asList(optArr).subList(1, optArr.length);





              share|improve this answer














              You can also use the overloaded method Arrays.stream​(T array, int startInclusive, int endExclusive) as :



              List<String> list = Arrays.stream(optArr, 1, optArr.length)
              .collect(Collectors.toList());



              Returns a sequential Stream with the specified range of the specified
              array as its source
              .






              Alternatively(non Java-8), using the subList is an option, but I would prefer chaining it in one-line instead of creating a new object as:



              List<String> list = Arrays.asList(optArr).subList(1, optArr.length);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 hour ago

























              answered 1 hour ago









              nullpointer

              41.2k1086173




              41.2k1086173























                  1














                  One non Java 8 option might be to just create a view on top of your current list which omits the first element:



                  List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
                  List<String> viewList = list.subList(1, list.size());


                  This would mean though that the underlying data structure is still the original list, but one extra element in memory does not seem like a big penalty.






                  share|improve this answer























                  • @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.
                    – Tim Biegeleisen
                    1 hour ago
















                  1














                  One non Java 8 option might be to just create a view on top of your current list which omits the first element:



                  List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
                  List<String> viewList = list.subList(1, list.size());


                  This would mean though that the underlying data structure is still the original list, but one extra element in memory does not seem like a big penalty.






                  share|improve this answer























                  • @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.
                    – Tim Biegeleisen
                    1 hour ago














                  1












                  1








                  1






                  One non Java 8 option might be to just create a view on top of your current list which omits the first element:



                  List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
                  List<String> viewList = list.subList(1, list.size());


                  This would mean though that the underlying data structure is still the original list, but one extra element in memory does not seem like a big penalty.






                  share|improve this answer














                  One non Java 8 option might be to just create a view on top of your current list which omits the first element:



                  List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
                  List<String> viewList = list.subList(1, list.size());


                  This would mean though that the underlying data structure is still the original list, but one extra element in memory does not seem like a big penalty.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 1 hour ago

























                  answered 1 hour ago









                  Tim Biegeleisen

                  216k1386139




                  216k1386139












                  • @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.
                    – Tim Biegeleisen
                    1 hour ago


















                  • @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.
                    – Tim Biegeleisen
                    1 hour ago
















                  @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.
                  – Tim Biegeleisen
                  1 hour ago




                  @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.
                  – Tim Biegeleisen
                  1 hour ago











                  0














                  List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());


                  I think it must work.



                  See docs here : https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html






                  share|improve this answer








                  New contributor




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























                    0














                    List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());


                    I think it must work.



                    See docs here : https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html






                    share|improve this answer








                    New contributor




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





















                      0












                      0








                      0






                      List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());


                      I think it must work.



                      See docs here : https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html






                      share|improve this answer








                      New contributor




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









                      List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());


                      I think it must work.



                      See docs here : https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html







                      share|improve this answer








                      New contributor




                      Crutch Master 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 answer



                      share|improve this answer






                      New contributor




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









                      answered 1 hour ago









                      Crutch Master

                      313




                      313




                      New contributor




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





                      New contributor





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






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























                          0














                          One method use List.sublist(int,int)



                          List<String> list = Arrays.asList(optArr).subList(1,optArr.length);
                          System.out.println(list);


                          Second method use Stream



                          List<String> list = Arrays.stream(optArr)
                          .skip(1)
                          .collect(Collectors.toList());
                          System.out.println(list);





                          share|improve this answer


























                            0














                            One method use List.sublist(int,int)



                            List<String> list = Arrays.asList(optArr).subList(1,optArr.length);
                            System.out.println(list);


                            Second method use Stream



                            List<String> list = Arrays.stream(optArr)
                            .skip(1)
                            .collect(Collectors.toList());
                            System.out.println(list);





                            share|improve this answer
























                              0












                              0








                              0






                              One method use List.sublist(int,int)



                              List<String> list = Arrays.asList(optArr).subList(1,optArr.length);
                              System.out.println(list);


                              Second method use Stream



                              List<String> list = Arrays.stream(optArr)
                              .skip(1)
                              .collect(Collectors.toList());
                              System.out.println(list);





                              share|improve this answer












                              One method use List.sublist(int,int)



                              List<String> list = Arrays.asList(optArr).subList(1,optArr.length);
                              System.out.println(list);


                              Second method use Stream



                              List<String> list = Arrays.stream(optArr)
                              .skip(1)
                              .collect(Collectors.toList());
                              System.out.println(list);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 1 hour ago









                              TongChen

                              1046




                              1046























                                  0














                                  It should be like this.



                                  List<String> al = new ArrayList<>(Arrays.asList(optArr));
                                  List actualList = list.subList(1, al.size());





                                  share|improve this answer


























                                    0














                                    It should be like this.



                                    List<String> al = new ArrayList<>(Arrays.asList(optArr));
                                    List actualList = list.subList(1, al.size());





                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      It should be like this.



                                      List<String> al = new ArrayList<>(Arrays.asList(optArr));
                                      List actualList = list.subList(1, al.size());





                                      share|improve this answer












                                      It should be like this.



                                      List<String> al = new ArrayList<>(Arrays.asList(optArr));
                                      List actualList = list.subList(1, al.size());






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 1 hour ago









                                      TheSprinter

                                      244110




                                      244110























                                          0














                                          Looking at your data you might consider :



                                          List<String> list = Stream.of(optArr).filter(s -> s.startsWith("map")).collect(toList());


                                          But if you just want to filter the first index sublist is the way to go.
                                          You do not need to use stream whatever of are you can.






                                          share|improve this answer



















                                          • 1




                                            not content based, but index based
                                            – nullpointer
                                            1 hour ago
















                                          0














                                          Looking at your data you might consider :



                                          List<String> list = Stream.of(optArr).filter(s -> s.startsWith("map")).collect(toList());


                                          But if you just want to filter the first index sublist is the way to go.
                                          You do not need to use stream whatever of are you can.






                                          share|improve this answer



















                                          • 1




                                            not content based, but index based
                                            – nullpointer
                                            1 hour ago














                                          0












                                          0








                                          0






                                          Looking at your data you might consider :



                                          List<String> list = Stream.of(optArr).filter(s -> s.startsWith("map")).collect(toList());


                                          But if you just want to filter the first index sublist is the way to go.
                                          You do not need to use stream whatever of are you can.






                                          share|improve this answer














                                          Looking at your data you might consider :



                                          List<String> list = Stream.of(optArr).filter(s -> s.startsWith("map")).collect(toList());


                                          But if you just want to filter the first index sublist is the way to go.
                                          You do not need to use stream whatever of are you can.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited 1 hour ago

























                                          answered 1 hour ago









                                          fastcodejava

                                          23.8k19109161




                                          23.8k19109161








                                          • 1




                                            not content based, but index based
                                            – nullpointer
                                            1 hour ago














                                          • 1




                                            not content based, but index based
                                            – nullpointer
                                            1 hour ago








                                          1




                                          1




                                          not content based, but index based
                                          – nullpointer
                                          1 hour ago




                                          not content based, but index based
                                          – nullpointer
                                          1 hour ago


















                                          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.





                                          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%2f53940628%2fconvert-an-array-to-list-with-specific-range-in-java-8%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

                                          flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                          Mangá

                                           ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕