Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/poetix/protonpack
Stream utilities for Java 8
https://github.com/poetix/protonpack
Last synced: about 2 months ago
JSON representation
Stream utilities for Java 8
- Host: GitHub
- URL: https://github.com/poetix/protonpack
- Owner: poetix
- License: mit
- Created: 2014-08-18T09:27:50.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-05-09T20:21:28.000Z (7 months ago)
- Last Synced: 2024-07-31T09:11:23.257Z (5 months ago)
- Language: Java
- Size: 243 KB
- Stars: 480
- Watchers: 25
- Forks: 56
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome - poetix/protonpack - Stream utilities for Java 8 (Java)
- awesome-java8 - ProtonPack - Offers about a dozen utilities for `Stream`, e.g., `takeWhile`, `zip`, `aggregate`, and a `unique` collector. :8ball: (Functional libraries)
- awesome-java - protonpack - Collection of stream utilities. (Projects / Functional Programming)
README
protonpack
==========[![Maven Central](https://img.shields.io/maven-central/v/com.codepoetics/protonpack.svg)](http://search.maven.org/#search%7Cga%7C1%7Cprotonpack)
[![Build Status](https://travis-ci.org/poetix/protonpack.svg?branch=master)](https://travis-ci.org/poetix/protonpack)A small collection of ```Stream``` utilities for Java 8. Protonpack provides the following:
* ```takeWhile``` and ```takeUntil```
* ```skipWhile``` and ```skipUntil```
* ```zip``` and ```zipWithIndex```
* ```unfold```
* ```MapStream```
* ```aggregate```
* ```Streamable```
* ```unique``` collectorFor full API documentation, see (http://poetix.github.io/protonpack).
Available from Maven Central:
```xml
com.codepoetics
protonpack
1.16```
## takeWhile
Takes elements from the stream while the supplied condition is met. ```takeUntil``` does the same, but with the condition negated.
```java
Stream infiniteInts = Stream.iterate(0, i -> i + 1);
Stream finiteInts = StreamUtils.takeWhile(infiniteInts, i -> i < 10);assertThat(finiteInts.collect(Collectors.toList()),
hasSize(10));
```## skipWhile
Skips elements from the stream while the supplied condition is met. ```skipUntil``` does the same, but with the condition negated.
```java
Stream ints = Stream.of(1,2,3,4,5,6,7,8,9,10);
Stream skipped = StreamUtils.skipWhile(ints, i -> i < 4);List collected = skipped.collect(Collectors.toList());
assertThat(collected,
contains(4, 5, 6, 7, 8, 9, 10));
```## zip
Combines two streams using the supplied combiner function.
```java
Stream streamA = Stream.of("A", "B", "C");
Stream streamB = Stream.of("Apple", "Banana", "Carrot", "Doughnut");List zipped = StreamUtils.zip(streamA,
streamB,
(a, b) -> a + " is for " + b)
.collect(Collectors.toList());assertThat(zipped,
contains("A is for Apple", "B is for Banana", "C is for Carrot"));
```## unfold
Generates a (potentially infinite) stream using a generator that can indicate the end of the stream at any time by returning Optional.empty().
```java
Stream unfolded = StreamUtils.unfold(1, i ->
(i < 10)
? Optional.of(i + 1)
: Optional.empty());assertThat(unfolded.collect(Collectors.toList()),
contains(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
```## stream
Transforms a source type into a stream
`stream(Optional optional):`
```java
Stream items = idStream.flatMap(id -> StreamUtils.stream(fetchItem(id));
```## Streamable
Streamable is to Stream as Iterable is to Iterator. Useful when you will want to stream repeatedly over some source.
## unique
A collector that returns the one and only item in a stream, if present, or throws an exception if multiple items are found.
```java
assertThat(Stream.of(1, 2, 3).filter(i -> i > 3).collect(CollectorUtils.unique()),
equalTo(Optional.empty()));assertThat(Stream.of(1, 2, 3).filter(i -> i > 2).collect(CollectorUtils.unique()),
equalTo(Optional.of(3)));// Throws NonUniqueValueException
Stream.of(1, 2, 3).filter(i -> i > 1).collect(CollectorUtils.unique());
```## toFutureList
A collector that converts a stream of `CompletableFuture` into a `CompletableFuture>`, which completes
exceptionally if (and as soon as) any of the futures in the list completes exceptionally.```java
Function> processAsynchronously = i -> CompletableFuture.completedFuture(i * 2);
assertThat(
Stream.of(1, 2, 3).map(processAsynchronously)
.collect(CompletableFutures.toFutureList())
.get(),
contains(2, 4, 6));
```