IntStream rangeClosed unable to return value other than int [duplicate]












12















This question already has an answer here:




  • Why can't I map integers to strings when streaming from an array?

    5 answers




Why is this getting error? I thought map can return any value.



var s = IntStream.rangeClosed(1, 5).map(String::valueOf).collect(Collectors.toList());



| Error: | incompatible types: bad return type in method reference |
java.lang.String cannot be converted to int | var s =
IntStream.rangeClosed(1,
5).map(String::valueOf).collect(Collectors.toList()); |

^-------------^











share|improve this question















marked as duplicate by 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();
}
);
});
});
2 days ago


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.











  • 1




    Note that this post is tagged java-8, but the var keyword is available from Java 9.
    – MC Emperor
    2 days ago






  • 2




    @MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
    – nullpointer
    2 days ago


















12















This question already has an answer here:




  • Why can't I map integers to strings when streaming from an array?

    5 answers




Why is this getting error? I thought map can return any value.



var s = IntStream.rangeClosed(1, 5).map(String::valueOf).collect(Collectors.toList());



| Error: | incompatible types: bad return type in method reference |
java.lang.String cannot be converted to int | var s =
IntStream.rangeClosed(1,
5).map(String::valueOf).collect(Collectors.toList()); |

^-------------^











share|improve this question















marked as duplicate by 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();
}
);
});
});
2 days ago


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.











  • 1




    Note that this post is tagged java-8, but the var keyword is available from Java 9.
    – MC Emperor
    2 days ago






  • 2




    @MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
    – nullpointer
    2 days ago
















12












12








12


2






This question already has an answer here:




  • Why can't I map integers to strings when streaming from an array?

    5 answers




Why is this getting error? I thought map can return any value.



var s = IntStream.rangeClosed(1, 5).map(String::valueOf).collect(Collectors.toList());



| Error: | incompatible types: bad return type in method reference |
java.lang.String cannot be converted to int | var s =
IntStream.rangeClosed(1,
5).map(String::valueOf).collect(Collectors.toList()); |

^-------------^











share|improve this question
















This question already has an answer here:




  • Why can't I map integers to strings when streaming from an array?

    5 answers




Why is this getting error? I thought map can return any value.



var s = IntStream.rangeClosed(1, 5).map(String::valueOf).collect(Collectors.toList());



| Error: | incompatible types: bad return type in method reference |
java.lang.String cannot be converted to int | var s =
IntStream.rangeClosed(1,
5).map(String::valueOf).collect(Collectors.toList()); |

^-------------^






This question already has an answer here:




  • Why can't I map integers to strings when streaming from an array?

    5 answers








java lambda java-8 java-stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Nicholas K

5,74451031




5,74451031










asked 2 days ago









Julez Jupiter

326211




326211




marked as duplicate by 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();
}
);
});
});
2 days ago


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 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();
}
);
});
});
2 days ago


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.










  • 1




    Note that this post is tagged java-8, but the var keyword is available from Java 9.
    – MC Emperor
    2 days ago






  • 2




    @MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
    – nullpointer
    2 days ago
















  • 1




    Note that this post is tagged java-8, but the var keyword is available from Java 9.
    – MC Emperor
    2 days ago






  • 2




    @MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
    – nullpointer
    2 days ago










1




1




Note that this post is tagged java-8, but the var keyword is available from Java 9.
– MC Emperor
2 days ago




Note that this post is tagged java-8, but the var keyword is available from Java 9.
– MC Emperor
2 days ago




2




2




@MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
– nullpointer
2 days ago






@MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
– nullpointer
2 days ago














4 Answers
4






active

oldest

votes


















13














Use mapToObj:



var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());


map of IntStream can only map an int value to another int value. It accepts an IntUnaryOperator (which accepts an int and returns an int) as the mapper function, and returns an IntStream.



On the other hand, mapToObj allows you to map an int value to any reference type, and thus transform the IntStream to a Stream<SomeReferenceType>. It accepts an IntFunction<? extends U> (which accepts an int and returns a reference type) as the mapper function, and returns a Stream<U>.






