Why does Optional.map make this assignment work? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Stream.findFirst different than Optional.of?Does a finally block always get executed in Java?How does the Java 'for each' loop work?What is a serialVersionUID and why should I use it?Why does Java have transient fields?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why does this code using random strings print “hello world”?Why is printing “B” dramatically slower than printing “#”?

Can you force honesty by using the Speak with Dead and Zone of Truth spells together?

What are the main differences between Stargate SG-1 cuts?

I can't produce songs

How many time has Arya actually used Needle?

Delete free apps from library

How to ternary Plot3D a function

Putting class ranking in CV, but against dept guidelines

Did any compiler fully use 80-bit floating point?

Does silver oxide react with hydrogen sulfide?

Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?

Is CEO the "profession" with the most psychopaths?

what is the log of the PDF for a Normal Distribution?

Monty Hall Problem-Probability Paradox

A `coordinate` command ignored

Positioning dot before text in math mode

Tips to organize LaTeX presentations for a semester

Tannaka duality for semisimple groups

GDP with Intermediate Production

Is there public access to the Meteor Crater in Arizona?

Why are vacuum tubes still used in amateur radios?

How to force a browser when connecting to a specific domain to be https only using only the client machine?

If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?

How to ask rejected full-time candidates to apply to teach individual courses?

Can two person see the same photon?



Why does Optional.map make this assignment work?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Stream.findFirst different than Optional.of?Does a finally block always get executed in Java?How does the Java 'for each' loop work?What is a serialVersionUID and why should I use it?Why does Java have transient fields?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why does this code using random strings print “hello world”?Why is printing “B” dramatically slower than printing “#”?



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








34















Optional<ArrayList<String>> option = Optional.of(new ArrayList<>());

Optional<ArrayList<?>> doesntWork = option;

Optional<ArrayList<?>> works = option.map(list -> list);


The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>>. Is there some sort of implicit cast going on?










