Carl Minden
Carl Minden

Reputation: 842

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?

Upvotes: 36

Views: 2240

Answers (4)

Eran
Eran

Reputation: 393811

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)

Upvotes: 26

Eugene
Eugene

Reputation: 120848

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

Upvotes: 10

dpr
dpr

Reputation: 10964

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.

Upvotes: 0

user11044402
user11044402

Reputation:

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.

Upvotes: 0

Related Questions