迭代构建流的最佳方法是什么?

What is the best way to build a stream iteratively?

假设我们有 an algorithm 在循环中按顺序生成项目(每次迭代一个项目),我们想将此算法放入一个方法中,该方法 returns 这些项目的流。

哪种方法最好;这个?

Stream<Cell> streamCellsTouchingRay(Point2D fromPoint, Vector2D direction){
    // ...
    Stream<Cell> stream = Stream.of(/* 1st item */);
    while(/* ... */){
        // ...
        stream = Stream.concat(stream, /* i'th item */);
    }
}

...还是这个?

Stream<Cell> streamCellsTouchingRay(Point2D fromPoint, Vector2D direction){
    // ...
    ArrayList<Cell> cells = new ArrayList<>(/* required capacity is known */);
    cells.add(/* 1st item */);
    while(/* ... */){
        // ...
        cells.add(/* i'th item */);
    }
    return cells.stream();
}

...或者完全是另一种方法?

完全是另一种方法:使用Stream.Builder

Stream<Cell> streamCellsTouchingRay(Point2D fromPoint, Vector2D direction){
    // ...
    Stream.Builder<Cell> cells = Stream.builder();
    cells.add(/* 1st item */);
    while(/* ... */){
        // ...
        cells.add(/* i'th item */);
    }
    return cells.build();
}
  • 使用 ArrayList 将在您最终获得流时产生一些复制开销。

  • 使用 Stream.concat() 时,文档指出在连接多个项目时应谨慎使用:

Use caution when constructing streams from repeated concatenation. Accessing an element of a deeply concatenated stream can result in deep call chains, or even WhosebugException.

似乎是最好的,如果你连接许多项目。