Java 8 Features to start using in Android

In this tutorial, I will explore to start using java 8 features in your project. I have seen many of developers they are not using the java 8 features in android. Probably they are thinking that Java is not giving any new feature. I would say for those people that Java is still not dead, a bunch of new features is added in version 8. Every android developer should start using the java 8 features as much as possible. Ok Sound Great:)

Let’s check what are the best java 8 features available to use in basic programming.

Lambda Expression

Lamba expression is added in java 8. Why should use the lambda expression? I would say there are many reasons to use this. We can easily distribute processing of collection over multiple threads. It is kind of block of code to execute. Let’s take an example for sorting the list of android versions by using Lambda expression.

prior Java 8:

Result after sort: Jelly Bean, Kitkat, Lolipop, Marshmallow

Now in java 8.

That is very less code now wow. Even We can make more short.

Wow, Lambda is the amazing feature.  Let’s check another java 8 feature.

Method and Constructor References

Java 8 provide the option to pass references to methods or constructors via the: : keyword.  Let’s check an example, How we can pass reference constructor? Below example, I have a class TestJava with method startsWith to get the first character from my String.

In java 8 we can call by using the converter to convert via the : : keyword.

Let’s see how the : : keyword works for constructors. First, we define an example class name MobileDevices with different constructors.

and we need to create the interface factory for MobileDevices to create the Object of same.

Now we can create the object of Mobile devices based on Mobilefactory via reference constructor.  Java compiler automatically chooses the right constructor for signature to create the object.

Predicates

Predicates are popular in mathematic, it is kind of function which is return a boolean value. Let’s see an example of predicate keyword used in java collection.


Suppose I have many devices having different platform and version. I want to access the list of valid devices. For ex: I need only those devices which are Android version 5 or higher. Ok Cool:) Let’s see how we can filter by using filter keyword.

Wow, I am loving java 8 now. This makes new kind of programming in Java World.

Streams

A stream represents a sequence of elements and supports a different kind of operations to perform computations upon those elements. What does mean of a different kind of operation?  Here is the list of operations that can be done by the stream.

1.  Filter

2. Sorted

3. Map

4. Match

5. count

Ok That’s fine. Let’s see an example of streams to do the above operations.  Suppose I have a list of devices as below code.

Let’s say I want to filter only those devices which name start with “a” character.  So I use filter operation on the streams. A filter is an immediate operation which enables us to call another stream operation (forEach) on the result.

Great: Now I have a list of devices which are android platform based. But I want this list as a sorted. So we can use sorted operation on stream.

Now I got the sorting device’s names on the list. I want to all names should be in uppercase. I need to use Map operation. Let’s see how?

Through the match operation, we can check any devices is matching which name start with “i” character. Let’s check.

We can count the number of devices which are start with “a”  by using the count operation.

Greate. Awesome Java 8 feature. In my next tutorial,  we will learn and understand Java 8 stream and Rx Observable in Rxjava2 and RxAndroid. Here are the best offer and recommendation for you to download this. Here is the link to click.

Happy weekend all. Enjoy 😉

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

One thought on “Java 8 Features to start using in Android

  1. Good article! worth reading information you have shared here on java 8 features to start using in android. Most of us just aware of java 8 features only but after reading this we got to know that how the way we can use java features in android.Thanks for sharing.

Leave a Reply

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