Java 8功能:如何计算对象的依赖进化列表?

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)

Hol*_*ger 7

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.