Stream generate random String and Numbers | Stream.generate() Exampl

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see how Java 8 Stream Generate Random String and Numbers.
Stream provides generate method – It returns an infinite sequential unordered stream where each element is generated by the given supplier.
static <T> Stream<T>  generate(Supplier<T> s)
The random method takes Supplier as a parameter, generates random elements and returns the infinite sequential unordered stream.
Creating a Supplier class which supplies the random string.
import java.util.Random;
import java.util.function.Supplier;

public class Generator implements Supplier<String> {
    Random random = new Random(50);
    char[] letters =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    @Override
    public String get() {
        System.out.print(letters[random.nextInt(letters.length)]);
        return "" + letters[random.nextInt(letters.length)];
    }
}
Generating random string length of 20
public class StreamGenerate {
    public static void main(String[] args) {
        String random_string = Stream.generate(new Generator())
                .limit(20)
                .collect(Collectors.joining());
        System.out.println(random_string);
    }
}
Output:
NY3Q5NQSAQUUM9IA6LXQ
Generating random numbers (integers and doubles) using Stream.generate() method.
// Generating 10 random Integers
Stream.generate(new Random()::nextInt)
       .limit(10).forEach(System.out::println);
// Generating 10 random Doubles
Stream.generate(new Random()::nextDouble)
       .limit(10).forEach(System.out::println);
Output:
-1196464118
-1593812527
-1724863181
259768096
-934357725
-192325390
-2083605141
865137992
760235079

0.4896064504440346
0.4426364500730168
0.9603767307360307
0.7014553531359498
0.7286628591385252
0.961113001747233
0.6084732423154003
0.5903369288532173
0.8575574428205311
0.6410731496668606

Done!
Java 8 Stream API
Happy Learning 🙂

How to convert List to Map in Java 8
How to Filter null values from Java8 Stream
How to get Stream count in Java 8
Java 8 Getting Min and Max values from a Stream
How to Convert Iterable to Stream Java 8
Java 8 How to convert Stream to List
How to Convert Java 8 Stream to Array
Java – How to Convert InputStream to String
C Program – Print prime numbers between two numbers
Java 8 Stream API and Parallelism
Java 8 Stream Filter Example with Objects
C Program – Armstrong numbers between given numbers
Java8 Concatenate Arrays Example using Stream
Java 8 How to Convert List to String comma separated values
PDFBox – How to generate Encrypted pdf in Java

How to convert List to Map in Java 8
How to Filter null values from Java8 Stream
How to get Stream count in Java 8
Java 8 Getting Min and Max values from a Stream
How to Convert Iterable to Stream Java 8
Java 8 How to convert Stream to List
How to Convert Java 8 Stream to Array
Java – How to Convert InputStream to String
C Program – Print prime numbers between two numbers
Java 8 Stream API and Parallelism
Java 8 Stream Filter Example with Objects
C Program – Armstrong numbers between given numbers
Java8 Concatenate Arrays Example using Stream
Java 8 How to Convert List to String comma separated values
PDFBox – How to generate Encrypted pdf in Java
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String