int sum = a;
int pow = 1;
for (int i = 0; i < n ; i++) {
sum += b*pow;
System.out.print(sum+" ");
pow *= 2;
}
In Java-8 on using Stream
gives errors for sum and pow variable that the variable should be final.
int sum = a;
int pow = 1;
for (int i = 0; i < n ; i++) {
sum += b*pow;
System.out.print(sum+" ");
pow *= 2;
}
In Java-8 on using Stream
gives errors for sum and pow variable that the variable should be final.
You can use the generated Stream using
IntStream
and process the numbers in the same way. Note thatMath::pow
returnsDouble
therefore the pipeline results inDoubleStream
.The only disadvantage is no consumer is available during the reducing, therefore you have to amend it a bit:
To answer this:
One of the java-stream conditions is that the variables used within lambda expressions must be either final or effectively-final, therefore the mutable operations you used in the for-loop are not allowed and have to be replaced with mapping feature of Streams. This issue is nicely explained at http://ilkinulas.github.io.
Remember, you cannot use java-stream the same way you use for-loops.