title: Java streams: Optional to stream
body: <p>This is my code:</p>
  
  <pre><code>Optional&lt;Application&gt; application = this.applicationDao.findById(id);
  </code></pre>
  
  <p>where, <code>Application</code> class is:</p>
  
  <pre><code>public class Application {
      private String code;    
      private Collection&lt;ApplicationQuote&gt; quotes;
  }
  </code></pre>
  
  <p>I need to create an stream from return <code>Optional&lt;Application&gt;</code> like this:</p>
  
  <pre><code>(app, quote-1) &gt; (app, quote-2) &gt; ... &gt; (app, quote-n)
  </code></pre>
  
  <p>Where each <code>quote-n</code> is inside returned <code>Optional&lt;Application&gt;.quotes</code>.</p>
  
  <p>I hope I've explained so well.</p>
  
  <p>Up to now, I've been able to write this code, but I don't feel confortable with that:</p>
  
  <pre><code>Optional&lt;Application&gt; application = this.applicationDao.findById(id);
      application.map(app -&gt; Pair.of(app, Optional.ofNullable(app.getQuotes())))
          .filter(quote -&gt; quote.getValue().isPresent())
          .map(quote -&gt; quote.getValue().get().stream().map(q -&gt; Pair.of(quote.getKey(), q)));
  </code></pre>