Skip to content

Stream API from Java 8 rewritten on iterators for Java 7 and Android - Shared functional interfaces with Abacus-Util

License

Notifications You must be signed in to change notification settings

landawn/Abacus-Lightweight-Stream-API

Repository files navigation

Abacus-Lightweight-Stream-API

Maven Central Javadocs

Stream API from Java 8 rewritten on iterators for Java 7 and Android - Shared functional interfaces with Abacus-Util. Full API documentation is available here.

Includes

  • Very light and simple Stream APIs: Stream/EntryStream/IntStream/LongStream/DoubleStream (without parallel processing, but with a variety of additional methods and with custom operators);

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>abacus-stream-lite</artifactId>
  <version>0.8.4</version>
</dependency>

or Gradle:

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

Also included version for Java ME. Checkout javame branch.

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

About

Stream API from Java 8 rewritten on iterators for Java 7 and Android - Shared functional interfaces with Abacus-Util

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages