FindFirst and orElse operators return different class type
<p>I´m using Stream with <code>findFirst()</code> which returns <code>Optional</code>, but also there´s a chance where my <code>Stream</code> maybe does not emit anything, so I added the <code>orElse</code> operator at the bottom.</p>
<p>The problem is that <code>orElse</code> does not return an optional but the class type that the optional <code>findFirst</code> operator wrap.</p>
<pre><code>        def optional = categories.categories.stream()
                .filter { category -&gt; category.name == selCategory }
                .map { category -&gt; loadUniqueIds.call(category) }
                .map { UUIDs -&gt; new JsonArray(UUIDs) }
                .findFirst().orElse{Optional.of(new JsonArray())}
        optional.get()
</code></pre>
<p>So I end up without the possibility of use <code>findFirst</code> and <code>orElse</code>.</p>
<p>What I´m doing wrong here?</p>
<p>Thanks</p>
<p>UPDATE:</p>
<p>I end up removing the <code>orElse</code> after the <code>findFirst</code> and check the optional later</p>
<pre><code>        optional.isPresent() ? optional.get(): new JsonArray()
</code></pre>
<p>Anyway if somebody has a better/elegant solution, please let me know.</p>
<p>Regards.</p>