How to implement the Elvis operator in Java 8?
问题 I have the classic "Elvis operator" case, where I'm calling methods that each may return null and chaining them together: thing?:nullableMethod1(a)?:nullableMethod2(b)?:nullableMethod3() In Java 8, the most faithful implementation I've found is something like this: return Optional.ofNullable(thing) .flatMap(x -> Optional.ofNullable(x.nullableMethod1(a))) .flatMap(y -> Optional.ofNullable(y.nullableMethod2(b))) .flatMap(z -> Optional.ofNullable(z.nullableMethod3())) I wish that Java's Optional