Java8 generic puzzle

HenryNguyen :

I'm using Java 1.8.0_151 and there is some code that doesn't compile and I don't understand:

Optional optional = Optional.of("dummy"); 
Optional<Boolean> result1 = optional.map(obj -> true);     // works fine
boolean result2 = result1.orElse(false);                   // works fine
boolean result3 = optional.map(obj -> true).orElse(false); // compilation error: Incompatible types: required boolean, found object
Object result4 = optional.map(obj -> true).orElse(false); // works fine

why it works fine on result1 but gives compilation error on result3?
Additional info:

  • In the first line, when I change Optional to Optional<String>, result3 is also able to compile
  • When I break result3 into 2 lines: like result1 and result2, result3 is able to compile
Eugene :

Once you lose the type safety - it is lost for chained calls as-well. That is Optional<Object> != Optional. So when you do

 Optional optional = Optional.of("dummy");
 optional.map()

The map can only accept a raw Function and nothing else, which will return an Object obviously.

The correct way is to add type information :

Optional<String> optional = Optional.of("dummy");

Or you can cast unsafely:

boolean result3 = (boolean) optional.map(obj -> true).orElse(false)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=459665&siteId=1