Stream generate(Supplier s) example
Description
Stream generate(Supplier<T> s)
returns an infinite sequential unordered stream where each
element is generated by the provided Supplier.
Syntax
generate
has the following syntax.
static <T> Stream<T> generate(Supplier<T> s)
Example
The following example shows how to use generate
.
import java.util.Random;
import java.util.stream.Stream;
// ww w . jav a 2 s. co m
public class Main {
public static void main(String[] args) {
Stream.generate(new Random()::nextDouble)
.limit(10)
.forEach(System.out::println);
}
}
The code above generates the following result.