Packrat
Gatherers library for Java Stream API
Install / Use
/learn @jhspetersson/PackratREADME
Packrat
Packrat is a Java library that provides various Gatherer implementations for the Stream API. Gatherers can enhance streams with custom intermediate operations.
You can think of the gatherers like of the custom map, filter, etc. stateful operations which you can insert into the usual streams. No need to use external AtomicLong for counting, for example.
This library can be used as is or as a source of inspiration for writing your own gatherers.
Introduction to the Gatherers by Viktor Klang
Availability
Requires JDK 25 or later.
Usage
Maven
<dependency>
<groupId>io.github.jhspetersson</groupId>
<artifactId>packrat</artifactId>
<version>0.2.2</version>
</dependency>
Gradle
implementation("io.github.jhspetersson:packrat:0.2.2")
Gatherers
Filtering and mapping operations
| Name | Description |
|----------------------------------------------------------------|-----------------------------------------------------------------------------------|
| distinctBy | Distinct values with custom mapper |
| filterBy | Filter with custom mapper and (optionally) predicate |
| filterEntries | Filter Map.Entry elements using a BiPredicate on key and value |
| removeBy | Remove with custom mapper and (optionally) predicate |
| removeEntries | Remove Map.Entry elements using a BiPredicate on key and value |
| removeDuplicates | Removes consecutive duplicates from a stream |
| flatMapIf | Optional flatMap depending on predicate |
| minBy | The smallest element compared after mapping applied |
| maxBy | The greatest element compared after mapping applied |
Sequence operations
| Name | Description | |----------------------------------------------------------------|-----------------------------------------------------------------------------------| | increasing | Increasing sequence, other elements dropped | | increasingOrEqual | Increasing (or equal) sequence, other elements dropped | | decreasing | Decreasing sequence, other elements dropped | | decreasingOrEqual | Decreasing (or equal) sequence, other elements dropped | | reverse | All elements in reverse order | | rotate | All elements rotated left or right | | shuffle | All elements in random order |
Mapping with position operations
| Name | Description | |----------------------------------------------------------------|-----------------------------------------------------------------------------------| | mapFirst | Maps first element with mapper, other unchanged | | mapN | Maps n elements, other unchanged | | skipAndMap | Skips n elements, maps others | | skipAndMapN | Skips skipN elements, maps mapN others | | mapWhile | Maps elements using the supplied function while the predicate evaluates to true. | | mapUntil | Maps elements using the supplied function until the predicate evaluates to false. |
Collection and chunking operations
| Name | Description |
|----------------------------------------------------------------|-----------------------------------------------------------------------------------|
| increasingChunks | Lists of increasing values |
| increasingOrEqualChunks | Lists of increasing or equal values |
| equalChunks | Lists of equal values |
| decreasingChunks | Lists of decreasing values |
| decreasingOrEqualChunks | Lists of decreasing or equal values |
| nCopies | Copies every element n times |
| repeat | Collects the whole stream and repeats it n times |
| atLeast | Distinct values that appear at least n times |
| atMost | Distinct values that appear at most n times |
Indexing and zipping operations
| Name | Description | |----------------------------------------------------------------|---------------------------------------------------------------------------------| | zip | Zips values with zipper, leftovers dropped | | mapWithIndex or zipWithIndex | Maps/zips values with an increasing index | | peekWithIndex | Peek at each element with its index | | filterWithIndex | Filter elements based on their index and a predicate | | removeWithIndex | Remove elements based on their index and a predicate | | windowSlidingWithIndex | Returns fixed-size windows of elements along with their indices | | windowFixedWithIndex | Returns fixed-size non-overlapping windows of elements along with their indices |
Element selection operations
| Name | Description | |----------------------------------------------------------------|-----------------------------------------------------------------------------------| | sample | Sample of the specified size | | randomFilter | Randomly accepts each element with a given probability | | nth | Takes nth element from the stream | | dropNth | Drops every nth element from the stream | | even | Returns elements at even indices (0, 2, 4, ...) | | odd | Returns elements at odd indices (1, 3, 5, ...) | | last | Last n elements | | lastUnique | Last n unique elements | | lastUniqueBy
