[Android Tutorials #001] How to use Java 8 Stream API on Android

in #programming7 years ago

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

Sort:  

Nice post....
please your post whit utopia-io brather,,,

Thanks man, never heard of utopia and by taking a quick look at the website, it supports open source contributions, right? Well, the tutorial is not exactly an open source contribution, should I submit/add it anyway?

Coin Marketplace

STEEM 0.29
TRX 0.21
JST 0.039
BTC 97652.91
ETH 3729.32
SBD 3.91