In answer to my question user nullpointer suggested here - https://stackoverflow.com/a/53905940/10824969 how for loops can be converted to stream way using IntStream.iterate
.
I had a similar for loop condition with which I iterate
void printMultiples(int number, int threshold) {
for (int i = number; ; i = i + threshold) {
if (i < threshold) {
break;
} else {
System.out.println(i);
}
}
}
trying to convert this to
IntStream.iterate(number, i -> i + number).forEach(System.out::println);
^^
// Non-short-circuit operation consumes the infinite stream
there is a warning with IDE in above code. I also tried to check for multiples using IntStream.range
, the below prints all the numbers instead
IntStream.range(number, threshold).forEach(System.out::println);