How can you avoid explicit generation Exceptionwhen trying to get a value from Optionalor when using ? Optional.get
This is currently possible orElseThrowAPI or orElseThrowas:
// exception could be replaced with any other
Integer opt = anyOddInStream.orElseThrow(
() -> new NoSuchElementException("No value present"));
and yet, the immediate implementation is getdisappointing (when someone might not want to explicitly throw an exception) when trying to access something like
Integer previous = anyOddInStream.get();
What if someone wants to make sure that Optionaleither matters, and if not, they don’t want to continue to spread nullforward?
source
share