Lightweight-Stream-API
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
andComparators
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 elementsStream.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 functionStream.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 elementsStream.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 functionStream.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 StreamIntStream.range(1, 6) .scan((a, b) -> a + b) // [1, 3, 6, 10, 15]
-
indexed
- adds an index to every element, result isIntPair
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.