Understanding Java 8 Stream and RxJava2 Observable in Android

In my last tutorial, I have described some of the superior features added in java version 8. In this tutorial, we will learn how RxJava-2 Observable is strongly following Java Stream. I would be recommended to check this post-Java 8 features to start using in android

Before learning Rx Observable in Android we need to understand Java Stream because Java is core programming to building an android app and the perspective of the concept is exactly same as Rx Observable.  Awesome Let’s start to understand Rx Observable but before first thing, we need to know what is Stream in Java?

“Everything is a stream.”

If we talk about Stream in common language it means a continuous flow of data or instructions, typically one having a constant or predictable rate or in another word transmit or receive (data, especially video and audio material) over the Internet as a steady, continuous flow. From the definition it clear that a continuous flow of liquid, air, or gas is called stream.

Yes, an exactly same meaning of Java Stream also. Let me define it in the technical term, A class that supports function style operation on a stream of elements like Filter, Map, sorted etc. Ohh sounds not understand right here Ok. But I am sure guaranteed that end of the tutorial you will able to get to know the thing clear.

Let me explain this with an example in the technical term what is Java Stream and Rx Observable that how we can use in general programming? Let’s say I have a list of devices and I want to iterate and print the list of elements by using Java Stream and Rx Observable.

Java Stream

Rx Observable

If we look into above both programmings, it’s clear that concept is almost matching.

Now Let me take an another example which is used in daily programming which I am taking from my last post. Suppose I have a list of mobile devices of the different platform like android and ios of different -2 versions. In which some of the devices have defected and some are not.  But I am not sure which are not defect.

So basically I want those devices which have not defected and have platform android version 6.  So let’s check how we can use stream concept here to filter and map by using Rx Observable.  I explained in my the last post that how to identify the devices have not defected and android version 6? Here I need to use Predicate function of Java 8. I hoping you understand the Predicate meaning from the last post. But here I don’t use Java 8 stream. Now I want to use Predicate of the RxJava2 feature.

Now I need to filter devices with this predicated devices from a list of devices by using  RxJava.

Now I want to call this method and iterate to check that filtered list is exactly same as  I was expecting.

Here is the result:

Android: Samsung
Android: LG
Android: Micromax

Great 🙂 Awesome Rx Java and Rx Android. Great Thanks to Java 8 Stream.

Here is all code one place.

and calling this function in the Main function.

Awesome. Loved it (Rx Observable). I would say everyone should start using Stream concept.

Please do subscribe to the next post. Enjoy this post  🙂

Sunil Gupta

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

You may also like...

1 Response

  1. April 30, 2017

    […] Greate. Awesome Java 8 feature. In my next tutorial,  we will learn understanding Java 8 stream and Rx Observable in Rxjava2 in RxAndroid. Here is the link to click. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.