Convert an array to list with specific range in Java 8





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







10















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
    Dec 27 '18 at 6:28






  • 1





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

    – Naman
    Dec 27 '18 at 6:41













  • Basically all answers until now (except for the answer by TongChen ) are wasting time and space. (Literally - I mean, here on the site, as well as when they are implemented). Imagine having a List with 10 Million elements, and you just want to omit the first one. Creating a stream and collecting it to a new list is O(n) in time and space. The answer that uses Arrays.asList(optArr).subList(1,optArr.length) is an elegant, concise one-liner, with O(1) in time and space.

    – Marco13
    Dec 27 '18 at 15:26




















10















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
    Dec 27 '18 at 6:28






  • 1





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

    – Naman
    Dec 27 '18 at 6:41













  • Basically all answers until now (except for the answer by TongChen ) are wasting time and space. (Literally - I mean, here on the site, as well as when they are implemented). Imagine having a List with 10 Million elements, and you just want to omit the first one. Creating a stream and collecting it to a new list is O(n) in time and space. The answer that uses Arrays.asList(optArr).subList(1,optArr.length) is an elegant, concise one-liner, with O(1) in time and space.

    – Marco13
    Dec 27 '18 at 15:26
















10












10








10


4






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 Dec 27 '18 at 6:23









manjunath ramiganimanjunath ramigani

586416




586416








  • 1





    ArrayList class have a subList method, which can be used to slice the list.

    – raviraja
    Dec 27 '18 at 6:28






  • 1





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

    – Naman
    Dec 27 '18 at 6:41













  • Basically all answers until now (except for the answer by TongChen ) are wasting time and space. (Literally - I mean, here on the site, as well as when they are implemented). Imagine having a List with 10 Million elements, and you just want to omit the first one. Creating a stream and collecting it to a new list is O(n) in time and space. The answer that uses Arrays.asList(optArr).subList(1,optArr.length) is an elegant, concise one-liner, with O(1) in time and space.

    – Marco13
    Dec 27 '18 at 15:26
















  • 1





    ArrayList class have a subList method, which can be used to slice the list.

    – raviraja
    Dec 27 '18 at 6:28






  • 1





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

    – Naman
    Dec 27 '18 at 6:41













  • Basically all answers until now (except for the answer by TongChen ) are wasting time and space. (Literally - I mean, here on the site, as well as when they are implemented). Imagine having a List with 10 Million elements, and you just want to omit the first one. Creating a stream and collecting it to a new list is O(n) in time and space. The answer that uses Arrays.asList(optArr).subList(1,optArr.length) is an elegant, concise one-liner, with O(1) in time and space.

    – Marco13
    Dec 27 '18 at 15:26










1




1





ArrayList class have a subList method, which can be used to slice the list.

– raviraja
Dec 27 '18 at 6:28





ArrayList class have a subList method, which can be used to slice the list.

– raviraja
Dec 27 '18 at 6:28




1




1





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

– Naman
Dec 27 '18 at 6:41







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

– Naman
Dec 27 '18 at 6:41















Basically all answers until now (except for the answer by TongChen ) are wasting time and space. (Literally - I mean, here on the site, as well as when they are implemented). Imagine having a List with 10 Million elements, and you just want to omit the first one. Creating a stream and collecting it to a new list is O(n) in time and space. The answer that uses Arrays.asList(optArr).subList(1,optArr.length) is an elegant, concise one-liner, with O(1) in time and space.

– Marco13
Dec 27 '18 at 15:26







Basically all answers until now (except for the answer by TongChen ) are wasting time and space. (Literally - I mean, here on the site, as well as when they are implemented). Imagine having a List with 10 Million elements, and you just want to omit the first one. Creating a stream and collecting it to a new list is O(n) in time and space. The answer that uses Arrays.asList(optArr).subList(1,optArr.length) is an elegant, concise one-liner, with O(1) in time and space.

– Marco13
Dec 27 '18 at 15:26














9 Answers
9






active

oldest

votes


















