title: Using Java 8 Optional for List of String as output
body: <p>I want to use Optional for a method which returns a List</p>
  
  <p>Lets say the function is </p>
  
  <pre><code>public Output getListOfSomething() {
      // In some cases there is nothing to return and hence it makes sense to have return 
      // type as Optional here
  }
  </code></pre>
  
  <p>Hence the function looks like : </p>
  
  <pre><code>public Optional&lt;List&lt;String&gt;&gt; getListOfSomething() {
      // return something only when there is some valid list
  }
  </code></pre>
  
  <p>Now I want to do something if the list is present so something like :</p>
  
  <pre><code>Optional&lt;List&lt;String&gt;&gt; listOfSomething = getListOfSomething();
  
  int size = 0;
  
  listOfSomething.ifPresent(size = listOfSomething.get().size());
  </code></pre>
  
  <p>I am new to Optional and have gone through the articles about Optional and it seems like this should work however am getting syntax error in my IDE : </p>
  
  <blockquote>
    <p>method ifPresent is not applicable for the arguments (void).</p>
  </blockquote>
  
  <p>I wanted to get some help from developers who might be more fluent with lamdas in java 8.</p>