Here's how you can implement the missing peek
method for Optional
:
<T> UnaryOperator<T> peek(Consumer<T> c) {
return x -> {
c.accept(x);
return x;
};
}
Usage:
Optional.ofNullable(key)
.map(Person::get)
.map(peek(this::printName))
.map(peek(this::printAddress));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…