June 2018
Beginner
722 pages
18h 47m
English
The Stream<T> generate(Supplier<T> supplier) static method of the Stream interface creates an infinite stream, where each element is generated by the provided Supplier<T> function. Here are two examples:
Stream.generate(() -> 1).limit(5) .forEach(System.out::print); //prints: 11111Stream.generate(() -> new Random().nextDouble()).limit(5) .forEach(System.out::println); //prints: 0.38575117472619247 // 0.5055765386778835 // 0.6528038976983277 // 0.4422354489467244 // 0.06770955839148762
Since the stream is infinite, we have added a limit() operation.
Read now
Unlock full access