Goddchen avatar

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

goddchen

Published: 08 Feb 2018 › Updated: 08 Feb 2018[Android Tutorials #001] How to use Java 8 Stream API on Android

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

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

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

Written by

Freelancer - Android Development

Read more #programming posts


Best Posts From Goddchen

We have not curated any of goddchen's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Goddchen