Have this:
List<Integer> list = new LinkedList<>();
for (int i = 0; i < upperBound; i += step) {
list.add(i);
}
How can I replace it with functional styled streams?
thanks
Have this:
List<Integer> list = new LinkedList<>();
for (int i = 0; i < upperBound; i += step) {
list.add(i);
}
How can I replace it with functional styled streams?
thanks
Your loop looks fine.
If you absolutely want to use a stream, you can create an IntStream
and box it into a List
. For example:
int elementCount = upperBound / step;
if (upperBound % step != 0) elementCount++;
List<Integer> list = IntStream.range(0, elementCount)
.map(i -> i * step)
.boxed()
.collect(toCollection(LinkedList::new));
Note that defining the upper bound is not straightforward.