Can the skip method of Java – stream make the infinite stream limited?

"The limit() and skip() methods make the stream smaller. They can make the finite flow smaller, or they can generate a finite flow from an infinite flow. The method signature is as follows:

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 says "a finite flow can be generated from an infinite flow", do they represent them together or separately? I can imagine how limit () can make infinite flow small, but how can skip () be implemented alone? Is there any way or the wording in the document needs to be clearer?

Solution

翻译错误 TIMEOUT

Skipping is like taking a glass of water from the ocean and wondering "how much water is left in the ocean?", Restriction is like taking the same water from it, wondering "how much water have I taken out of the ocean“

If the flow is infinite, skipping some elements will still bring you infinite flow

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

In theory, it will run forever So it's probably just a small inaccuracy that escaped the reviewers of the book

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>