Home

Awesome

Lightweight-Stream-API

Coverage Status SonarCloud Status SonarCloud Quality Gate
Build Status Maven Central javadoc

Stream API from Java 8 rewritten on iterators for Java 7 and below.

Includes

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(...)
    .custom(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:

Throwable functions

No more ugly try/catch in lambda expressions.

// Java 8
stream.map(file -> {
    try {
        return new FileInputStream(file);
    } catch (IOException ioe) {
        return null;
    }
})
// LSA
stream.map(Function.Util.safe(FileInputStream::new))

Download

Download latest release or grab via Maven:

<dependency>
  <groupId>com.annimon</groupId>
  <artifactId>stream</artifactId>
  <version>1.2.2</version>
</dependency>

or Gradle:

dependencies {
  ...
  implementation 'com.annimon:stream:1.2.2'
  ...
}

or use latest unrealeased features with JitPack.

Also included version for Java ME. Checkout javame branch.