Return the N last elements in Java Streams
<p>This question is just for fun.</p>

<p>I have this method:</p>

<pre><code>private static String getBaseDomain(String fullDomain) {
    // we take the base domain from the usual xxx.yyy.basedomain.tld: we 
    // want only the last 2 elements: basedomain.tld
    List&lt;String&gt; elements = Arrays.asList(fullDomain.split("\\."));
    if( elements.size() &gt; 2){
        elements = elements.subList(elements.size()-2, elements.size());
    }
    return String.join(".", elements);
}
</code></pre>

<p>And I'm wondering how to get the same result using the java stream API (actually, I'm wondering which method would be the most resource-efficient).</p>

<p>I can't figure how to get only the last 2 elements from a stream: <code>limit(2)</code> will give me the <em>first</em> two, and for <code>skip(XXX)</code> I don't know how to extract the size of the stream "inline".</p>

<p>Can you tell me how you would do?</p>