IntStream rangeClosed unable to return value other than int












9














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





























    9














    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



























      9












      9








      9


      1





      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















      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()); |

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








      java lambda java-8 java-stream






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      Nicholas K

      5,62151031




      5,62151031










      asked 1 hour ago









      Julez Jupiter

      169110




      169110
























          4 Answers
          4






          active

          oldest

          votes


















          8














          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.



          mapToObj allows you to map an int value to a reference type, and thus transform the IntStream to a Stream<SomeReferenceType>.






          share|improve this answer























          • Thank you. It works like a charm.
            – Julez Jupiter
            1 hour ago



















          5














          Use mapToObj instead :



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





          share|improve this answer





























            3














            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



















            • 3




              causes unnecessary overhead, mapToObj is the idiomatic approach and the way to go.
              – Aomine
              59 mins ago





















            2














            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



















            • 1




              Thank you for the clear explanation.
              – Julez Jupiter
              13 mins ago











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984363%2fintstream-rangeclosed-unable-to-return-value-other-than-int%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            8














            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.



            mapToObj allows you to map an int value to a reference type, and thus transform the IntStream to a Stream<SomeReferenceType>.






            share|improve this answer























            • Thank you. It works like a charm.
              – Julez Jupiter
              1 hour ago
















            8














            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.



            mapToObj allows you to map an int value to a reference type, and thus transform the IntStream to a Stream<SomeReferenceType>.






            share|improve this answer























            • Thank you. It works like a charm.
              – Julez Jupiter
              1 hour ago














            8












            8








            8






            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.



            mapToObj allows you to map an int value to a reference type, and thus transform the IntStream to a Stream<SomeReferenceType>.






            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.



            mapToObj allows you to map an int value to a reference type, and thus transform the IntStream to a Stream<SomeReferenceType>.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 1 hour ago









            Eran

            279k37449535




            279k37449535












            • Thank you. It works like a charm.
              – Julez Jupiter
              1 hour ago


















            • Thank you. It works like a charm.
              – Julez Jupiter
              1 hour ago
















            Thank you. It works like a charm.
            – Julez Jupiter
            1 hour ago




            Thank you. It works like a charm.
            – Julez Jupiter
            1 hour ago













            5














            Use mapToObj instead :



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





            share|improve this answer


























              5














              Use mapToObj instead :



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





              share|improve this answer
























                5












                5








                5






                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 1 hour ago









                Nicholas K

                5,62151031




                5,62151031























                    3














                    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



















                    • 3




                      causes unnecessary overhead, mapToObj is the idiomatic approach and the way to go.
                      – Aomine
                      59 mins ago


















                    3














                    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



















                    • 3




                      causes unnecessary overhead, mapToObj is the idiomatic approach and the way to go.
                      – Aomine
                      59 mins ago
















                    3












                    3








                    3






                    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 13 mins ago

























                    answered 1 hour ago









                    nullpointer

                    42.1k1089175




                    42.1k1089175








                    • 3




                      causes unnecessary overhead, mapToObj is the idiomatic approach and the way to go.
                      – Aomine
                      59 mins ago
















                    • 3




                      causes unnecessary overhead, mapToObj is the idiomatic approach and the way to go.
                      – Aomine
                      59 mins ago










                    3




                    3




                    causes unnecessary overhead, mapToObj is the idiomatic approach and the way to go.
                    – Aomine
                    59 mins ago






                    causes unnecessary overhead, mapToObj is the idiomatic approach and the way to go.
                    – Aomine
                    59 mins ago













                    2














                    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



















                    • 1




                      Thank you for the clear explanation.
                      – Julez Jupiter
                      13 mins ago
















                    2














                    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



















                    • 1




                      Thank you for the clear explanation.
                      – Julez Jupiter
                      13 mins ago














                    2












                    2








                    2






                    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 5 mins ago

























                    answered 32 mins ago









                    Aomine

                    39.4k73669




                    39.4k73669








                    • 1




                      Thank you for the clear explanation.
                      – Julez Jupiter
                      13 mins ago














                    • 1




                      Thank you for the clear explanation.
                      – Julez Jupiter
                      13 mins ago








                    1




                    1




                    Thank you for the clear explanation.
                    – Julez Jupiter
                    13 mins ago




                    Thank you for the clear explanation.
                    – Julez Jupiter
                    13 mins ago


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984363%2fintstream-rangeclosed-unable-to-return-value-other-than-int%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

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

                    Mangá

                    Eduardo VII do Reino Unido