https://github.com/amaembo/streamex
Enhancing Java Stream API
https://github.com/amaembo/streamex
collections java java8 streams-api
Last synced: 6 months ago
JSON representation
Enhancing Java Stream API
- Host: GitHub
- URL: https://github.com/amaembo/streamex
- Owner: amaembo
- License: apache-2.0
- Created: 2015-04-10T17:02:32.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2025-03-27T15:08:55.000Z (8 months ago)
- Last Synced: 2025-05-14T08:04:24.288Z (6 months ago)
- Topics: collections, java, java8, streams-api
- Language: Java
- Homepage:
- Size: 5.08 MB
- Stars: 2,244
- Watchers: 78
- Forks: 256
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-java - StreamEx - Enhances Java 8 Streams. (Projects / Functional Programming)
- awesome-java8 - StreamEx - Does what it says: enhances the Java 8 streams. The ["cheat sheet"](https://github.com/amaembo/streamex/blob/master/CHEATSHEET.md) is really nice: if you know what you want (e.g., swap keys and values coming from entries in a map), it tells you how to get it. :8ball: (Streams libraries)
- awesome-java-zh - StreamEx - 增强Java 8流。 (项目 / 函数式编程)
- fucking-awesome-java - StreamEx - Enhances Java 8 Streams. (Projects / Functional Programming)
- awesome-java - StreamEx - Enhances Java 8 Streams. (Projects / Functional Programming)
README
# StreamEx 0.8.3
Enhancing the Java Stream API.
[](https://maven-badges.herokuapp.com/maven-central/one.util/streamex/)
[](https://www.javadoc.io/doc/one.util/streamex)
[](https://github.com/amaembo/streamex/actions/workflows/test.yml)
[](https://coveralls.io/github/amaembo/streamex?branch=master)
This library defines four classes: `StreamEx`, `IntStreamEx`, `LongStreamEx`, `DoubleStreamEx`
that are fully compatible with the Java 8 stream classes and provide many useful additional methods.
Also, the `EntryStream` class is provided which represents a stream of map entries and provides
additional functionality for this case. Finally, there are some useful new collectors defined in `MoreCollectors`
class as well as the primitive collectors concept.
Full API documentation is available [here](http://amaembo.github.io/streamex/javadoc/).
Take a look at the [Cheatsheet](wiki/CHEATSHEET.md) for brief introduction to StreamEx!
Before updating StreamEx check the [migration notes](wiki/MIGRATION.md) and the full list of [changes](wiki/CHANGES.md).
StreamEx main points are the following:
* Shorter and convenient ways to do common tasks.
* Better interoperability with older code.
* 100% compatibility with the original JDK streams.
* Friendliness for parallel processing: any new feature takes advantage of parallel streams as much as possible.
* Performance and minimal overhead. Whenever StreamEx allows solving a task using less code compared to the standard JDK
Stream API, it should not be significantly slower than the standard way (and sometimes it's even faster).
### Examples
Collector shortcut methods (toList, toSet, groupingBy, joining, etc.)
```java
List userNames = StreamEx.of(users).map(User::getName).toList();
Map> role2users = StreamEx.of(users).groupingBy(User::getRole);
StreamEx.of(1,2,3).joining("; "); // "1; 2; 3"
```
Selecting stream elements of a specific type
```java
public List elementsOf(NodeList nodeList) {
return IntStreamEx.range(nodeList.getLength())
.mapToObj(nodeList::item).select(Element.class).toList();
}
```
Adding elements to a stream
```java
public List getDropDownOptions() {
return StreamEx.of(users).map(User::getName).prepend("(none)").toList();
}
public int[] addValue(int[] arr, int value) {
return IntStreamEx.of(arr).append(value).toArray();
}
```
Removing unwanted elements and using a stream as an Iterable:
```java
public void copyNonEmptyLines(Reader reader, Writer writer) throws IOException {
for(String line : StreamEx.ofLines(reader).remove(String::isEmpty)) {
writer.write(line);
writer.write(System.lineSeparator());
}
}
```
Selecting map keys by value predicate:
```java
Map nameToRole;
public Set getEnabledRoleNames() {
return StreamEx.ofKeys(nameToRole, Role::isEnabled).toSet();
}
```
Operating on key-value pairs:
```java
public Map> invert(Map> map) {
return EntryStream.of(map).flatMapValues(List::stream).invert().grouping();
}
public Map stringMap(Map map) {
return EntryStream.of(map).mapKeys(String::valueOf)
.mapValues(String::valueOf).toMap();
}
Map nameToGroup;
public Map> getGroupMembers(Collection groupNames) {
return StreamEx.of(groupNames).mapToEntry(nameToGroup::get)
.nonNullValues().mapValues(Group::getMembers).toMap();
}
```
Pairwise differences:
```java
DoubleStreamEx.of(input).pairMap((a, b) -> b-a).toArray();
```
Support for byte/char/short/float types:
```java
short[] multiply(short[] src, short multiplier) {
return IntStreamEx.of(src).map(x -> x*multiplier).toShortArray();
}
```
Define a custom lazy intermediate operation recursively:
```java
static StreamEx scanLeft(StreamEx input, BinaryOperator operator) {
return input.headTail((head, tail) -> scanLeft(tail.mapFirst(cur -> operator.apply(head, cur)), operator)
.prepend(head));
}
```
And more!
### License
This project is licensed under [Apache License, version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
### Installation
Releases are available in [Maven Central](https://repo1.maven.org/maven2/one/util/streamex/)
Before updating StreamEx check the [migration notes](wiki/MIGRATION.md) and the full list of [changes](wiki/CHANGES.md).
#### Maven
Add this snippet to your project's pom.xml `dependencies` section:
```xml
one.util
streamex
0.8.3
```
#### Gradle
Add this snippet to your project's build.gradle `dependencies` section:
```groovy
implementation 'one.util:streamex:0.8.3'
```
Pull requests are welcome.