Lambda expression for Android using the new JAVA8 feature

Source: Internet
Author: User

Objective

New features of Lambda expression,java8. With lambda expression, you can replace an interface implementation that has only one function, leaving the anonymous inner class, and the code looks more concise and understandable.
JAVA8 has some other new features, but it may not work on Android.
The Jack compiler is supported after studio 2.x, using it to use java8 lambda expression, but other features are not guaranteed to work.

Note: Some of the JDK's source code is integrated into the Android SDK, and some of the native JDK classes may have added some implementations of the new features, but not in Android.

You can also use the plug-in RETROLAMBDA to support java8 lambda expression.

Jack Configuration

Add the following configuration

android {    defaultConfig {        24//7.0    }    jackOptions {        true    }    compileOptions {//jack、retrolambda编译必需        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }}

To enable the Jack compiler to support lambda expression

RETROLAMBDA Configuration

Project Address: Https://github.com/evant/gradle-retrolambda

Retrolambda is created to be compatible with lambda expression for JAVA5, 6, and 7.

Configured as follows

{    repositories {        jcenter()    }    {        classpath ‘com.android.tools.build:gradle:2.2.3‘        classpath ‘me.tatarka:gradle-retrolambda:3.6.0‘    }‘me.tatarka.retrolambda‘{    defaultConfig {        minSdkVersion 11 //这个没有硬性要求    }    {//jack、retrolambda编译必需        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }}

Note: In order to be compatible with version 7.0 below, Jack mode is not applicable. So the general still chooses RETROLAMBDA

How LAMBDA expression is used

First of all, we need to know that the expression, as a whole, expresses an " object type ".

The simplest lambda expression

Code:

Runnable runnable = () -> System.out.println("hi, 我是 stone");runnable.run();

() is actually the method parameter list, there is no argument, then will go to match the method, because runnable only a void run (), so it will match to it, there is no method name, so the name of the method is ignored.

This is followed by the method body. There is only one line of printed code, you can omit the curly braces for the method body: {}.

Note: The code "new Runnable" is omitted here because the compiler makes automatic type inference. If you call ()---System.out.println ("Hi, I Am Stone"). Run (); So it is compiled, because the compiler does not know which class to look for in the corresponding method

lambda expression with parameters and a return value

Code:

button.setOnTouchListener((view, event)-> {    if (event.getAction() == MotionEvent.ACTION_DOWN) {        if (flag) {            return true;        }    }    return super.onTouchEvent(event);});

When parameters are passed, the name can be defined arbitrarily, and the type definition or undefined is possible; when undefined, the compiler automatically infers it.
When there is a return value, you can use return in the method body, or you can omit return when there is only one piece of code, as follows:

button.setOnTouchListener((v, e) -> super.onTouchEvent(e));

Lambda expression for Android using the new JAVA8 feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.