As Misha said, this feature will come with jdk9 in the form of a ifPresentOrElse method.
/**
* If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* @param action the action to be performed, if a value is present
* @param emptyAction the empty-based action to be performed, if no value is
* present
* @throws NullPointerException if a value is present and the given action
* is {@code null}, or no value is present and the given empty-based
* action is {@code null}.
* @since 9
*/
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
if (value != null) {
action.accept(value);
} else {
emptyAction.run();
}
}
.ifPresentOrElseis in the pipeline for jdk9: see mailing list discussionRunnableis indeed the zero-arg void functional interface. And the second argument of the new JDK 9Optional.ifPresentOrElseisRunnable.