Stream API from Java 8 rewrited on iterators for Java 7 and below.
- Functional interfaces (Supplier, Function, Consumer etc);
Stream
(without parallel processing but with custom operators);Optional
class;Exceptional
class - functional way to deal with exceptions;Objects
from Java 7.
Stream.of(/* array | list | set | map | anything based on Iterator/Iterable interface */)
.filter(..)
.map(..)
...
.sorted()
.forEach(..);
Stream.of(value1, value2, value3)...
Stream.range(0, 10)...
Example project: https://github.com/aNNiMON/Android-Java-8-Stream-Example
Unlike Java 8 streams, Lightweight-Stream-API provides an 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());
}
}
More examples you can find here.
Download latest release or grab via Maven:
<dependency>
<groupId>com.annimon</groupId>
<artifactId>stream</artifactId>
<version>1.0.7</version>
</dependency>
or Gradle:
dependencies {
...
compile 'com.annimon:stream:1.0.7'
...
}
Also included version for Java ME. Checkout javame branch.
For use lambda expressions in Java 6, Java 7 or Android, take look at Retrolambda repository.