bla*_*ter -3 java java-8 java-stream
这个Java代码可以用Stream API重写吗?
while (true) {
...
if (isTrue()) break;
}
private boolean isTrue() {
...
Run Code Online (Sandbox Code Playgroud)
这是可怕的代码,没有理由使用 this。流是常规循环的附加工具,它们不是循环的替代品for,尤其是while循环。
// If you use this code seriously somewhere, I will find you
IntStream.generate(() -> 0)
.peek(i -> {
// Any custom logic
System.out.println(i);
})
.noneMatch(i -> isTrue());
Run Code Online (Sandbox Code Playgroud)
代码无限地生成零,在流中查看以执行自定义逻辑,然后在noneMatch计算为时停止true。
以上相当于问题中的代码,可以更简洁地写成
do {
// custom logic
} while(!isTrue());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
514 次 |
| 最近记录: |