I have a list of numbers like this:
[ 0, 1, 2, 3, 4, 5, 6, 7 ]
How to sum up every N (let's assume 2) elements in an elegant way and transform the list into:
[ 1, 5, 9, 13 ]
edit: I came up with the following solution:
List<Double> input = Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0);
List<Double> output = new ArrayList<>();
int N = 2;
IntStream.range(0, (input.size() + N - 1) / N)
.mapToObj(i -> input.subList(i * N, Math.min(N * (i + 1), input.size())))
.mapToDouble(l -> l.stream().mapToDouble(Double::doubleValue).sum())
.forEach(output::add);
System.out.println(output);
It works, but I'm still looking for a more readable and simple one.