In this tutorial, we will see “How to generate Infinite stream of values using Java 8?”Generate infinite stream of values in java 8
In Java 8, the Stream.generate() method can be used to generate an infinite stream of values. This method takes a Supplier<T> as an argument which is a functional interface that defines a method get() that generates the next value in the stream.
Java 8 – How to Generate Infinite Stream of Values? Share on X
Using below example, learn how generate() method can be used to generate an infinite stream of random numbers:
/**
* Using 'generate' method of Java 8 Stream, create infinite stream of values
* @author Deepak Verma
*
*/
import java.util.Random;
import java.util.stream.Stream;
public class Generate_Infinite_Stream_Of_Values_Java8_Example {
public static void main(String args[]) {
Random random = new Random();
Stream<Double> randomNumbers = Stream.generate(random::nextDouble);
randomNumbers.forEach(System.out::println);
}
}
Output:
0.0866725571572432 0.5356644334800491 0.2192017138052651 0.8080789865151401 0.7752261572301408 0.0018122228851923383 0.2772936988222323 0.07417026154673667 0.8872217548221702 0.038617746014178334 0.6513961288493136 0.035659661529567344 0.10961282276036777 .............. ......... ....
check if text or string present in a file using java 8
In the above example, random::nextDouble is a method reference to the nextDouble() method of the Random class, which creates a random double value between 0.0 and 1.0.
In order to limit the stream to a specific value then limit() method can be used:
/**
* Using 'Limit' method of Java 8 Stream, Limit the infinite stream of values to a specific limit
* @author Deepak Verma
*
*/
import java.util.Random;
import java.util.stream.Stream;
public class Generate_Infinite_Stream_Of_Values_Java8_Example {
public static void main(String args[]) {
Random random = new Random();
Stream<Double> randomNumbers = Stream.generate(random::nextDouble).limit(5);
randomNumbers.forEach(System.out::println);
}
}
Output:
0.10590612189140236 0.1542923812372361 0.6045931739634557 0.025816524638359373 0.2260982830392817
find second largest number in an array using java 8 stream
Using above example, we can create a stream of random double values limited to ‘5’.
For your information, let me share that ‘generate’ method can be used to create a stream of any type of elements.
skip method java 8 stream
Stream<String> stringStream = Stream.generate(() -> "test").limit(10);
Generate infinite stream of values in java 8
Using above code, you can create a stream of 5 Strings with value “test”.
Java 8 – How to Generate Infinite Stream of Values? Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: