Return the N last elements in Java Streams

java, java-8, java-stream, split, string

Solution

You could use `skip`:

elements.stream().skip(elements.size() - 2)

From the API:

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.

Probably useless example:

// a list made of a, b, c and d
List<String> l = Arrays.asList("a", "b", "c", "d");

// prints c and d
l.stream().skip(l.size() - 2).forEach(System.out::println);

Probably useless note:

As mentioned by a few, this only works if you have a size to work with, i.e. if you're streaming from a collection.

Quoting Nicolas, a stream doesn't have a size.

Problem

This question is just for fun. I have this method: ``` 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<String> elements = Arrays.asList(fullDomain.split("\\.")); if( elements.size() > 2){ elements = elements.subList(elements.size()-2, elements.size()); } return String.join(".", elements); } ``` 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). I can't figure how to get only the last 2 elements from a stream: `limit(2)` will give me the first two, and for `skip(XXX)` I don't know how to extract the size of the stream "inline". Can you tell me how you would do?

Original source

Related problems