Why does Optional.map make this assignment work?Stream.findFirst different than Optional.of?Does a finally...

SSH "lag" in LAN on some machines, mixed distros

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

Is it legal for company to use my work email to pretend I still work there?

Is it canonical bit space?

How could indestructible materials be used in power generation?

Fully-Firstable Anagram Sets

Should I tell management that I intend to leave due to bad software development practices?

In a Spin are Both Wings Stalled?

Stopping power of mountain vs road bike

What is the PIE reconstruction for word-initial alpha with rough breathing?

Can a virus destroy the BIOS of a modern computer?

Took a trip to a parallel universe, need help deciphering

Is "remove commented out code" correct English?

1960's book about a plague that kills all white people

How do I write bicross product symbols in latex?

Why is Collection not simply treated as Collection<?>

Etiquette around loan refinance - decision is going to cost first broker a lot of money

Why does Arabsat 6A need a Falcon Heavy to launch

I Accidentally Deleted a Stock Terminal Theme

What exploit are these user agents trying to use?

Can I ask the recruiters in my resume to put the reason why I am rejected?

How to draw the figure with four pentagons?

Why is consensus so controversial in Britain?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?



Why does Optional.map make this assignment work?


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;
}







24















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































    24















    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



























      24












      24








      24


      2






      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 16 hours ago









      Eran

      291k37481564




      291k37481564










      asked 17 hours ago









      Carl MindenCarl Minden

      373218




      373218
























          5 Answers
          5






          active

          oldest

          votes


















          15














          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<>(map.get()). So you can replace your last assignment with:



          Optional<ArrayList<?>> works = new Optional<>(map.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
            16 hours ago






          • 2





            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
            14 hours ago













          • Should be option.get(), not map.get()?

            – Bergi
            6 hours ago



















          6














          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































            1














            The line:



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



            is equal to:



            Optional<ArrayList<?>> works = option.map(new Function<ArrayList<String>, ArrayList<?>>() {
            @Override
            public ArrayList<?> apply(ArrayList<String> list) {
            return list;
            }
            });


            You can think of it as:



            ArrayList<String> strings = new ArrayList<>();
            ArrayList<?> list = strings;
            Optional<ArrayList<?>> works = Optional.ofNullable(list);





            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
                16 hours ago



















              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
                16 hours ago











              • 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
                16 hours 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%2f55510951%2fwhy-does-optional-map-make-this-assignment-work%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              15














              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<>(map.get()). So you can replace your last assignment with:



              Optional<ArrayList<?>> works = new Optional<>(map.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
                16 hours ago






              • 2





                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
                14 hours ago













              • Should be option.get(), not map.get()?

                – Bergi
                6 hours ago
















              15














              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<>(map.get()). So you can replace your last assignment with:



              Optional<ArrayList<?>> works = new Optional<>(map.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
                16 hours ago






              • 2





                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
                14 hours ago













              • Should be option.get(), not map.get()?

                – Bergi
                6 hours ago














              15












              15








              15







              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<>(map.get()). So you can replace your last assignment with:



              Optional<ArrayList<?>> works = new Optional<>(map.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<>(map.get()). So you can replace your last assignment with:



              Optional<ArrayList<?>> works = new Optional<>(map.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 14 hours ago









              Holger

              171k23247467




              171k23247467










              answered 16 hours ago









              EranEran

              291k37481564




              291k37481564













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

                – Carl Minden
                16 hours ago






              • 2





                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
                14 hours ago













              • Should be option.get(), not map.get()?

                – Bergi
                6 hours ago



















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

                – Carl Minden
                16 hours ago






              • 2





                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
                14 hours ago













              • Should be option.get(), not map.get()?

                – Bergi
                6 hours ago

















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

              – Carl Minden
              16 hours ago





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

              – Carl Minden
              16 hours ago




              2




              2





              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
              14 hours ago







              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
              14 hours ago















              Should be option.get(), not map.get()?

              – Bergi
              6 hours ago





              Should be option.get(), not map.get()?

              – Bergi
              6 hours ago













              6














              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




























                6














                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


























                  6












                  6








                  6







                  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 15 hours ago









                  EugeneEugene

                  72.2k9103173




                  72.2k9103173























                      1














                      The line:



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



                      is equal to:



                      Optional<ArrayList<?>> works = option.map(new Function<ArrayList<String>, ArrayList<?>>() {
                      @Override
                      public ArrayList<?> apply(ArrayList<String> list) {
                      return list;
                      }
                      });


                      You can think of it as:



                      ArrayList<String> strings = new ArrayList<>();
                      ArrayList<?> list = strings;
                      Optional<ArrayList<?>> works = Optional.ofNullable(list);





                      share|improve this answer






























                        1














                        The line:



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



                        is equal to:



                        Optional<ArrayList<?>> works = option.map(new Function<ArrayList<String>, ArrayList<?>>() {
                        @Override
                        public ArrayList<?> apply(ArrayList<String> list) {
                        return list;
                        }
                        });


                        You can think of it as:



                        ArrayList<String> strings = new ArrayList<>();
                        ArrayList<?> list = strings;
                        Optional<ArrayList<?>> works = Optional.ofNullable(list);





                        share|improve this answer




























                          1












                          1








                          1







                          The line:



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



                          is equal to:



                          Optional<ArrayList<?>> works = option.map(new Function<ArrayList<String>, ArrayList<?>>() {
                          @Override
                          public ArrayList<?> apply(ArrayList<String> list) {
                          return list;
                          }
                          });


                          You can think of it as:



                          ArrayList<String> strings = new ArrayList<>();
                          ArrayList<?> list = strings;
                          Optional<ArrayList<?>> works = Optional.ofNullable(list);





                          share|improve this answer















                          The line:



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



                          is equal to:



                          Optional<ArrayList<?>> works = option.map(new Function<ArrayList<String>, ArrayList<?>>() {
                          @Override
                          public ArrayList<?> apply(ArrayList<String> list) {
                          return list;
                          }
                          });


                          You can think of it as:



                          ArrayList<String> strings = new ArrayList<>();
                          ArrayList<?> list = strings;
                          Optional<ArrayList<?>> works = Optional.ofNullable(list);






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 4 hours ago

























                          answered 10 hours ago









                          OleksandrOleksandr

                          9,59044272




                          9,59044272























                              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
                                16 hours ago
















                              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
                                16 hours ago














                              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 16 hours ago









                              Lutz HornLutz Horn

                              65612




                              65612













                              • 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
                                16 hours ago



















                              • 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
                                16 hours ago

















                              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
                              16 hours ago





                              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
                              16 hours ago











                              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
                                16 hours ago











                              • 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
                                16 hours ago
















                              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
                                16 hours ago











                              • 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
                                16 hours ago














                              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 16 hours ago









                              dprdpr

                              4,93011647




                              4,93011647













                              • 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
                                16 hours ago











                              • 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
                                16 hours ago



















                              • 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
                                16 hours ago











                              • 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
                                16 hours ago

















                              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
                              16 hours ago





                              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
                              16 hours ago













                              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
                              16 hours ago





                              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
                              16 hours 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.




                              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

                              Paper upload error, “Upload failed: The top margin is 0.715 in on page 3, which is below the required...

                              Emraan Hashmi Filmografia | Linki zewnętrzne | Menu nawigacyjneGulshan GroverGulshan...

                              How can I write this formula?newline and italics added with leqWhy does widehat behave differently if I...