title: How to properly use the combination of .map and .orElseGet within stream API?
body: <p>I have the following code:</p>
  <pre><code>private List&lt;User&gt; findUsers(...) {
  ...
  
      return usersData.stream() //userData is another list of objects
          .findFirst()
          .map(this::getCode)
          .map(code-&gt; {
              if (...) {
                  Optional&lt;AnotherObject&gt; anotherObject = getAnotherObject();
                  return anotherObject.map(userService::getUsersFromAnotherObject) // getUsersFromAnotherObject returns List&lt;User&gt; but the whole line returns Optional of List&lt;User&gt;
              } else {
                  ...
                  return null;
              }
          }).orElseGet(() -&gt; findXYZ(...));
  }
  </code></pre>
  <p>which does not compile and says:<br>
  &quot;Bad return type in lambda expression: <code>List&lt;User&gt;</code> cannot be converted to <code>Optional&lt;List&lt;User&gt;&gt;</code>&quot;<br>
  even though findXYZ and all other if/else statements return in fact type List.</p>
  <p>Could anybody explain to me what is wrong with the code?</p>
  <p>EDIT:
  Sorry, I noticed that one of if statements is actually returning the Optional of List</p>
  <p>If anybody is interested, I solved it simply editing the first &quot;if&quot; to:</p>
  <pre><code>return userService.getUsersFromAnotherObject(anotherObject.orElse(null));
  </code></pre>