LongStream generate() method in Java


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

Updated on: 30-Jul-2019

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements