Suppose we have an algorithm that generates items sequentially in a loop (one item per iteration) and we want to put this algorithm into a method that returns a stream of those items.
Which approach is best; this one?
Stream<Cell> streamCellsTouchingRay(Point2D fromPoint, Vector2D direction){ // ... Stream<Cell> stream = Stream.of(/* 1st item */); while(/* ... */){ // ... stream = Stream.concat(stream, /* i'th item */); } } ...or this one?
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(); } ...or another approach entirely?