java 8 getter with default value on null object or attribute
<p>I'm not quite proficient in Java, so I would like to know if I'm doing right here.</p>

<p>As the title states, the code below is for getting some default reply when fetching an attribute on a null object, or an object with a null attribute value.</p>

<p>this seems to work, but I'd be thankful if someone could tell me if I'm looking at the problem the right way.</p>

<pre><code>import java.util.Optional;
import java.util.function.Function;

public class ReplyOnNullPointer {

    static class C{
        private String a;
        public C(String a){this.a=a;}
        public String getAtt(){return a;}
    }


    private static&lt;I,R&gt; R getterWithDefault(I o,Function&lt;? super I,R&gt; mapper, R orDefault){
        return Optional.ofNullable(o).flatMap(x-&gt;Optional.ofNullable(mapper.apply(x))).orElse(orDefault);
    }

    public static void main(String[] args) {
        C o1 = null;
        C o2 = new C(null);
        C o3 = new C("attribut");

        System.out.println("o3 : "+o3);
        System.out.println("o3.att : "+o3.getAtt());
        System.out.println();

        System.out.println("o2 : "+o2);
        System.out.println("o2.att : "+o2.getAtt());
        System.out.println("o2.att : "+Optional.of(o2).flatMap(x-&gt;Optional.ofNullable(x.getAtt())).orElse("&lt;nope&gt;"));
        System.out.println();

        System.out.println("o1 : "+o1);
        System.out.println("o1.att : "+Optional.ofNullable(o1).flatMap(x-&gt;Optional.ofNullable(x.getAtt())).orElse("&lt;nope&gt;"));

        System.out.println();
        System.out.println("o1.att : "+getterWithDefault(o1,C::getAtt,"&lt;nope&gt;"));
        System.out.println("o2.att : "+getterWithDefault(o2,C::getAtt,"&lt;nope&gt;"));
        System.out.println("o3.att : "+getterWithDefault(o3,C::getAtt,"&lt;nope&gt;"));

    }
}
</code></pre>

<p>the ouput looks like this :</p>

<pre><code>o3 : ReplyOnNullPointer$C@15db9742
o3.att : attribut

o2 : ReplyOnNullPointer$C@6d06d69c
o2.att : null
o2.att : &lt;nope&gt;

o1 : null
o1.att : &lt;nope&gt;

o1.att : &lt;nope&gt;
o2.att : &lt;nope&gt;
o3.att : attribut
</code></pre>

<p>thanks for your corrections/opinions/advices</p>