I am new to the Java 8 Stream API and I actually don't understand why my code does not work:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamExample {
public static void main(String[] args) {
List<Integer> numbers = Stream.iterate(0, x -> x+3)
.filter(x -> x>10 && x<100).peek(System.out::println)
.collect(Collectors.toList());
numbers.forEach(System.out::println);
}
}
As I understand "laziness" of streams I wrote:
Create stream with numbers divisible by 3
filter it and give me a stream of numbers from range (10, 100)
collect this stream into list
As I can see there is some problem with infinity loop, so peek() prints number from range (12, 99) which is ok, but after that it again prints numbers from (11, 98) etc. Could you explain where I made a mistake?