java - Can Stream's SKIP method making an infinite stream finite?
问题描述
"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?
解决方案
"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.
推荐阅读
- python - Travis CI Python 在 osx 上构建失败 - “未安装 2.7”
- ssl - How to use private key and public key with chain in a mutual ssl authentication
- android - How should I set the scaleType of an ImageView, when I use Glide
- javascript - Javascript:通过遍历数组来检查复选框是否被选中
- angular5 - 当子路由更改时,Angular 5 Parent 会重新加载
- javascript - javascript banner - close button doesn't work corectly
- c# - 如何使用 AutoMapper 进行嵌套映射?
- postgresql - 在 postgresql 的子查询中使用执行
- php - 由于自定义字段中的撇号,查询后过滤不起作用
- r - 构建 conda 包时未定义的 cran_mirror