如何避免Exception在尝试从 anOptional或在使用时显式地抛出一个值Optional.get?
目前,这可能通过 API 得到保护orElseThrow:
// exception could be replaced with any other
Integer opt = anyOddInStream.orElseThrow(
() -> new NoSuchElementException("No value present"));
然而,get在尝试访问类似
// the following not only just fails but throws an exception as well
Integer previous = anyOddInStream.get();
如果一个人想确保其中Optional一个有一个值,如果没有,他们不想继续null向前传播怎么办?