StreamEx

Enhancing Java 8 Streams

License

License

Categories

Categories

StreamEx General Purpose Libraries Functional Programming
GroupId

GroupId

com.landawn
ArtifactId

ArtifactId

streamex
Last Version

Last Version

2.3.3
Release Date

Release Date

Type

Type

jar
Description

Description

StreamEx
Enhancing Java 8 Streams
Project URL

Project URL

https://github.com/landawn/streamex
Source Code Management

Source Code Management

https://github.com/landawn/streamex

Download streamex

How to add to project

<!-- https://jarcasting.com/artifacts/com.landawn/streamex/ -->
<dependency>
    <groupId>com.landawn</groupId>
    <artifactId>streamex</artifactId>
    <version>2.3.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.landawn/streamex/
implementation 'com.landawn:streamex:2.3.3'
// https://jarcasting.com/artifacts/com.landawn/streamex/
implementation ("com.landawn:streamex:2.3.3")
'com.landawn:streamex:jar:2.3.3'
<dependency org="com.landawn" name="streamex" rev="2.3.3">
  <artifact name="streamex" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.landawn', module='streamex', version='2.3.3')
)
libraryDependencies += "com.landawn" % "streamex" % "2.3.3"
[com.landawn/streamex "2.3.3"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

StreamEx

Enhancing Java 8 Streams.

Maven Central Javadocs

This library defines four classes: StreamEx, IntStreamEx, LongStreamEx, DoubleStreamEx which are fully compatible with Java 8 stream classes and provide many additional useful methods. Also EntryStream class is provided which represents the stream of map entries and provides additional functionality for this case. Finally there are some new useful collectors defined in MoreCollectors class as well as primitive collectors concept.

Full API documentation is available here.

Take a look at the Cheatsheet for brief introduction to the StreamEx!

Before updating StreamEx check the migration notes and full list of changes.

StreamEx library main points are following:

  • Shorter and convenient ways to do the common tasks.
  • Better interoperability with older code.
  • Friendliness for parallel processing: any new feature takes the advantage on parallel streams as much as possible.
  • Performance and minimal overhead. If StreamEx allows to solve the task using less code compared to standard Stream, it should not be significantly slower than the standard way (and sometimes it's even faster).
  • Almost 100% compatibility with original JDK streams except not throwing exception in StreamEx.toMap/Collectors.toMap for null entry values.

Examples

Collector shortcut methods (toList, toSet, toMap, join, etc.)

List<String> userNames = StreamEx.of(users).map(User::getName).toList();
Map<Role, List<User>> role2users = StreamEx.of(users).groupTo(User::getRole);
StreamEx.of(1,2,3).join("; "); // "1; 2; 3"

Intermediate operations: groupBy/groupByToEntry

// By native Java Stream APIs:
accounts.stream()
    .collect(Collectors.groupingBy(e -> e.getFirstName(), Collectors.counting()))
    .entrySet().stream()
    .sorted(Entry.comparingByValue())
    .collect(Collectors.toMap(Function.identity(), Function.identity(), () -> new LinkedHashMap<>()));

// groupBy. Less steps and more clear.
StreamEx.of(accounts)
    .groupBy(e -> e.getFirstName(), Collectors.counting())
    .sorted(Entry.comparingByValue())
    .toMap(Function.identity(), Function.identity(), () -> new LinkedHashMap<>());

// even shorter and clearer with: groupByToEntry.
StreamEx.of(accounts)
    .groupByToEntry(e -> e.getFirstName(), Collectors.counting())
    .sorted(Entry.comparingByValue())
    .toMap(LinkedHashMap::new);

Selecting stream elements of specific type

public List<Element> elementsOf(NodeList nodeList) {
    return IntStreamEx.range(nodeList.getLength())
      .mapToObj(nodeList::item).select(Element.class).toList();
}

Adding elements to stream

public List<String> 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 the stream as Iterable:

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:

Map<String, Role> nameToRole;

public Set<String> getEnabledRoleNames() {
    return StreamEx.ofKeys(nameToRole, Role::isEnabled).toSet();
}

Operating on key-value pairs:

public Map<String, List<String>> invert(Map<String, List<String>> map) {
    return EntryStream.of(map).flatMapValues(List::stream).invert().groupTo();
}

public Map<String, String> stringMap(Map<Object, Object> map) {
    return EntryStream.of(map).mapKeys(String::valueOf)
        .mapValues(String::valueOf).toMap();
}

Map<String, Group> nameToGroup;

public Map<String, List<User>> getGroupMembers(Collection<String> groupNames) {
    return StreamEx.of(groupNames).mapToEntry(nameToGroup::get)
        .nonNullValues().mapValues(Group::getMembers).toMap();
}

Pairwise differences:

DoubleStreamEx.of(input).pairMap((a, b) -> b-a).toArray();

Support of byte/char/short/float types:

short[] multiply(short[] src, short multiplier) {
    return IntStreamEx.of(src).map(x -> x*multiplier).toShortArray(); 
}

Define custom lazy intermediate operation recursively:

static <T> StreamEx<T> scanLeft(StreamEx<T> input, BinaryOperator<T> 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

Installation

Releases are available in Maven Central

Before updating StreamEx check the migration notes and full list of changes.

To use from maven add this snippet to the pom.xml dependencies section:

<dependency>
  <groupId>com.landawn</groupId>
  <artifactId>streamex</artifactId>
  <version>2.3.2</version>
</dependency>

Pull requests are welcome.

Versions

Version
2.3.3
2.3.2
2.3.1
2.3
2.2.2
2.2.1
2.2
2.1.9
2.1.8
2.1.7
2.1.6
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1
2.0.3
2.0.2
2.0.1
2.0.0
1.0.1
1.0
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8