This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.

[Android Tutorial #1] Use Java8 Stream API on Android

Words
222
Reading
1 min
Listen
Play
9y

Welcome back everyone! I thought about starting a tutorial series on Android development here on Steemit. So let's get right to it!

Yes, Android now supports some Java 8 features like lambdas. You know, the setOnClickListener(v -> doSomething())stuff. Very neat. So you will say to yourself: great, let's use the other great Java8 feature: the streaming API. But nope! Not supported! :/

But as (almost) always, there's some awesome devs out there who come to help :D So let me introduce: Lightweight-Stream-API.
It is a backport of the Java8 stream API with the help of Java7 iterators.

Include in your project

dependencies {
  compile 'com.annimon:stream:1.1.9'
}

Usage

Streams

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)...

Just be careful to import the proper classes: instead of importing java.util.stream.Stream you have to import com.annimon.stream.Stream!!!

Functional Interfaces

As an addition, the library also provides you all of the nice classes that help you implement functional programming in Java7 (like Function, BiFunction, etc...).
Also watch out for the correct class imports here!

PS: Sorry for the missing code highlighting. Unfortunately Steemit doesn't support code highlighting as of now. But as soon as they add this feature, it should automatically be enabled here :D

PPS: This is a test to see if this post performs any different in the #programming category then in the #androiddev category

PPPS: Trying out the utopian.io website



Posted on Utopian.io - Rewarding Open Source Contributors

[Android Tutorial #1] Use Java8 Stream API on Android | Ecency