by oftenwrong · 6y ago · 0 comments
You generally would not want to call '.get()' without an 'isPresent()' check because it will throw a NoSuchElementException if the Optional is empty. This is a foreseeable case, so most would like to handle it somewhat explicitly.
Today, it is idiomatic to use '.map' to transform it:
Optional<String> foo = optionalCountry.map(Country::getName);
You can also throw a domain-specific exception from that point: Optional<String> foo = optionalCountry
.map(Country::getName)
.orElseThrow(...);