Optional Consumer for ifNotPresent As Optional have only ifPresent and not have another function to be run in false case so I did the below class for that, please check it and give me your feedback:

public class OptionalConsumer&lt;T&gt; {
private Optional&lt;T&gt; optional;

private OptionalConsumer(Optional&lt;T&gt; optional) {
    this.optional = optional;
}

public static &lt;T&gt; OptionalConsumer&lt;T&gt; of(Optional&lt;T&gt; optional) {
    return new OptionalConsumer&lt;&gt;(optional);
}

public OptionalConsumer&lt;T&gt; ifPresent(Consumer&lt;T&gt; c) {
    optional.ifPresent(c);
    return this;
}

public OptionalConsumer&lt;T&gt; ifNotPresent(Runnable r) {
    if (!optional.isPresent())
        r.run();
    return this;
}


}
Then :

Optional&lt;Any&gt; o = Optional.of(...);
OptionalConsumer.of(o).ifPresent(s -&gt;System.out.println("isPresent "+s))
        .ifNotPresent(() -&gt; System.out.println("! isPresent"));
Two things:


Your class only provides a if-else statement, really. If it would also provide all the features in the existing java.util.Optional, it would have been slightly more useful.
private Optional&lt;T&gt; optional; should definitely be marked with final


Okay then, three things:


Your factory method takes an existing Optional&lt;T&gt;, but you don't provide a getter or anything for the Optional&lt;T&gt;, which means that you could also provide an ordinary OptionalConsumer&lt;T&gt; ofNullable(T value)