13














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

    – Naman
    Dec 27 '18 at 6:38






  • 2





    @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
    Dec 27 '18 at 6:40



















17














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





    asList + subList has the advantage of not having to allocate a whole new list. The array is used as as backing storage for the list instead of being copied.

    – John Kugelman
    Dec 27 '18 at 14:03



















4














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
    Dec 27 '18 at 6:30











  • Why create a new list with streams, and not just Arrays.asList(optArr)?

    – Marco13
    Dec 27 '18 at 15:30



















3














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
























  • The first one should likely be the solution. O(1) time and space, in contrast to basically all other answers here.

    – Marco13
    Dec 27 '18 at 15:23



















2














You can also use Arrays.copyOfRange to copy an array.




Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive.




public static <T> T copyOfRange(T original, int from, int to)


And then convert into List



List<String> result = Arrays.asList(Arrays.copyOfRange(optArr, 1, optArr.length));


IntStream : one other way by using IntStream



List<String> result = IntStream.range(1, optArr.length).mapToObj(i->optArr[i]).collect(Collectors.toList());





share|improve this answer

































    1














    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































      1














      It should be like this.



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





      share|improve this answer































        1














        Why do you need to use streams? Why isn't some ancient magic like for-loops enough for the task?



        String optArr = {"start", "map1", "map2", "map3"};
        final List<String> list = new ArrayList<>();

        for (int i = 1; i < optArr.length; i++) // skip first element
        list.add(optArr[i]);


        The simplest tool for the job.






        share|improve this answer































          1














          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 wherever you can.






          share|improve this answer





















          • 1





            not content based, but index based

            – Naman
            Dec 27 '18 at 6:33












          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

























          9 Answers
          9






          active

          oldest

          votes








          9 Answers
          9






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          13














          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

            – Naman
            Dec 27 '18 at 6:38






          • 2





            @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
            Dec 27 '18 at 6:40
















          13














          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

            – Naman
            Dec 27 '18 at 6:38






          • 2





            @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
            Dec 27 '18 at 6:40














          13












          13








          13







          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 Dec 27 '18 at 6:27









          Robby CornelissenRobby Cornelissen

          46.7k147193




          46.7k147193













          • the only challenge with skip would be selecting initial elements instead of skipping

            – Naman
            Dec 27 '18 at 6:38






          • 2





            @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
            Dec 27 '18 at 6:40



















          • the only challenge with skip would be selecting initial elements instead of skipping

            – Naman
            Dec 27 '18 at 6:38






          • 2





            @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
            Dec 27 '18 at 6:40

















          the only challenge with skip would be selecting initial elements instead of skipping

          – Naman
          Dec 27 '18 at 6:38





          the only challenge with skip would be selecting initial elements instead of skipping

          – Naman
          Dec 27 '18 at 6:38




          2




          2





          @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
          Dec 27 '18 at 6:40





          @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
          Dec 27 '18 at 6:40













          17














          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





            asList + subList has the advantage of not having to allocate a whole new list. The array is used as as backing storage for the list instead of being copied.

            – John Kugelman
            Dec 27 '18 at 14:03
















          17














          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





            asList + subList has the advantage of not having to allocate a whole new list. The array is used as as backing storage for the list instead of being copied.

            – John Kugelman
            Dec 27 '18 at 14:03














          17












          17








          17







          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 Dec 27 '18 at 6:48

























          answered Dec 27 '18 at 6:27









          NamanNaman

          45.9k12102204




          45.9k12102204








          • 6





            asList + subList has the advantage of not having to allocate a whole new list. The array is used as as backing storage for the list instead of being copied.

            – John Kugelman
            Dec 27 '18 at 14:03














          • 6





            asList + subList has the advantage of not having to allocate a whole new list. The array is used as as backing storage for the list instead of being copied.

            – John Kugelman
            Dec 27 '18 at 14:03








          6




          6





          asList + subList has the advantage of not having to allocate a whole new list. The array is used as as backing storage for the list instead of being copied.

          – John Kugelman
          Dec 27 '18 at 14:03





          asList + subList has the advantage of not having to allocate a whole new list. The array is used as as backing storage for the list instead of being copied.

          – John Kugelman
          Dec 27 '18 at 14:03











          4














          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
            Dec 27 '18 at 6:30











          • Why create a new list with streams, and not just Arrays.asList(optArr)?

            – Marco13
            Dec 27 '18 at 15:30
















          4














          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
            Dec 27 '18 at 6:30











          • Why create a new list with streams, and not just Arrays.asList(optArr)?

            – Marco13
            Dec 27 '18 at 15:30














          4












          4








          4







          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 Dec 27 '18 at 6:30

























          answered Dec 27 '18 at 6:27









          Tim BiegeleisenTim Biegeleisen

          239k13100160




          239k13100160













          • @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.

            – Tim Biegeleisen
            Dec 27 '18 at 6:30











          • Why create a new list with streams, and not just Arrays.asList(optArr)?

            – Marco13
            Dec 27 '18 at 15:30



















          • @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.

            – Tim Biegeleisen
            Dec 27 '18 at 6:30











          • Why create a new list with streams, and not just Arrays.asList(optArr)?

            – Marco13
            Dec 27 '18 at 15:30

















          @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.

          – Tim Biegeleisen
          Dec 27 '18 at 6:30





          @nullpointer Um...I am suggesting to just run the original stream, and then view it however you want.

          – Tim Biegeleisen
          Dec 27 '18 at 6:30













          Why create a new list with streams, and not just Arrays.asList(optArr)?

          – Marco13
          Dec 27 '18 at 15:30





          Why create a new list with streams, and not just Arrays.asList(optArr)?

          – Marco13
          Dec 27 '18 at 15:30











          3














          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
























          • The first one should likely be the solution. O(1) time and space, in contrast to basically all other answers here.

            – Marco13
            Dec 27 '18 at 15:23
















          3














          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
























          • The first one should likely be the solution. O(1) time and space, in contrast to basically all other answers here.

            – Marco13
            Dec 27 '18 at 15:23














          3












          3








          3







          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 Dec 27 '18 at 6:33









          TongChenTongChen

          4951310




          4951310













          • The first one should likely be the solution. O(1) time and space, in contrast to basically all other answers here.

            – Marco13
            Dec 27 '18 at 15:23



















          • The first one should likely be the solution. O(1) time and space, in contrast to basically all other answers here.

            – Marco13
            Dec 27 '18 at 15:23

















          The first one should likely be the solution. O(1) time and space, in contrast to basically all other answers here.

          – Marco13
          Dec 27 '18 at 15:23





          The first one should likely be the solution. O(1) time and space, in contrast to basically all other answers here.

          – Marco13
          Dec 27 '18 at 15:23











          2














          You can also use Arrays.copyOfRange to copy an array.




          Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive.




          public static <T> T copyOfRange(T original, int from, int to)


          And then convert into List



          List<String> result = Arrays.asList(Arrays.copyOfRange(optArr, 1, optArr.length));


          IntStream : one other way by using IntStream



          List<String> result = IntStream.range(1, optArr.length).mapToObj(i->optArr[i]).collect(Collectors.toList());





          share|improve this answer






























            2














            You can also use Arrays.copyOfRange to copy an array.




            Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive.




            public static <T> T copyOfRange(T original, int from, int to)


            And then convert into List



            List<String> result = Arrays.asList(Arrays.copyOfRange(optArr, 1, optArr.length));


            IntStream : one other way by using IntStream



            List<String> result = IntStream.range(1, optArr.length).mapToObj(i->optArr[i]).collect(Collectors.toList());





            share|improve this answer




























              2












              2








              2







              You can also use Arrays.copyOfRange to copy an array.




              Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive.




              public static <T> T copyOfRange(T original, int from, int to)


              And then convert into List



              List<String> result = Arrays.asList(Arrays.copyOfRange(optArr, 1, optArr.length));


              IntStream : one other way by using IntStream



              List<String> result = IntStream.range(1, optArr.length).mapToObj(i->optArr[i]).collect(Collectors.toList());





              share|improve this answer















              You can also use Arrays.copyOfRange to copy an array.




              Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive.




              public static <T> T copyOfRange(T original, int from, int to)


              And then convert into List



              List<String> result = Arrays.asList(Arrays.copyOfRange(optArr, 1, optArr.length));


              IntStream : one other way by using IntStream



              List<String> result = IntStream.range(1, optArr.length).mapToObj(i->optArr[i]).collect(Collectors.toList());






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 27 '18 at 10:01

























              answered Dec 27 '18 at 9:31









              DeadpoolDeadpool

              7,8822831




              7,8822831























                  1














                  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




























                    1














                    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


























                      1












                      1








                      1







                      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













                      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












                      share|improve this answer



                      share|improve this answer










                      answered Dec 27 '18 at 6:28









                      Crutch MasterCrutch Master

                      814




                      814























                          1














                          It should be like this.



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





                          share|improve this answer




























                            1














                            It should be like this.



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





                            share|improve this answer


























                              1












                              1








                              1







                              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 Dec 27 '18 at 6:33









                              TheSprinterTheSprinter

                              587316




                              587316























                                  1














                                  Why do you need to use streams? Why isn't some ancient magic like for-loops enough for the task?



                                  String optArr = {"start", "map1", "map2", "map3"};
                                  final List<String> list = new ArrayList<>();

                                  for (int i = 1; i < optArr.length; i++) // skip first element
                                  list.add(optArr[i]);


                                  The simplest tool for the job.






                                  share|improve this answer




























                                    1














                                    Why do you need to use streams? Why isn't some ancient magic like for-loops enough for the task?



                                    String optArr = {"start", "map1", "map2", "map3"};
                                    final List<String> list = new ArrayList<>();

                                    for (int i = 1; i < optArr.length; i++) // skip first element
                                    list.add(optArr[i]);


                                    The simplest tool for the job.






                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      Why do you need to use streams? Why isn't some ancient magic like for-loops enough for the task?



                                      String optArr = {"start", "map1", "map2", "map3"};
                                      final List<String> list = new ArrayList<>();

                                      for (int i = 1; i < optArr.length; i++) // skip first element
                                      list.add(optArr[i]);


                                      The simplest tool for the job.






                                      share|improve this answer













                                      Why do you need to use streams? Why isn't some ancient magic like for-loops enough for the task?



                                      String optArr = {"start", "map1", "map2", "map3"};
                                      final List<String> list = new ArrayList<>();

                                      for (int i = 1; i < optArr.length; i++) // skip first element
                                      list.add(optArr[i]);


                                      The simplest tool for the job.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 27 '18 at 11:28









                                      Marko PacakMarko Pacak

                                      2,4891529




                                      2,4891529























                                          1














                                          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 wherever you can.






                                          share|improve this answer





















                                          • 1





                                            not content based, but index based

                                            – Naman
                                            Dec 27 '18 at 6:33
















                                          1














                                          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 wherever you can.






                                          share|improve this answer





















                                          • 1





                                            not content based, but index based

                                            – Naman
                                            Dec 27 '18 at 6:33














                                          1












                                          1








                                          1







                                          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 wherever 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 wherever you can.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Dec 27 '18 at 16:15

























                                          answered Dec 27 '18 at 6:31









                                          fastcodejavafastcodejava

                                          24.7k19110163




                                          24.7k19110163








                                          • 1





                                            not content based, but index based

                                            – Naman
                                            Dec 27 '18 at 6:33














                                          • 1





                                            not content based, but index based

                                            – Naman
                                            Dec 27 '18 at 6:33








                                          1




                                          1





                                          not content based, but index based

                                          – Naman
                                          Dec 27 '18 at 6:33





                                          not content based, but index based

                                          – Naman
                                          Dec 27 '18 at 6:33


















                                          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%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

                                          Willebadessen

                                          Ida-Boy-Ed-Garten

                                          Residenzschloss Arolsen