Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Optional.map make this assignment work?

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?

like image 793
Carl Minden Avatar asked Apr 04 '19 08:04

Carl Minden


People also ask

What does map do in optional?

The method Optional. map(Function) applies the given function to the optional value if it is present and returns an optional with the result or empty optional.

What does optional map return?

Like Stream, Optional#map will return a value wrapped by a Optional.

Why do we use optional?

The main design goal of Optional is to provide a means for a function returning a value to indicate the absence of a return value. See this discussion. This allows the caller to continue a chain of fluent method calls.

Why should I use optional Java?

So, to overcome this, Java 8 has introduced a new class Optional in java. util package. It can help in writing a neat code without using too many null checks. By using Optional, we can specify alternate values to return or alternate code to run.


1 Answers

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) 
like image 121
Eran Avatar answered Sep 23 '22 18:09

Eran