title: Proper usage of Optional.ifPresent()
body: <p>I am trying to understand the <code>ifPresent()</code> method of the <code>Optional</code> API in Java 8.</p>
  
  <p>I have simple logic:</p>
  
  <pre><code>Optional&lt;User&gt; user=...
  user.ifPresent(doSomethingWithUser(user.get()));
  </code></pre>
  
  <p>But this results in a compilation error: </p>
  
  <pre><code>ifPresent(java.util.functionError:(186, 74) java: 'void' type not allowed here)
  </code></pre>
  
  <p>Of course I can do something like this:</p>
  
  <pre><code>if(user.isPresent())
  {
    doSomethingWithUser(user.get());
  }
  </code></pre>
  
  <p>But this is exactly like a cluttered <code>null</code> check.</p>
  
  <p>If I change the code into this:</p>
  
  <pre><code> user.ifPresent(new Consumer&lt;User&gt;() {
              @Override public void accept(User user) {
                  doSomethingWithUser(user.get());
              }
          });
  </code></pre>
  
  <p>The code is getting dirtier, which makes me think of going back to the old <code>null</code> check.</p>
  
  <p>Any ideas?</p>