Java filter list to generic type T
<p>How can I do the following (this doesn't compile):</p>

<pre><code>&lt;T&gt; List&lt;T&gt; getElementsOf() {
    return list.stream()
            .filter(x -&gt; x instanceof T)
            .map(x -&gt; (T) x)
            .collect(toList());
}
</code></pre>

<p>What would be example of usage? Ideally, it should be like <code>obj.&lt;Derived&gt;getElementsOf()</code>.</p>