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:
List<String> versionList = Arrays.asList("Marshmallow", "Lolipop", "Kitkat", "Jelly Bean"); Collections.sort(versionList, new Comparator<String>() { @Override public int compare(String a, String b) { return b.compareTo(a); } });
Result after sort: Jelly Bean, Kitkat, Lolipop, Marshmallow
Now in java 8.
List<String> versionList = Arrays.asList("Marshmallow", "Lolipop", "Kitkat", "Jelly Bean"); Collections.sort(versionList, (String a, String b) -> { return b.compareTo(a); });
That is very less code now wow. Even We can make more short.
List<String> versionList = Arrays.asList("Marshmallow", "Lolipop", "Kitkat", "Jelly Bean"); Collections.sort(versionList, (String a, String b) -> b.compareTo(a));
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.
public class TestJava { String startsWith(String s) { return String.valueOf(s.charAt(0)); } }
In java 8 we can call by using the converter to convert via the : : keyword.
TestJava testJavaObj = new TestJava(); Converter<String, String> converter = testJavaObj::startsWith; String converted = converter.convert("Android"); System.out.println(converted); // A
Let’s see how the : : keyword works for constructors. First, we define an example class name MobileDevices with different constructors.
public class MobileDevices { String deviceName; String platform; MobileDevices() {} MobileDevices(String deviceName, String platform) { this.deviceName = deviceName; this.platform = platform; } }
and we need to create the interface factory for MobileDevices to create the Object of same.
interface MobileDeviceFactory<M extends MobileDevices> { M create(String deviceName, String platform); }
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.
MobileDeviceFactory<MobileDevices> mobileFactory = MobileDevices::new; MobileDevices mobile = mobileFactory.create("Google Pixel", "Android");
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.
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
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.