java IntStream cannot use collect(Collectors.toList()), compilation error, why? [duplicate]












4
















This question already has an answer here:




  • Java Stream Collectors.toList() wont compile

    3 answers




As below:



    IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!


Java 1.8 compiler gives type deduction error. Similar code could work for String type:



    List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());


Why? Does IntStream/LongStream/DoubleStream are not working the same way like other types? How to fix my compilation error?










share|improve this question















marked as duplicate by Oleksandr, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 10:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Call boxed () before collecting.

    – ETO
    Nov 16 '18 at 7:47
















4
















This question already has an answer here:




  • Java Stream Collectors.toList() wont compile

    3 answers




As below:



    IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!


Java 1.8 compiler gives type deduction error. Similar code could work for String type:



    List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());


Why? Does IntStream/LongStream/DoubleStream are not working the same way like other types? How to fix my compilation error?










share|improve this question















marked as duplicate by Oleksandr, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 10:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Call boxed () before collecting.

    – ETO
    Nov 16 '18 at 7:47














4












4








4









This question already has an answer here:




  • Java Stream Collectors.toList() wont compile

    3 answers




As below:



    IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!


Java 1.8 compiler gives type deduction error. Similar code could work for String type:



    List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());


Why? Does IntStream/LongStream/DoubleStream are not working the same way like other types? How to fix my compilation error?










share|improve this question

















This question already has an answer here:




  • Java Stream Collectors.toList() wont compile

    3 answers




As below:



    IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!


Java 1.8 compiler gives type deduction error. Similar code could work for String type:



    List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());


Why? Does IntStream/LongStream/DoubleStream are not working the same way like other types? How to fix my compilation error?





This question already has an answer here:




  • Java Stream Collectors.toList() wont compile

    3 answers








java compilation java-stream collect tolist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 11:51









Stefan Zobel

2,48231931




2,48231931










asked Nov 16 '18 at 7:44









Hind ForsumHind Forsum

3,07431742




3,07431742




marked as duplicate by Oleksandr, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 10:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Oleksandr, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 10:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Call boxed () before collecting.

    – ETO
    Nov 16 '18 at 7:47



















  • Call boxed () before collecting.

    – ETO
    Nov 16 '18 at 7:47

















Call boxed () before collecting.

– ETO
Nov 16 '18 at 7:47





Call boxed () before collecting.

– ETO
Nov 16 '18 at 7:47












2 Answers
2






active

oldest

votes


















2














IntStream (along with the other primitive streams) does not have a collect(Collector) method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer).



If you want to collect the ints into a List you can do:



List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);


Or you can call boxed() to convert the IntStream to a Stream<Integer>:



List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());


Both options will box the primitive ints into Integers, so which you want to use is up to you. Personally, I find the second option simpler and clearer.






share|improve this answer































    6














    The primitive streams don't have the same collect method as Stream. You can convert them to a stream of the wrapper type in order to use the collect method that accepts a Collector argument:



    List<Integer> list1 = iStream.boxed().collect(Collectors.toList());





    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      IntStream (along with the other primitive streams) does not have a collect(Collector) method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer).



      If you want to collect the ints into a List you can do:



      List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);


      Or you can call boxed() to convert the IntStream to a Stream<Integer>:



      List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());


      Both options will box the primitive ints into Integers, so which you want to use is up to you. Personally, I find the second option simpler and clearer.






      share|improve this answer




























        2














        IntStream (along with the other primitive streams) does not have a collect(Collector) method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer).



        If you want to collect the ints into a List you can do:



        List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);


        Or you can call boxed() to convert the IntStream to a Stream<Integer>:



        List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());


        Both options will box the primitive ints into Integers, so which you want to use is up to you. Personally, I find the second option simpler and clearer.






        share|improve this answer


























          2












          2








          2







          IntStream (along with the other primitive streams) does not have a collect(Collector) method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer).



          If you want to collect the ints into a List you can do:



          List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);


          Or you can call boxed() to convert the IntStream to a Stream<Integer>:



          List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());


          Both options will box the primitive ints into Integers, so which you want to use is up to you. Personally, I find the second option simpler and clearer.






          share|improve this answer













          IntStream (along with the other primitive streams) does not have a collect(Collector) method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer).



          If you want to collect the ints into a List you can do:



          List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);


          Or you can call boxed() to convert the IntStream to a Stream<Integer>:



          List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());


          Both options will box the primitive ints into Integers, so which you want to use is up to you. Personally, I find the second option simpler and clearer.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 7:51









          SlawSlaw

          9,36131235




          9,36131235

























              6














              The primitive streams don't have the same collect method as Stream. You can convert them to a stream of the wrapper type in order to use the collect method that accepts a Collector argument:



              List<Integer> list1 = iStream.boxed().collect(Collectors.toList());





              share|improve this answer




























                6














                The primitive streams don't have the same collect method as Stream. You can convert them to a stream of the wrapper type in order to use the collect method that accepts a Collector argument:



                List<Integer> list1 = iStream.boxed().collect(Collectors.toList());





                share|improve this answer


























                  6












                  6








                  6







                  The primitive streams don't have the same collect method as Stream. You can convert them to a stream of the wrapper type in order to use the collect method that accepts a Collector argument:



                  List<Integer> list1 = iStream.boxed().collect(Collectors.toList());





                  share|improve this answer













                  The primitive streams don't have the same collect method as Stream. You can convert them to a stream of the wrapper type in order to use the collect method that accepts a Collector argument:



                  List<Integer> list1 = iStream.boxed().collect(Collectors.toList());






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 7:46









                  EranEran

                  290k37477563




                  290k37477563















                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python