PushStream is an eagerly evaluated, re-usable stream. It continues to push its outputs from one stage to the next until it runs out.
PushStream pushStream = PushStream.fromRange(0, 10);
List<Integer> pushOutput = pushStream.flatMap(x -> PushStream.of(-x * x, x * x)).limit(10).toList();
//Output: [0, 0, -1, 1, -4, 4, -9, 9, -16, 16]- Initialization
fromList,of,concat,fromRange
- Debugging
peek
- Transformation
map,flatMap,filter,sortedlimit,skip
- Terminal
count,fold,summin,maxtoList
- See tests file examples
PullStream is a lazily evaluated, single-use stream. Each stage only requests for more information from upstream stages if absolutely necessary.
- This makes it possible to work with infinitely large sequences, as long as we use a stage that limits the size of the output (e.g.
stream.limit(10)). - Certain stages like
sorted(),sum(),count(), etc. will run forever if you attempt to call it on an infinite stream, since it requires knowing all data before it can return an answer- TODO: pass some metadata between stages to hint to the user that they shouldn't try to call such methods, or throw an exception
PullStream counter = PullStream.generator(1, x -> x + 1);
int ans = counter.limit(5).fold(0, (a, b) -> a * 10 + b);
assertEquals(12345, ans);- Initialization
fromList,fromRange,generator
- Transformation
map,flatMap,filter,sortedlimit,takeWhile,skip
- Terminal
count,fold,reducemin,maxtoList
- See tests file examples