Can Stream's SKIP method making an infinite stream finite?

Arun S R :

"The limit() and skip() methods make a Stream smaller. They could make a finite stream smaller, or they could make a finite stream out of an infinite stream. The method signatures are shown here:

Stream<T> limit(int maxSize)
Stream<T> skip(int n)

The following code c...."

The above is an excerpt from OCP java 8 book. When it said "could make a finite stream out of an infinite stream", did they mean it in both the methods together or alone? I can imagine how limit() would make an infinite stream smaller, but how skip() alone would accomplish that? Is there a way or the wording in the documentation needs to be clearer?

ernest_k :

"could make a finite stream out of an infinite stream" is surely applicable to limit() only, not to to skip().

skip is like taking a cup of water from the ocean and wondering "how much water is left in the ocean?", whereas limit is like taking the same cup of water from it and wondering "how much water did I take from the ocean?"

If the stream is infinite, then skipping a number of elements will still leave you with an infinite stream...

Stream.iterate(0L, i -> i + 1).skip(100).forEach(System.out::println);

This will theoretically run forever. So chances are it's just a minor inaccuracy that escaped the reviewer of the book.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=87374&siteId=1