Java 8 way to repeat code block x times

Perhaps the normal for the loop is still correct, but I wanted to see if there is a more concise way to do this in java 8.

for (int i = 0; i < LIMIT; i++) { // Code } 

Is there another java 8 way to do this. I really do not need, I just need to repeat something x times.

Thanks Nathan

+7
java java-8
source share
2 answers

The best way to see how to do this is something like IntStream.range(0, LIMIT).forEach($ -> code) .

+11
source share

One reason for using IntStream is to add concurrency, assuming you understand the impact of this.

 IntStream.range(0, LIMIT).parallel().forEach($ -> { // some thing thread safe. }); 
+5
source share

All Articles