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));