title: Get value from one Optional or another
body: <p>I have two <code>java.util.Optional</code> instances and I want to get an <code>Optional</code> that either:</p>
  
  <ul>
  <li>Has the value of the first Optional, if it has a value.</li>
  <li>Has the value of the second Optional, if it has a value.</li>
  <li>Is empty of neither Optional has a value.</li>
  </ul>
  
  <p>Is there a straight-forward way to do that, i.e. is there already some API to do that?</p>
  
  <p>The following expressions will do that, but I have to mention the first optional twice:</p>
  
  <pre><code>firstOptional.isPresent() ? firstOptional : secondOptional
  </code></pre>
  
  <p>This is exactly what <code>com.google.common.base.Optional.or()</code> does, but that method is not present in Java 8's API.</p>
  
  <hr>
  
  <p>The accepted answer by <em>aioobe</em> lists a few alternative approaches to overcome this omission of the <code>Optional</code> API right where such a value has to be computed (which answers my question). I've now opted to add a utility function to my codebase:</p>
  
  <pre><code>public static &lt;T&gt; Optional&lt;T&gt; or(Optional&lt;T&gt; a, Optional&lt;T&gt; b) {
      if (a.isPresent())
          return a;
      else
          return b;
  }
  </code></pre>