Home  >  Java 8

Java Stream generate()

By Arvind Rai, May 28, 2020
This page will walk through Stream.generate method example. The generate method returns an infinite sequential unordered stream where each element is generated by the provided Supplier. The generate method is used to generate constant streams and streams of random elements.
Find the generate method signature from Java doc.
static <T> Stream<T> generate‚Äč(Supplier<? extends T> s) 
Parameters:
Pass the Supplier using which stream elements are generated.
Returns:
It returns a new infinite sequential unordered Stream.

Example-1
Find the example to generate stream of random integer.
Stream<Integer> stream = Stream.generate(() -> new Random().nextInt(10));
stream.forEach(e -> System.out.println(e)); 
Find the output.
2
5
1
--- 
Find the example to generate stream of random Boolean.
Stream.generate(() -> new Random().nextBoolean())
   .forEach(e -> System.out.println(e)); 
Find the output.
true
false
true
--- 
Find the example to generate constant stream.
Stream.generate(() -> "Hello World!")
   .forEach(e -> System.out.println(e)); 
Output
Hello World!
Hello World!
Hello World!
--- 

Example-2
As we know that generate returns infinite sequential stream, to limit the number of elements in stream, we can use Stream.limit method.
LimitGenerateDemo.java
package com.concretepage;
import java.util.Random;
import java.util.stream.Stream;
public class LimitGenerateDemo {
  public static void main(String[] args) {
	Stream.generate(() -> new Random().nextInt(10)).limit(3)
	   .forEach(e -> System.out.println(e));
	
	Stream.generate(() -> new Random().nextBoolean()).limit(3)
	   .forEach(e -> System.out.println(e));
	
	Stream.generate(() -> "Hello World!").limit(3)
	   .forEach(e -> System.out.println(e));
  }
} 
Output
3
1
3
true
false
false
Hello World!
Hello World!
Hello World! 

Example-3
Find the generate method declaration from IntStream, LongStream and DoubleStream.
For IntStream
static IntStream generate(IntSupplier s) 
For LongStream
static LongStream generate(LongSupplier s) 
For DoubleStream
static DoubleStream generate(DoubleSupplier s) 

Now find the example.
GenerateDemo.java
package com.concretepage;
import java.util.Random;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class GenerateDemo {
  public static void main(String[] args) {
	System.out.println("--- IntStream ---");
	IntStream.generate(() -> new Random().nextInt()).limit(3)
	   .forEach(e -> System.out.println(e));
	
	System.out.println("--- LongStream ---");	
	LongStream.generate(() -> new Random().nextLong()).limit(3)
	   .forEach(e -> System.out.println(e));
	
	System.out.println("--- DoubleStream ---");	
	DoubleStream.generate(() -> new Random().nextDouble()).limit(3)
	   .forEach(e -> System.out.println(e));	
  }
} 
Output
--- IntStream ---
1208611141
1228590173
1248692128
--- LongStream ---
-1743816652457802629
-5799826804201802284
795354974140733326
--- DoubleStream ---
0.8264963255616102
0.6543249573645461
0.037807208216310784 

Reference

Java doc: Stream
POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us