LongStream generate() method in Java

Java 8Object Oriented ProgrammingProgramming

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

The syntax is as follows:

static LongStream generate(LongSupplier s)

Here, s is the LongSupplier for generate elements. The LongSupplier is the supplier of long-valued results.

To use the LongStream class in Java, import the following package:

import java.util.stream.LongStream;

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

Example

 Live Demo

import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args){
      LongStream longStream = LongStream.generate(()
         -> { return (long)(Math.random() * 100); });
      System.out.println("Unordered Stream...");
      longStream.limit(3).forEach(System.out::println);
   }
}

output

Unordered Stream...
79
43
18
raja
Published on 28-Mar-2019 15:51:24
Advertisements