share|improve this answer































    7














    Use mapToObj instead :



    IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());





    share|improve this answer





























      4














      While the aforementioned answers are correct and mapToObj is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.



      it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.



      So, let's go through the relevant stream pipeline operations:



      IntStream.rangeClosed returns an IntStream as per the documentation:




      Returns a sequential ordered IntStream from startInclusive (inclusive)
      to endInclusive (inclusive) by an incremental step of 1.




      invoking map on an IntStream is expected to return an IntStream as per the documentation:




      Returns a stream consisting of the results of applying the given
      function to the elements of this stream.




      As well as that it's important to note that the method declaration for map is as follows:



      IntStream map(IntUnaryOperator mapper)


      i.e. it takes a IntUnaryOperator which in fact represents an operation on a single int-valued operand that produces an int-valued result.



      However, you're passing a function String::valueOf which consumes an int as we're dealing with an IntStream and returns a String thus not compliant with IntUnaryOperator and this is the cause of the problem.



      Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R> as a result then mapToObj is the way to go.



      mapToObj is declared as:



      mapToObj(IntFunction<? extends U> mapper)



      IntFunction represents a function that accepts an int-valued argument and produces a result and this result is of type R which means you'll have a Stream<R> after the mapToObj.






      share|improve this answer































        4














        Alternatively, you could use IntStream.boxed as :



        var s = IntStream.rangeClosed(1, 5) // IntStream
        .boxed() // Stream<Integer>
        .map(String::valueOf) // Stream<String>
        .collect(Collectors.toList());


        since the IntStream originally is a sequence of primitive int-values elements.





        Another variant of performing such an operation would be :



        var s = IntStream.rangeClosed(1, 5)
        .boxed()
        .map(a -> Integer.toString(a))
        .collect(Collectors.toList());





        share|improve this answer






























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          13














          Use mapToObj:



          var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());


          map of IntStream can only map an int value to another int value. It accepts an IntUnaryOperator (which accepts an int and returns an int) as the mapper function, and returns an IntStream.



          On the other hand, mapToObj allows you to map an int value to any reference type, and thus transform the IntStream to a Stream<SomeReferenceType>. It accepts an IntFunction<? extends U> (which accepts an int and returns a reference type) as the mapper function, and returns a Stream<U>.






          share|improve this answer




























            13














            Use mapToObj:



            var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());


            map of IntStream can only map an int value to another int value. It accepts an IntUnaryOperator (which accepts an int and returns an int) as the mapper function, and returns an IntStream.



            On the other hand, mapToObj allows you to map an int value to any reference type, and thus transform the IntStream to a Stream<SomeReferenceType>. It accepts an IntFunction<? extends U> (which accepts an int and returns a reference type) as the mapper function, and returns a Stream<U>.






            share|improve this answer


























              13












              13








              13






              Use mapToObj:



              var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());


              map of IntStream can only map an int value to another int value. It accepts an IntUnaryOperator (which accepts an int and returns an int) as the mapper function, and returns an IntStream.



              On the other hand, mapToObj allows you to map an int value to any reference type, and thus transform the IntStream to a Stream<SomeReferenceType>. It accepts an IntFunction<? extends U> (which accepts an int and returns a reference type) as the mapper function, and returns a Stream<U>.






              share|improve this answer














              Use mapToObj:



              var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());


              map of IntStream can only map an int value to another int value. It accepts an IntUnaryOperator (which accepts an int and returns an int) as the mapper function, and returns an IntStream.



              On the other hand, mapToObj allows you to map an int value to any reference type, and thus transform the IntStream to a Stream<SomeReferenceType>. It accepts an IntFunction<? extends U> (which accepts an int and returns a reference type) as the mapper function, and returns a Stream<U>.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 days ago

























              answered 2 days ago









              Eran

              279k37452538




              279k37452538

























                  7














                  Use mapToObj instead :



                  IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());





                  share|improve this answer


























                    7














                    Use mapToObj instead :



                    IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());





                    share|improve this answer
























                      7












                      7








                      7






                      Use mapToObj instead :



                      IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());





                      share|improve this answer












                      Use mapToObj instead :



                      IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 2 days ago









                      Nicholas K

                      5,74451031




                      5,74451031























                          4














                          While the aforementioned answers are correct and mapToObj is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.



                          it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.



                          So, let's go through the relevant stream pipeline operations:



                          IntStream.rangeClosed returns an IntStream as per the documentation:




                          Returns a sequential ordered IntStream from startInclusive (inclusive)
                          to endInclusive (inclusive) by an incremental step of 1.




                          invoking map on an IntStream is expected to return an IntStream as per the documentation:




                          Returns a stream consisting of the results of applying the given
                          function to the elements of this stream.




                          As well as that it's important to note that the method declaration for map is as follows:



                          IntStream map(IntUnaryOperator mapper)


                          i.e. it takes a IntUnaryOperator which in fact represents an operation on a single int-valued operand that produces an int-valued result.



                          However, you're passing a function String::valueOf which consumes an int as we're dealing with an IntStream and returns a String thus not compliant with IntUnaryOperator and this is the cause of the problem.



                          Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R> as a result then mapToObj is the way to go.



                          mapToObj is declared as:



                          mapToObj(IntFunction<? extends U> mapper)



                          IntFunction represents a function that accepts an int-valued argument and produces a result and this result is of type R which means you'll have a Stream<R> after the mapToObj.






                          share|improve this answer




























                            4














                            While the aforementioned answers are correct and mapToObj is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.



                            it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.



                            So, let's go through the relevant stream pipeline operations:



                            IntStream.rangeClosed returns an IntStream as per the documentation:




                            Returns a sequential ordered IntStream from startInclusive (inclusive)
                            to endInclusive (inclusive) by an incremental step of 1.




                            invoking map on an IntStream is expected to return an IntStream as per the documentation:




                            Returns a stream consisting of the results of applying the given
                            function to the elements of this stream.




                            As well as that it's important to note that the method declaration for map is as follows:



                            IntStream map(IntUnaryOperator mapper)


                            i.e. it takes a IntUnaryOperator which in fact represents an operation on a single int-valued operand that produces an int-valued result.



                            However, you're passing a function String::valueOf which consumes an int as we're dealing with an IntStream and returns a String thus not compliant with IntUnaryOperator and this is the cause of the problem.



                            Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R> as a result then mapToObj is the way to go.



                            mapToObj is declared as:



                            mapToObj(IntFunction<? extends U> mapper)



                            IntFunction represents a function that accepts an int-valued argument and produces a result and this result is of type R which means you'll have a Stream<R> after the mapToObj.






                            share|improve this answer


























                              4












                              4








                              4






                              While the aforementioned answers are correct and mapToObj is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.



                              it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.



                              So, let's go through the relevant stream pipeline operations:



                              IntStream.rangeClosed returns an IntStream as per the documentation:




                              Returns a sequential ordered IntStream from startInclusive (inclusive)
                              to endInclusive (inclusive) by an incremental step of 1.




                              invoking map on an IntStream is expected to return an IntStream as per the documentation:




                              Returns a stream consisting of the results of applying the given
                              function to the elements of this stream.




                              As well as that it's important to note that the method declaration for map is as follows:



                              IntStream map(IntUnaryOperator mapper)


                              i.e. it takes a IntUnaryOperator which in fact represents an operation on a single int-valued operand that produces an int-valued result.



                              However, you're passing a function String::valueOf which consumes an int as we're dealing with an IntStream and returns a String thus not compliant with IntUnaryOperator and this is the cause of the problem.



                              Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R> as a result then mapToObj is the way to go.



                              mapToObj is declared as:



                              mapToObj(IntFunction<? extends U> mapper)



                              IntFunction represents a function that accepts an int-valued argument and produces a result and this result is of type R which means you'll have a Stream<R> after the mapToObj.






                              share|improve this answer














                              While the aforementioned answers are correct and mapToObj is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.



                              it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.



                              So, let's go through the relevant stream pipeline operations:



                              IntStream.rangeClosed returns an IntStream as per the documentation:




                              Returns a sequential ordered IntStream from startInclusive (inclusive)
                              to endInclusive (inclusive) by an incremental step of 1.




                              invoking map on an IntStream is expected to return an IntStream as per the documentation:




                              Returns a stream consisting of the results of applying the given
                              function to the elements of this stream.




                              As well as that it's important to note that the method declaration for map is as follows:



                              IntStream map(IntUnaryOperator mapper)


                              i.e. it takes a IntUnaryOperator which in fact represents an operation on a single int-valued operand that produces an int-valued result.



                              However, you're passing a function String::valueOf which consumes an int as we're dealing with an IntStream and returns a String thus not compliant with IntUnaryOperator and this is the cause of the problem.



                              Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R> as a result then mapToObj is the way to go.



                              mapToObj is declared as:



                              mapToObj(IntFunction<? extends U> mapper)



                              IntFunction represents a function that accepts an int-valued argument and produces a result and this result is of type R which means you'll have a Stream<R> after the mapToObj.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 2 days ago

























                              answered 2 days ago









                              Aomine

                              40k73770




                              40k73770























                                  4














                                  Alternatively, you could use IntStream.boxed as :



                                  var s = IntStream.rangeClosed(1, 5) // IntStream
                                  .boxed() // Stream<Integer>
                                  .map(String::valueOf) // Stream<String>
                                  .collect(Collectors.toList());


                                  since the IntStream originally is a sequence of primitive int-values elements.





                                  Another variant of performing such an operation would be :



                                  var s = IntStream.rangeClosed(1, 5)
                                  .boxed()
                                  .map(a -> Integer.toString(a))
                                  .collect(Collectors.toList());





                                  share|improve this answer




























                                    4














                                    Alternatively, you could use IntStream.boxed as :



                                    var s = IntStream.rangeClosed(1, 5) // IntStream
                                    .boxed() // Stream<Integer>
                                    .map(String::valueOf) // Stream<String>
                                    .collect(Collectors.toList());


                                    since the IntStream originally is a sequence of primitive int-values elements.





                                    Another variant of performing such an operation would be :



                                    var s = IntStream.rangeClosed(1, 5)
                                    .boxed()
                                    .map(a -> Integer.toString(a))
                                    .collect(Collectors.toList());





                                    share|improve this answer


























                                      4












                                      4








                                      4






                                      Alternatively, you could use IntStream.boxed as :



                                      var s = IntStream.rangeClosed(1, 5) // IntStream
                                      .boxed() // Stream<Integer>
                                      .map(String::valueOf) // Stream<String>
                                      .collect(Collectors.toList());


                                      since the IntStream originally is a sequence of primitive int-values elements.





                                      Another variant of performing such an operation would be :



                                      var s = IntStream.rangeClosed(1, 5)
                                      .boxed()
                                      .map(a -> Integer.toString(a))
                                      .collect(Collectors.toList());





                                      share|improve this answer














                                      Alternatively, you could use IntStream.boxed as :



                                      var s = IntStream.rangeClosed(1, 5) // IntStream
                                      .boxed() // Stream<Integer>
                                      .map(String::valueOf) // Stream<String>
                                      .collect(Collectors.toList());


                                      since the IntStream originally is a sequence of primitive int-values elements.





                                      Another variant of performing such an operation would be :



                                      var s = IntStream.rangeClosed(1, 5)
                                      .boxed()
                                      .map(a -> Integer.toString(a))
                                      .collect(Collectors.toList());






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 2 days ago

























                                      answered 2 days ago









                                      nullpointer

                                      42.8k1091175




                                      42.8k1091175















                                          Popular posts from this blog

                                          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                          Alcedinidae

                                          RAC Tourist Trophy