e.g.#1
[1, 1, 1, 2, 22, 35, 35, 120, 320]
==>> [3, 2, 22, 70, 120, 320]
note how repeated consecutive 1\'s and 35\'s are coalesced to 3 and 70
Why bother using streams when you can do it with a simple for loop and an if-else block?
List list = List.of(0, 0, 0, 0, 1, 1, 1, 0, 0, 0);
List result = new ArrayList<>();
Integer curr = list.get(0);
Integer sum = 0;
for(Integer i : list){
if(i.equals(curr)){
sum += i;
}
else{
result.add(sum);
sum = i;
curr = i;
}
}
result.add(sum);
System.out.println(result);