title: Java Optional "while not null" equivalent
body: <p>Let us assume we start out with this code</p>
  
  <pre><code>while ((element = getNextElement()) != null) {
     // do something
  }
  </code></pre>
  
  <p>and want to rewrite it so that getNextElement() returns an <code>Optional</code> as opposed to something that might be <code>null</code>.</p>
  
  <p>What would be the preferred way to do that? Something like</p>
  
  <pre><code>while ((element = getNextElement()) &amp;&amp; element.isPresent()) {
  
  }
  </code></pre>
  
  <p>... or is there a better way?</p>
  
  <p>There is also</p>
  
  <pre><code>for (Optional&lt;ElementType&gt; element = getNextElement(); element.isPresent(); element = getNextElement()) {
     // do something
  }
  </code></pre>
  
  <p>Are there other/better options (pun intended, I suppose) for this?</p>