-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamCreation.java
More file actions
159 lines (135 loc) · 6.8 KB
/
Copy pathStreamCreation.java
File metadata and controls
159 lines (135 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package Phase5_CollectionsLambdasStreams.LambdaAndStreams;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* Creation of Streams - 10+ Ways
* ------------------------------
* There is no single "Stream constructor". You build a Stream from a
* source - and Java offers many sources to choose from.
* <p>
*
* The Common Routes
* -----------------
* 1. From a Collection list.stream() / set.parallelStream()
* 2. From an Array Arrays.stream(arr)
* 3. Fixed values Stream.of("a", "b", "c")
* 4. Empty stream Stream.empty()
* 5. Builder Stream.<String>builder().add(...).build()
* 6. Iterate Stream.iterate(seed, n -> n + 1).limit(...)
* 7. Generate Stream.generate(Math::random)
* 8. Iterate with predicate Stream.iterate(seed, hasNext, next) (Java 9+)
* 9. Concat two streams Stream.concat(s1, s2)
* 10. From a String split Pattern.compile(",").splitAsStream("a,b,c")
* 11. From an Iterable / Iterator StreamSupport.stream(itble.spliterator(), false)
* 12. From a single nullable value Stream.ofNullable(x) (Java 9+)
* 13. From a file Files.lines(path) (see StreamFileIO)
* 14. Primitive ranges IntStream.range / rangeClosed / iterate
* <p>
*
* Stream.iterate - Two Forms
* --------------------------
* Stream.iterate(seed, UnaryOperator) // INFINITE - needs .limit
* Stream.iterate(seed, Predicate, UnaryOperator) // FINITE (Java 9+)
* <p>
*
* Sequential or Parallel?
* -----------------------
* list.stream() // sequential
* list.parallelStream() // parallel
* anyStream.parallel() // toggle to parallel
* anyStream.sequential() // toggle back
* <p>
*
* See SequentialVsParallel.java for the deep dive.
*/
public class StreamCreation {
public static void main(String[] args) {
section("1) From a Collection");
List<String> names = List.of("Alice", "Bob", "Carol");
Stream<String> s1 = names.stream();
System.out.println("count = " + s1.count());
section("2) From an Array");
String[] arr = {"a", "b", "c"};
Stream<String> s2 = Arrays.stream(arr);
System.out.println("count = " + s2.count());
// Primitive array versions use IntStream / DoubleStream / LongStream
int[] ints = {10, 20, 30, 40};
int total = Arrays.stream(ints).sum();
System.out.println("int sum = " + total);
section("3) Fixed values - Stream.of");
Stream<Integer> s3 = Stream.of(1, 2, 3, 4, 5);
System.out.println("max = " + s3.max(Integer::compare).orElse(-1));
section("4) Empty stream");
Stream<String> none = Stream.empty();
System.out.println("empty count = " + none.count());
section("5) Builder - add a few then build");
Stream<String> built = Stream.<String>builder()
.add("one")
.add("two")
.add("three")
.build();
built.forEach(s -> System.out.print(s + " "));
System.out.println();
section("6) Stream.iterate INFINITE - need .limit");
Stream.iterate(1, n -> n + 2) // 1, 3, 5, 7, ...
.limit(5)
.forEach(n -> System.out.print(n + " "));
System.out.println();
section("7) Stream.iterate FINITE - Java 9+");
Stream.iterate(1, n -> n < 100, n -> n * 2) // 1, 2, 4, ..., 64
.forEach(n -> System.out.print(n + " "));
System.out.println();
section("8) Stream.generate - lazy supplier");
Stream.generate(new Random()::nextInt) // potentially infinite
.limit(5)
.forEach(n -> System.out.print(n + " "));
System.out.println();
section("9) Stream.concat - join two streams");
Stream<Integer> a = Stream.of(1, 2, 3);
Stream<Integer> b = Stream.of(4, 5, 6);
Stream.concat(a, b).forEach(n -> System.out.print(n + " "));
System.out.println();
section("10) From a String split - Pattern.splitAsStream");
Pattern.compile(",")
.splitAsStream("alpha,beta,gamma,,delta")
.forEach(s -> System.out.println(" '" + s + "'"));
section("11) From an Iterable / Iterator via StreamSupport");
Iterable<Integer> it = List.of(10, 20, 30);
Stream<Integer> fromIterable = StreamSupport.stream(it.spliterator(), false);
System.out.println("from Iterable max = " + fromIterable.max(Integer::compare).orElse(-1));
// Build a Stream from a one-shot Iterator
java.util.Iterator<Integer> raw = List.of(100, 200, 300).iterator();
Stream<Integer> fromIter = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(raw, Spliterator.ORDERED),
false);
System.out.println("from Iterator count = " + fromIter.count());
section("12) Stream.ofNullable - one or zero elements - Java 9+");
Stream.ofNullable("hello").forEach(s -> System.out.println(" got: " + s));
Stream.ofNullable(null).forEach(s -> System.out.println(" got: " + s)); // no output
System.out.println("ofNullable(null).count() = " + Stream.ofNullable(null).count());
section("13) Map gives you THREE streams - keys / values / entries");
Map<String, Integer> ages = Map.of("alice", 30, "bob", 25);
ages.keySet().stream().forEach(k -> System.out.println(" key: " + k));
ages.values().stream().forEach(v -> System.out.println(" val: " + v));
ages.entrySet().stream().forEach(e -> System.out.println(" " + e));
section("14) Primitive Streams - ranges");
IntStream.range(0, 5).forEach(n -> System.out.print(n + " ")); // 0,1,2,3,4
System.out.println();
IntStream.rangeClosed(1, 5).forEach(n -> System.out.print(n + " ")); // 1,2,3,4,5
System.out.println();
long countOfTens = IntStream.range(0, 1000).filter(n -> n % 10 == 0).count();
System.out.println("multiples of 10 in 0..999 = " + countOfTens);
DoubleStream.of(1.5, 2.5, 3.5).average().ifPresent(v -> System.out.println("avg = " + v));
section("15) Optional as a degenerate 0-or-1 stream");
Optional<String> opt = Optional.of("present");
opt.stream().forEach(s -> System.out.println(" optional stream: " + s));
// OUTPUT (representative; sample of random values varies)
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}