The generate() method of the DoubleStream interface is used to generate an unlimited, unordered, and sequential stream, in which each element is created by the supplied DoubleSupplier interface implementation. This method may be used to create steady streams, streams with random values, and so on.
public static DoubleStream generate(DoubleSupplier s)
DoubleSupplier s: This is the implementation of the DoubleSupplier interface.
This method returns an infinite, sequential, and unordered stream of double values.
The code given below shows us how we can use the generate() method:
import java.util.Random;import java.util.stream.DoubleStream;class Main {public static void main(String[] args) {DoubleStream doubleStream = DoubleStream.generate(() -> new Random().nextDouble());doubleStream.limit(5).forEach(System.out::println);}}
Random class and the DoubleStream interface.double values, using the generate() method and the nextDouble() method of the Random class.double values to 5 and print the random generated values.