title: Java 8 Optional get if present
body: <p>I have a class structure like this:</p>
  
  <pre><code>public class Foo {
      private FooB fooB;
  
      public Optional&lt;FooB&gt; getFooB() {
          return Optional.ofNullable(fooB);
      }
  }
  
  public class FooB {
      private String a;
      private String b;
  
      public String getA() {
          return a;
      }
  
      public String getB() {
          return b;
      }
  }
  </code></pre>
  
  <p>What I want to do is something like this:</p>
  
  <pre><code>main() {
      //initialize a Foo object called foo
      String a = foo.getFooB().ifPresent(FooB::getA);
  }
  </code></pre>
  
  <p>Basically if the foo object returns FooB as present then get the field <code>String a</code> from <code>FooB</code> and store it in the local variable <code>String a</code>. How can I do this elegantly in 1 line?</p>