Reputation: 39
I have the following list :
INPUT :: 4 5 8 -11 9 5 -7 4 6 -6 -8 -11 80 -32 -56 -15 5 -49
OUTPUT :: 4 9 17 6 15 20 13 17 23 17 9 -2 78 46 -10 -25 -20 -69
I need to calculate cumulative sum - List meaning
T(n) = T(n) + T(n-1) for n >0;
and
T(0) = T(0)
I want to calculate that with Java stream API so that I can implement it with Spark for big data calculation. I am naive in Java Streams I have tried several expressions bt none of them is working the equivalent stuctured code should be like :
int[] numbers = {4, 5, 8, -11, 9, 5, -7, 4, 6,-6, -8, -11, 80, -32, -56, -15, 5, -49};
int temp = 0;
for (int i = 0 ; i < numbers.length ; i++) {
temp = temp + numbers[i];
numbers[i] = temp;
}
Upvotes: 0
Views: 1261
Reputation: 1
You can try using a custom collector.
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> cumulatives = integers.stream().collect(CumulativeAdd.collector());
}
private static final class CumulativeAdd {
List<Integer> retArray= new ArrayList<>();
int sum = 0;
public void accept(Integer num) {
sum +=num;
retArray.add(sum);
}
public CumulativeAdd combine(CumulativeAdd other) {
throw new UnsupportedOperationException("Parallel Stream not supported");
}
public List<Integer> finish() {
return retArray;
}
public static Collector<Integer, ?, List<Integer>> collector() {
return Collector.of(CumulativeAdd::new, CumulativeAdd::accept, CumulativeAdd::combine, CumulativeAdd::finish);
}
}
Upvotes: 0
Reputation: 40034
Here are two ways of doing it.
The first is very inefficient as it basically uses nested loops to accumulate the values. The first IntStream
specfies the range of values and the nested IntStream creates a variable range and sums up the values from 0 to the end of that range.
int[] result1 = IntStream.range(0, vals.length).map(
i -> IntStream.rangeClosed(0, i).map(k->vals[k]).reduce(0, (a, b) -> a + b))
.toArray();
This one is more in line with a more conventional method. Stream a single array of 0 and then use that to accumulate a running sum of the values.
int[] result2 = Stream.of(new int[] { 0 })
.flatMapToInt(k -> IntStream.of(vals).map(v -> {
k[0] += v;
return k[0];
})).toArray();
System.out.println(Arrays.toString(result1));
System.out.println(Arrays.toString(result2));
Both print
[4, 9, 17, 6, 15, 20, 13, 17, 23, 17, 9, -2, 78, 46, -10, -25, -20, -69]
[4, 9, 17, 6, 15, 20, 13, 17, 23, 17, 9, -2, 78, 46, -10, -25, -20, -69]
But you simply can't do any better than this.
for (int i = 1; i < vals.length; i++) {
vals[i] += vals[i-1];
}
Bottom line is to stick with what you have.
Upvotes: 0
Reputation:
Try this.
int[] a = {4, 5, 8, -11, 9, 5, -7, 4, 6, -6, -8, -11, 80, -32, -56, -15, 5, -49};
Arrays.parallelPrefix(a, (x, y) -> x + y);
System.out.println(Arrays.toString(a));
output:
[4, 9, 17, 6, 15, 20, 13, 17, 23, 17, 9, -2, 78, 46, -10, -25, -20, -69]
Upvotes: 4