I was reading this book called Modern Java in Action and one part of code I could not comprehend.
IntStream.iterate(0, n -> n + 4)
.filter(n -> n < 100)
.forEach(System.out::println);
Authors says that code will not terminate. The reason is that there’s no way to know in the filter that the numbers continue to increase, so it keeps on filtering them infinitely!
I did not get the reason. Could someone explain it why.
The int stream will generate a sequence of numbers that start from 0 and have step of 4. Then they will be filtered out. The Int keep generating because there is no terminal condition. It equivalent to