The Random class has been enhanced with a set of methods to produce streams:
// streams/RandomGenerators.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.*;
import java.util.stream.*;
public class RandomGenerators {
public static <T> void show(Stream<T> stream) {
stream.limit(4).forEach(System.out::println);
System.out.println("++++++++");
}
public static void main(String[] args) {
Random rand = new Random(47);
System.out.println("rand.ints():" + rand.ints());
System.out.println("rand.ints().boxed():" + rand.ints().boxed());
show(rand.ints().boxed());
System.out.println("rand.longs():" + rand.longs());
show(rand.longs().boxed());
show(rand.doubles().boxed());
// Control the lower and upper bounds:
show(rand.ints(10, 20).boxed());
show(rand.longs(50, 100).boxed());
show(rand.doubles(20, 30).boxed());
// Control the stream size:
show(rand.ints(2).boxed());
show(rand.longs(2).boxed());
show(rand.doubles(2).boxed());
// Control the stream size and bounds:
show(rand.ints(3, 3, 9).boxed());
show(rand.longs(3, 12, 22).boxed());
show(rand.doubles(3, 11.5, 12.3).boxed());
}
}
/* My Output:
rand.ints():java.util.stream.IntPipeline$Head@4e25154f
rand.ints().boxed():java.util.stream.IntPipeline$4@53d8d10a
-1172028779
1717241110
-2014573909
229403722
++++++++
rand.longs():java.util.stream.LongPipeline$Head@7229724f
2955289354441303771
3476817843704654257
-8917117694134521474
4941259272818818752
++++++++
0.2613610344283964
0.0508673570556899
0.8037155449603999
0.7620665811558285
++++++++
16
10
11
12
++++++++
65
99
54
58
++++++++
29.86777681078574
24.83968447804611
20.09247112332014
24.046793846338723
++++++++
1169976606
1947946283
++++++++
2970202997824602425
-2325326920272830366
++++++++
0.7024254510631527
0.6648552384607359
++++++++
6
7
7
++++++++
17
12
20
++++++++
12.27872414236691
11.732085449736195
12.196509449817267
++++++++
*/
The T type parameter can be anything, so it works with Integer, Long and Double. However, the Random class only produces the primitive types int, long and double. Fortunately, the boxed() stream operation automatically converts the primitives to their boxed counterparts, thus enabling show() to accept the stream.
We can use Random to create a Supplier for any set of objects. For instance:
// streams/RandomWords.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class RandomWords implements Supplier<String> {
List<String> words = new ArrayList<>();
Random rand = new Random(47);
RandomWords(String fname) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(fname));
// Skip the first line:
for (String line : lines.subList(1, lines.size())) {
for (String word : line.split("[ .?,]+")) {
// System.out.println("word:" + word); // word is not empty String
words.add(word.toLowerCase());
}
}
}
public String get() {
return words.get(rand.nextInt(words.size()));
}
@Override
public String toString() {
return words.stream().collect(Collectors.joining(" "));// class Collectors since 1.8
}
public static void main(String[] args) throws Exception {
System.out.println(Stream.generate(new RandomWords("Cheese.dat")).limit(10));
System.out.println(
Stream.generate(new RandomWords("Cheese.dat")).limit(10).collect(Collectors.joining(" ")));
}
}
/* My Output:
java.util.stream.SliceOps$1@5c647e05
it shop sir the much cheese by conclusion district is
*/
references:
1. On Java 8 - Bruce Eckel
2. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#limit-long-
3. https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#ints--
4. https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#ints-int-int-
5. https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#boxed--
6. https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#ints-long-
7. https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#ints-long-int-int-
8. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/RandomGenerators.java
9. https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html
10. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/RandomWords.java