Phi*_*das 4 java functional-programming java-8 java-stream
我想以函数方式编写以下代码,包含streams和lambdas:
Thing thing = new Thing();
List<Thing> things = new ArrayList<>();
things.add(thing);
for (int i = 0; i < 100; i++) {
thing = computeNextValue(thing);
things.add(thing);
}
Run Code Online (Sandbox Code Playgroud)
阻碍这个......
Supplier<Thing> initial = Thing::new;
List<Things> things = IntStream.range(0, 100).???(...).collect(toList());
Run Code Online (Sandbox Code Playgroud)
List<Thing> things = Stream.iterate(new Thing(), t->computeNextValue(t))
.limit(100).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
您还可以使用方法参考t->computeNextValue(t).
如果computeNextValue是一个static方法代替t->computeNextValue(t)用ContainingClass::computeNextValue,否则使用this::computeNextValue.
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |