DoubleStream generate() method in Java

Java 8Object Oriented ProgrammingProgramming

The generate() method of the DoubleStream class returns an infinite sequential unordered stream where each element is generated by the provided DoubleSupplier.

The syntax is as follows

static DoubleStream generate(DoubleSupplier s)

Here, the parameter s is the DoubleSupplier for generated elements. The DoubleSupplier is a supplier of double-valued results. To use the DoubleStream class in Java, import the following package

import java.util.stream.DoubleStream;

The following is an example to implement DoubleStream generate() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.generate(()
      -> { return (double)(Math.random() * 100); });
      doubleStream.limit(5).forEach(System.out::println);
   }
}

Output

66.39524427508303
3.184126437402224
18.562637999185117
19.340287633605
94.0690134411309
raja
Published on 28-Mar-2019 10:25:41
Advertisements