Lightweight-Stream-API

Enhancing Java 8 Streams

License

License

The Apache License, Version 2.1
GroupId

GroupId

com.landawn
ArtifactId

ArtifactId

stream
Last Version

Last Version

2.1
Release Date

Release Date

Type

Type

jar
Description

Description

Lightweight-Stream-API
Enhancing Java 8 Streams
Project URL

Project URL

https://github.com/landawn/Lightweight-Stream-API
Source Code Management

Source Code Management

https://github.com/landawn/Lightweight-Stream-API

Download stream

How to add to project

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

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.

Lightweight-Stream-API

Maven Central Javadocs

Stream API from Java 8 rewritten on iterators for Android and Java 7. Full API documentation is available here.

Includes

  • Functional interfaces (Supplier, Function, Consumer etc);
  • Stream/IntStream/LongStream/DoubleStream (without parallel processing, but with a variety of additional methods and with custom operators);
  • Optional/OptionalBoolean/OptionalInt/OptionalLong/OptionalDouble classes;
  • Fn and Comparators

Usage

Stream.of(/* array | list | set | map | anything based on Iterator/Iterable interface */)
    .filter(..)
    .map(..)
    ...
    .sorted()
    .forEach(..);
Stream.of(value1, value2, value3)...
IntStream.range(0, 10)...

Example project: https://github.com/aNNiMON/Android-Java-8-Stream-Example

Key features

Custom operators

Unlike Java 8 streams, Lightweight-Stream-API provides the ability to apply custom operators.

Stream.of(...)
    .chain(new Reverse<>())
    .forEach(...);

public final class Reverse<T> implements UnaryOperator<Stream<T>> {

    @Override
    public Stream<T> apply(Stream<T> stream) {
        final Iterator<? extends T> iterator = stream.getIterator();
        final ArrayDeque<T> deque = new ArrayDeque<T>();
        while (iterator.hasNext()) {
            deque.addFirst(iterator.next());
        }
        return Stream.of(deque.iterator());
    }
}

You can find more examples here.

Additional operators

In addition to backported Java 8 Stream operators, the library provides:

  • skipNull - filters only not null elements

    Stream.of("a", null, "c", "d", null)
        .skipNull() // [a, c, d]
  • sortedBy - sorts by extractor function

    // Java 8
    stream.sorted(Comparator.comparing(Person::getName))
    // LSA
    stream.sortedBy(Person::getName)
  • groupBy - groups by extractor function

    // Java 8
    stream.collect(Collectors.groupingBy(Person::getName)).entrySet().stream()
    // LSA
    stream.groupBy(Person::getName)
  • chunkBy - partitions sorted stream by classifier function

    Stream.of("a", "b", "cd", "ef", "gh", "ij", "klmnn")
        .chunkBy(String::length) // [[a, b], [cd, ef, gh, ij], [klmnn]]
  • sliding - partitions stream into fixed-sized list and sliding over the elements

    Stream.rangeClosed(0, 10)
        .sliding(4, 6) // [[0, 1, 2, 3], [6, 7, 8, 9]]
  • takeWhile / dropWhile - introduced in Java 9, limits/skips stream by predicate function

    Stream.of("a", "b", "cd", "ef", "g")
        .takeWhile(s -> s.length() == 1) // [a, b]
    Stream.of("a", "b", "cd", "ef", "g")
        .dropWhile(s -> s.length() == 1) // [cd, ef, g]
  • scan - iteratively applies accumulation function and returns Stream

    IntStream.range(1, 6)
        .scan((a, b) -> a + b) // [1, 3, 6, 10, 15]
  • indexed - adds an index to every element, result is IntPair

    Stream.of("a", "b", "c")
        .indexed() // [(0 : "a"), (1 : "b"), (2 : "c")]

Download

Releases are available in Maven Central

Maven:

<dependency>
  <groupId>com.landawn</groupId>
  <artifactId>stream-lite</artifactId>
  <version>0.8.5</version>
</dependency>

or Gradle:

dependencies {
  ...
  compile 'com.landawn:stream-lite:0.8.5'
  ...
}

Also included version for Java ME. Checkout javame branch.

For use lambda expressions in Java 7 or Android, take a look at Retrolambda repository.

Versions

Version
2.1
2.0