share|improve this question






























    34















    Optional<ArrayList<String>> option = Optional.of(new ArrayList<>());

    Optional<ArrayList<?>> doesntWork = option;

    Optional<ArrayList<?>> works = option.map(list -> list);


    The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>>. Is there some sort of implicit cast going on?










    share|improve this question


























      34












      34








      34


      4






      Optional<ArrayList<String>> option = Optional.of(new ArrayList<>());

      Optional<ArrayList<?>> doesntWork = option;

      Optional<ArrayList<?>> works = option.map(list -> list);


      The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>>. Is there some sort of implicit cast going on?










      share|improve this question
















      Optional<ArrayList<String>> option = Optional.of(new ArrayList<>());

      Optional<ArrayList<?>> doesntWork = option;

      Optional<ArrayList<?>> works = option.map(list -> list);


      The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>>. Is there some sort of implicit cast going on?







      java generics java-8 optional






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 4 at 8:29









      Eran

      293k37485565




      293k37485565










      asked Apr 4 at 8:12









      Carl MindenCarl Minden

      423418




      423418






















          4 Answers
          4






          active

          oldest

          votes


















          22














          If you look into the code of map and follow all the method calls, you'll see that option.map(list -> list) ends up returning new Optional<>(option.get()). So you can replace your last assignment with:



          Optional<ArrayList<?>> works = new Optional<>(option.get());


          This creates a new Optional<ArrayList<?>> and initializes its value instance variable (whose type is ArrayList<?>) with the ArrayList<String> returned by map.get(). This is a valid assignment.




          Is there some sort of implicit cast going on?




          No, map returns a new Optional instance. It doesn't cast the original instance on which it was called.



          Here's the chain of method calls:



          option.map(list -> list)


          returns (since option is not empty)



          Optional.ofNullable(mapper.apply(value))


          which in your case is the same as



          Optional.ofNullable(value)


          which returns (since the value is not null):



          Optional.of(value)


          which returns



          new Optional<>(value)





          share|improve this answer

























          • Interesting, it just seems so strange that .map(list -> list) would do something, but yeah I suppose that makes sense.

            – Carl Minden
            Apr 4 at 8:26






          • 3





            See also this answer. Its last code example uses map(Function.identity()) where a direct assignment of the Optional is not possible. In the context of this question, A would ArrayList<?> and B would be ArrayList<String>.

            – Holger
            Apr 4 at 10:31



















          10














          Well the first one does not work because generics are invariant, the only way to make them covariant is to add a bounded type for example:



           Optional<? extends ArrayList<String>> doesntWork = option; 


          that would compile.



          And when you say that the map step should no accomplish anything is well, not correct. Look at the definition of Optional::map:



          public <U> Optional<U> map(Function<? super T, ? extends U> mapper) 
          Objects.requireNonNull(mapper);
          if (!isPresent())
          return empty();
          else
          return Optional.ofNullable(mapper.apply(value));




          roughly speaking it does transform from Optional<T> to Optional<U>...






          share|improve this answer






























            0














            Your option.map has the signature



            <ArrayList<?>> Optional<ArrayList<?>> java.util.Optional.map(Function<? super ArrayList<String>, ? extends ArrayList<?>> mapper)


            So this



            Optional<? extends ArrayList<?>> doesntWork = option;


            does compile.






            share|improve this answer























            • Yeah, that does compile, but I am more interested in why the map fixes it, this is just a simplistic version of what I am actually trying to do. In my actual code I am overriding a method that needs to return the equivalent of the Optional<ArrayList<?>> so just changing the type of that assignment doesn't really help.

              – Carl Minden
              Apr 4 at 8:22


















            0














            In your latter case the return type of the Optional.map method is implicitly determined by the type of your works variable. That's why there is a difference.






            share|improve this answer























            • But how is that different from the first assignment? if the signature of map is determined to by the type declaration of the assignment to return Optional<ArrayList<?>> then that means the return of the lambda would be of the type ArrayList<?> and since my lambda is (at least as far as I can tell) returning the concrete type ArrayList<String> something somewhere is casting it after the fact I guess?

              – Carl Minden
              Apr 4 at 8:24











            • Yes, exactly. The difference is that the type of option is static where the return type of Optional.map is dynamically determined by the to be assigned variable.

              – dpr
              Apr 4 at 8:42











            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%2f55510951%2fwhy-does-optional-map-make-this-assignment-work%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









            22














            If you look into the code of map and follow all the method calls, you'll see that option.map(list -> list) ends up returning new Optional<>(option.get()). So you can replace your last assignment with:



            Optional<ArrayList<?>> works = new Optional<>(option.get());


            This creates a new Optional<ArrayList<?>> and initializes its value instance variable (whose type is ArrayList<?>) with the ArrayList<String> returned by map.get(). This is a valid assignment.




            Is there some sort of implicit cast going on?




            No, map returns a new Optional instance. It doesn't cast the original instance on which it was called.



            Here's the chain of method calls:



            option.map(list -> list)


            returns (since option is not empty)



            Optional.ofNullable(mapper.apply(value))


            which in your case is the same as



            Optional.ofNullable(value)


            which returns (since the value is not null):



            Optional.of(value)


            which returns



            new Optional<>(value)





            share|improve this answer

























            • Interesting, it just seems so strange that .map(list -> list) would do something, but yeah I suppose that makes sense.

              – Carl Minden
              Apr 4 at 8:26






            • 3





              See also this answer. Its last code example uses map(Function.identity()) where a direct assignment of the Optional is not possible. In the context of this question, A would ArrayList<?> and B would be ArrayList<String>.

              – Holger
              Apr 4 at 10:31
















            22














            If you look into the code of map and follow all the method calls, you'll see that option.map(list -> list) ends up returning new Optional<>(option.get()). So you can replace your last assignment with:



            Optional<ArrayList<?>> works = new Optional<>(option.get());


            This creates a new Optional<ArrayList<?>> and initializes its value instance variable (whose type is ArrayList<?>) with the ArrayList<String> returned by map.get(). This is a valid assignment.




            Is there some sort of implicit cast going on?




            No, map returns a new Optional instance. It doesn't cast the original instance on which it was called.



            Here's the chain of method calls:



            option.map(list -> list)


            returns (since option is not empty)



            Optional.ofNullable(mapper.apply(value))


            which in your case is the same as



            Optional.ofNullable(value)


            which returns (since the value is not null):



            Optional.of(value)


            which returns



            new Optional<>(value)





            share|improve this answer

























            • Interesting, it just seems so strange that .map(list -> list) would do something, but yeah I suppose that makes sense.

              – Carl Minden
              Apr 4 at 8:26






            • 3





              See also this answer. Its last code example uses map(Function.identity()) where a direct assignment of the Optional is not possible. In the context of this question, A would ArrayList<?> and B would be ArrayList<String>.

              – Holger
              Apr 4 at 10:31














            22












            22








            22







            If you look into the code of map and follow all the method calls, you'll see that option.map(list -> list) ends up returning new Optional<>(option.get()). So you can replace your last assignment with:



            Optional<ArrayList<?>> works = new Optional<>(option.get());


            This creates a new Optional<ArrayList<?>> and initializes its value instance variable (whose type is ArrayList<?>) with the ArrayList<String> returned by map.get(). This is a valid assignment.




            Is there some sort of implicit cast going on?




            No, map returns a new Optional instance. It doesn't cast the original instance on which it was called.



            Here's the chain of method calls:



            option.map(list -> list)


            returns (since option is not empty)



            Optional.ofNullable(mapper.apply(value))


            which in your case is the same as



            Optional.ofNullable(value)


            which returns (since the value is not null):



            Optional.of(value)


            which returns



            new Optional<>(value)





            share|improve this answer















            If you look into the code of map and follow all the method calls, you'll see that option.map(list -> list) ends up returning new Optional<>(option.get()). So you can replace your last assignment with:



            Optional<ArrayList<?>> works = new Optional<>(option.get());


            This creates a new Optional<ArrayList<?>> and initializes its value instance variable (whose type is ArrayList<?>) with the ArrayList<String> returned by map.get(). This is a valid assignment.




            Is there some sort of implicit cast going on?




            No, map returns a new Optional instance. It doesn't cast the original instance on which it was called.



            Here's the chain of method calls:



            option.map(list -> list)


            returns (since option is not empty)



            Optional.ofNullable(mapper.apply(value))


            which in your case is the same as



            Optional.ofNullable(value)


            which returns (since the value is not null):



            Optional.of(value)


            which returns



            new Optional<>(value)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 5 at 6:25

























            answered Apr 4 at 8:23









            EranEran

            293k37485565




            293k37485565












            • Interesting, it just seems so strange that .map(list -> list) would do something, but yeah I suppose that makes sense.

              – Carl Minden
              Apr 4 at 8:26






            • 3





              See also this answer. Its last code example uses map(Function.identity()) where a direct assignment of the Optional is not possible. In the context of this question, A would ArrayList<?> and B would be ArrayList<String>.

              – Holger
              Apr 4 at 10:31


















            • Interesting, it just seems so strange that .map(list -> list) would do something, but yeah I suppose that makes sense.

              – Carl Minden
              Apr 4 at 8:26






            • 3





              See also this answer. Its last code example uses map(Function.identity()) where a direct assignment of the Optional is not possible. In the context of this question, A would ArrayList<?> and B would be ArrayList<String>.

              – Holger
              Apr 4 at 10:31

















            Interesting, it just seems so strange that .map(list -> list) would do something, but yeah I suppose that makes sense.

            – Carl Minden
            Apr 4 at 8:26





            Interesting, it just seems so strange that .map(list -> list) would do something, but yeah I suppose that makes sense.

            – Carl Minden
            Apr 4 at 8:26




            3




            3





            See also this answer. Its last code example uses map(Function.identity()) where a direct assignment of the Optional is not possible. In the context of this question, A would ArrayList<?> and B would be ArrayList<String>.

            – Holger
            Apr 4 at 10:31






            See also this answer. Its last code example uses map(Function.identity()) where a direct assignment of the Optional is not possible. In the context of this question, A would ArrayList<?> and B would be ArrayList<String>.

            – Holger
            Apr 4 at 10:31














            10














            Well the first one does not work because generics are invariant, the only way to make them covariant is to add a bounded type for example:



             Optional<? extends ArrayList<String>> doesntWork = option; 


            that would compile.



            And when you say that the map step should no accomplish anything is well, not correct. Look at the definition of Optional::map:



            public <U> Optional<U> map(Function<? super T, ? extends U> mapper) 
            Objects.requireNonNull(mapper);
            if (!isPresent())
            return empty();
            else
            return Optional.ofNullable(mapper.apply(value));




            roughly speaking it does transform from Optional<T> to Optional<U>...






            share|improve this answer



























              10














              Well the first one does not work because generics are invariant, the only way to make them covariant is to add a bounded type for example:



               Optional<? extends ArrayList<String>> doesntWork = option; 


              that would compile.



              And when you say that the map step should no accomplish anything is well, not correct. Look at the definition of Optional::map:



              public <U> Optional<U> map(Function<? super T, ? extends U> mapper) 
              Objects.requireNonNull(mapper);
              if (!isPresent())
              return empty();
              else
              return Optional.ofNullable(mapper.apply(value));




              roughly speaking it does transform from Optional<T> to Optional<U>...






              share|improve this answer

























                10












                10








                10







                Well the first one does not work because generics are invariant, the only way to make them covariant is to add a bounded type for example:



                 Optional<? extends ArrayList<String>> doesntWork = option; 


                that would compile.



                And when you say that the map step should no accomplish anything is well, not correct. Look at the definition of Optional::map:



                public <U> Optional<U> map(Function<? super T, ? extends U> mapper) 
                Objects.requireNonNull(mapper);
                if (!isPresent())
                return empty();
                else
                return Optional.ofNullable(mapper.apply(value));




                roughly speaking it does transform from Optional<T> to Optional<U>...






                share|improve this answer













                Well the first one does not work because generics are invariant, the only way to make them covariant is to add a bounded type for example:



                 Optional<? extends ArrayList<String>> doesntWork = option; 


                that would compile.



                And when you say that the map step should no accomplish anything is well, not correct. Look at the definition of Optional::map:



                public <U> Optional<U> map(Function<? super T, ? extends U> mapper) 
                Objects.requireNonNull(mapper);
                if (!isPresent())
                return empty();
                else
                return Optional.ofNullable(mapper.apply(value));




                roughly speaking it does transform from Optional<T> to Optional<U>...







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 4 at 9:15









                EugeneEugene

                72.6k9103175




                72.6k9103175





















                    0














                    Your option.map has the signature



                    <ArrayList<?>> Optional<ArrayList<?>> java.util.Optional.map(Function<? super ArrayList<String>, ? extends ArrayList<?>> mapper)


                    So this



                    Optional<? extends ArrayList<?>> doesntWork = option;


                    does compile.






                    share|improve this answer























                    • Yeah, that does compile, but I am more interested in why the map fixes it, this is just a simplistic version of what I am actually trying to do. In my actual code I am overriding a method that needs to return the equivalent of the Optional<ArrayList<?>> so just changing the type of that assignment doesn't really help.

                      – Carl Minden
                      Apr 4 at 8:22















                    0














                    Your option.map has the signature



                    <ArrayList<?>> Optional<ArrayList<?>> java.util.Optional.map(Function<? super ArrayList<String>, ? extends ArrayList<?>> mapper)


                    So this



                    Optional<? extends ArrayList<?>> doesntWork = option;


                    does compile.






                    share|improve this answer























                    • Yeah, that does compile, but I am more interested in why the map fixes it, this is just a simplistic version of what I am actually trying to do. In my actual code I am overriding a method that needs to return the equivalent of the Optional<ArrayList<?>> so just changing the type of that assignment doesn't really help.

                      – Carl Minden
                      Apr 4 at 8:22













                    0












                    0








                    0







                    Your option.map has the signature



                    <ArrayList<?>> Optional<ArrayList<?>> java.util.Optional.map(Function<? super ArrayList<String>, ? extends ArrayList<?>> mapper)


                    So this



                    Optional<? extends ArrayList<?>> doesntWork = option;


                    does compile.






                    share|improve this answer













                    Your option.map has the signature



                    <ArrayList<?>> Optional<ArrayList<?>> java.util.Optional.map(Function<? super ArrayList<String>, ? extends ArrayList<?>> mapper)


                    So this



                    Optional<? extends ArrayList<?>> doesntWork = option;


                    does compile.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 4 at 8:17









                    Lutz HornLutz Horn

                    66212




                    66212












                    • Yeah, that does compile, but I am more interested in why the map fixes it, this is just a simplistic version of what I am actually trying to do. In my actual code I am overriding a method that needs to return the equivalent of the Optional<ArrayList<?>> so just changing the type of that assignment doesn't really help.

                      – Carl Minden
                      Apr 4 at 8:22

















                    • Yeah, that does compile, but I am more interested in why the map fixes it, this is just a simplistic version of what I am actually trying to do. In my actual code I am overriding a method that needs to return the equivalent of the Optional<ArrayList<?>> so just changing the type of that assignment doesn't really help.

                      – Carl Minden
                      Apr 4 at 8:22
















                    Yeah, that does compile, but I am more interested in why the map fixes it, this is just a simplistic version of what I am actually trying to do. In my actual code I am overriding a method that needs to return the equivalent of the Optional<ArrayList<?>> so just changing the type of that assignment doesn't really help.

                    – Carl Minden
                    Apr 4 at 8:22





                    Yeah, that does compile, but I am more interested in why the map fixes it, this is just a simplistic version of what I am actually trying to do. In my actual code I am overriding a method that needs to return the equivalent of the Optional<ArrayList<?>> so just changing the type of that assignment doesn't really help.

                    – Carl Minden
                    Apr 4 at 8:22











                    0














                    In your latter case the return type of the Optional.map method is implicitly determined by the type of your works variable. That's why there is a difference.






                    share|improve this answer























                    • But how is that different from the first assignment? if the signature of map is determined to by the type declaration of the assignment to return Optional<ArrayList<?>> then that means the return of the lambda would be of the type ArrayList<?> and since my lambda is (at least as far as I can tell) returning the concrete type ArrayList<String> something somewhere is casting it after the fact I guess?

                      – Carl Minden
                      Apr 4 at 8:24











                    • Yes, exactly. The difference is that the type of option is static where the return type of Optional.map is dynamically determined by the to be assigned variable.

                      – dpr
                      Apr 4 at 8:42















                    0














                    In your latter case the return type of the Optional.map method is implicitly determined by the type of your works variable. That's why there is a difference.






                    share|improve this answer























                    • But how is that different from the first assignment? if the signature of map is determined to by the type declaration of the assignment to return Optional<ArrayList<?>> then that means the return of the lambda would be of the type ArrayList<?> and since my lambda is (at least as far as I can tell) returning the concrete type ArrayList<String> something somewhere is casting it after the fact I guess?

                      – Carl Minden
                      Apr 4 at 8:24











                    • Yes, exactly. The difference is that the type of option is static where the return type of Optional.map is dynamically determined by the to be assigned variable.

                      – dpr
                      Apr 4 at 8:42













                    0












                    0








                    0







                    In your latter case the return type of the Optional.map method is implicitly determined by the type of your works variable. That's why there is a difference.






                    share|improve this answer













                    In your latter case the return type of the Optional.map method is implicitly determined by the type of your works variable. That's why there is a difference.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 4 at 8:18









                    dprdpr

                    5,05811847




                    5,05811847












                    • But how is that different from the first assignment? if the signature of map is determined to by the type declaration of the assignment to return Optional<ArrayList<?>> then that means the return of the lambda would be of the type ArrayList<?> and since my lambda is (at least as far as I can tell) returning the concrete type ArrayList<String> something somewhere is casting it after the fact I guess?

                      – Carl Minden
                      Apr 4 at 8:24











                    • Yes, exactly. The difference is that the type of option is static where the return type of Optional.map is dynamically determined by the to be assigned variable.

                      – dpr
                      Apr 4 at 8:42

















                    • But how is that different from the first assignment? if the signature of map is determined to by the type declaration of the assignment to return Optional<ArrayList<?>> then that means the return of the lambda would be of the type ArrayList<?> and since my lambda is (at least as far as I can tell) returning the concrete type ArrayList<String> something somewhere is casting it after the fact I guess?

                      – Carl Minden
                      Apr 4 at 8:24











                    • Yes, exactly. The difference is that the type of option is static where the return type of Optional.map is dynamically determined by the to be assigned variable.

                      – dpr
                      Apr 4 at 8:42
















                    But how is that different from the first assignment? if the signature of map is determined to by the type declaration of the assignment to return Optional<ArrayList<?>> then that means the return of the lambda would be of the type ArrayList<?> and since my lambda is (at least as far as I can tell) returning the concrete type ArrayList<String> something somewhere is casting it after the fact I guess?

                    – Carl Minden
                    Apr 4 at 8:24





                    But how is that different from the first assignment? if the signature of map is determined to by the type declaration of the assignment to return Optional<ArrayList<?>> then that means the return of the lambda would be of the type ArrayList<?> and since my lambda is (at least as far as I can tell) returning the concrete type ArrayList<String> something somewhere is casting it after the fact I guess?

                    – Carl Minden
                    Apr 4 at 8:24













                    Yes, exactly. The difference is that the type of option is static where the return type of Optional.map is dynamically determined by the to be assigned variable.

                    – dpr
                    Apr 4 at 8:42





                    Yes, exactly. The difference is that the type of option is static where the return type of Optional.map is dynamically determined by the to be assigned variable.

                    – dpr
                    Apr 4 at 8:42

















                    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%2f55510951%2fwhy-does-optional-map-make-this-assignment-work%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

                    Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

                    Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

                    Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?