Java streams utility methods for memoization
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md
pom.xml

README.md

streams

Build Status Maven Central Version Coverage Status

Java streams utility methods for memoization

How to replay Java streams?

Need to use your streams over and over again? Let's cover three different approaches, their benefits, and their pitfalls when recycling Java streams.

Read more here https://dzone.com/articles/how-to-replay-java-streams

Usage

Random rnd = new Random();
Stream<Integer> nrs = Stream.generate(() -> rnd.nextInt(99));
Supplier<Stream<Integer>> nrsSrc = Replayer.replay(nrs);

nrsSrc.get().limit(11).map(n -> n + ",").forEach(out::print); // e.g. 88,18,78,75,98,68,15,14,25,54,22,
out.println();
nrsSrc.get().limit(11).map(n -> n + ",").forEach(out::print); // Print the same previous numbers

Note that you cannot achieve this result with an intermediate collection because nrs is an infinite stream. Thus trying to collect nrs incurs in an infinite loop. Only on-demand memoization like replay() achieves this approach.

Installation

First, in order to include it to your Maven project, simply add this dependency:

<dependency>
    <groupId>com.github.javasync</groupId>
    <artifactId>streamemo</artifactId>
    <version>1.0.0</version>
</dependency>

To add a dependency using Gradle:

dependencies {
  compile 'com.github.javasync:streamemo:1.0.0'
}