Maybe a normal for loop is still the right way but I wanted to see if there is a more succinct way to do it in java 8.
for (int i = 0; i < LIMIT; i++) {
// Code
}
Is there a more java 8 way to do this. I don't actually need i just need to repeat something x number of times.
Thanks, Nathan
The while loop in Java is a so-called condition loop. This means repeating a code sequence, over and over again, until a condition is met. In other words, you use the while loop when you want to repeat an operation as long as a condition is met.
Java 8 has introduced a new way to loop over a List or Collection, by using the forEach() method of the new Stream class. You can iterate over any Collection like List, Set, or Map by converting them into a java. util. sttream.
What you want to do is use a nested for loop (i.e. a loop inside a loop). The outer loop will print line by line by first printing the string, then starting new line. A new line is added by System. out.
Enumerated loops are loops that are used when you know in advance how many times you want to loop. In Java, these are called for loops.
The best way I can see on how to do this would be something like IntStream.range(0, LIMIT).forEach($ -> code)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With