已经提出了几种情况,其中使用可选保留对先前映射对象的引用会很酷。
// Normally the following is a good candidate for Optional fluent API (imagine also all the null checks that could be necessary)
A a = new A();
B b = calculateB(a);
C c = calculateC(b)
D d = calculateD(c)
E e = calculateE(d)
// As
E e = Optional.ofNullable(a)
.map(this::calculateB)
...
// But the following can't be expressed with Optional API
A a = new A();
B b = calculateB(a);
C c = calculateC(a, b)
D d = calculateD(a, …Run Code Online (Sandbox Code Playgroud)