The generate() method accepts a Supplier for element generation. As the resulting stream is infinite, developer should specify the desired size or the generate() method will work until it reaches the memory limit:
Another way of creating an infinite stream is by using the iterate() method:
Stream streamIterated = Stream.iterate(40, n -> n + 2).limit(20);
The first element of the resulting stream is a first parameter of the iterate() method. For creating every following element the specified function is applied to the previous element. In the example above the second element will be 42.