Java 8 Optional throw () throw NPE map with function reference, but not with full lambda syntax

Is this a bug or function?

Below with NPE

Function<String, String> f = null; Optional.<String>empty().map(f).orElse(null); 

But not

 Function<String, String> f = null; Optional.<String>empty().map(value -> f.apply(value)).orElse(null); 

IntelliJ, for example, will suggest replacing the second expression with the first as equivalent, which has made sense to me so far.

The reason for this behavior is the implementation of Optional#map() :

 public<U> Optional<U> map(Function<? super T, ? extends U> mapper) { // check happens before the branching Objects.requireNonNull(mapper); if (!isPresent()) return empty(); else { return Optional.ofNullable(mapper.apply(value)); } } 

Instead, if map() was implemented using:

 public<U> Optional<U> map(Function<? super T, ? extends U> mapper) { if (!isPresent()) return empty(); else { // the check happens only when relevant Objects.requireNonNull(mapper); return Optional.ofNullable(mapper.apply(value)); } } 

we get consistent behavior between two 2 fragments. Any reason map() not the second implementation?

+5
source share
1 answer

This is not a mistake, because it is clearly documented. Quoting Optional.map(mapper) Javadoc:

If the value is present, apply the provided mapping function to it [...]
...

Throws:

NullPointerException - if the display function is null

So, the map method always throws a NullPointerException , regardless of whether or not this mapper set to null : this explains the exception in the first case. However, mapper only applies if there is a value: this explains why in the second case there is no exception.

+7
source

Source: https://habr.com/ru/post/1241141/


All Articles