Thank you for your responses. I just want to give detailed information on why it worked on resutl1 , but not result3 on:
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
on result1 , the optional variable was raw, so optional.map(obj -> true) returned an optional raw type.
When I declared Optional<Boolean> result1 , the optional raw type was automatically cast to Optional<Boolean>
optional.map(obj -> true).orElse(false); failed because the optional object of the raw type cannot call .orElse(false)
Another example of this raw type behavior is:
List li = new ArrayList<String>(); List<String> li1 = li; List<Integer> li2 = li; List<Map> li3 = li; li1.add("WWW"); li2.add(444); li3.add(new HashMap());
will work fine for all scripts, the li object will contain String, Integer and HashMap (). li1, li2, li3 was automatically added to the non-raw type.
HenryNguyen
source share