https://github.com/javasync/streamemo
Java streams utility methods for memoization
https://github.com/javasync/streamemo
Last synced: 5 months ago
JSON representation
Java streams utility methods for memoization
- Host: GitHub
- URL: https://github.com/javasync/streamemo
- Owner: javasync
- License: mit
- Created: 2018-05-08T12:26:27.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-12-12T22:23:26.000Z (over 1 year ago)
- Last Synced: 2025-08-17T05:51:37.755Z (10 months ago)
- Language: Java
- Size: 103 KB
- Stars: 11
- Watchers: 9
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# streams
[](https://sonarcloud.io/dashboard?id=com.github.javasync%3Astreamemo)
[](https://search.maven.org/#search%7Cga%7C1%7Ca%3A%22streamemo%22)
[](https://sonarcloud.io/component_measures?id=com.github.javasync%3Astreamemo&metric=Coverage)
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
```java
Random rnd = new Random();
Stream nrs = Stream.generate(() -> rnd.nextInt(99));
Supplier> 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:
```xml
com.github.javasync
streamemo
1.0.1
```
To add a dependency using Gradle:
```
dependencies {
compile 'com.github.javasync:streamemo:1.0.0'
}
```
## Changelog
### 1.0.1 (August, 2019)
Add the ability to close the original stream.
Now the the `onClose()` method of a stream from the `Supplier.get()`
will trigger a call to the original Stream's onClose() method.
Contribution from shollander issue #2.
### 1.0.0 (June, 2018)
First release according to the article "How to Reuse Java Streams" published on DZone at Jun. 12, 18