Lambda学习、配置以及lambda expressions are not supported at this language level
2017-11-07 16:10
656 查看
Lambda是把一些编译器本来就知道的东西隐藏去掉,留下的是IT程序员必须写出来或者是可以修改的。 一、首先进行lambda配置,步骤如下:apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.0"
defaultConfig {
applicationId"rxjava.example.com.rxjavademo"
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
//lambda
compileOptions {
sourceCompatibilityJavaVersion.VERSION_1_8
targetCompatibilityJavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFilesgetDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//lambda
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath'me.tatarka:gradle-retrolambda:3.4.0'
}
}
//or apply plugin: 'java'
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
//lambda
repositories {
mavenCentral()
}
dependencies {
compile fileTree(include: ['*.jar'],dir: 'libs')
testCompile 'junit:junit:4.12'
compile'com.android.support:appcompat-v7:23.4.0'
compile 'io.reactivex:rxjava:1.0.9'
compile'io.reactivex:rxandroid:0.24.0'
compile'com.squareup.retrofit:retrofit:1.9.0'
}
二、学习实例:
1、比如:
实例1FileFilter
File dir = new File("/an/dir/");
FileFilter directoryFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
通过Lambda表达式这段代码可以简化为如下:
File dir = new File("/an/dir/");
FileFilter directoryFilter = (File f) -> f.isDirectory();
File[] dirs = dir.listFiles(directoryFilter);
进一步简化:
File dir = new File("/an/dir/");
File[] dirs = dir.listFiles((File f) -> f.isDirectory());
Lambda表达式使得代码可读性增强了。我承认我开始学习Java的时候对那个匿名内部类感到很困扰,而现在Lambda表达式让这一切看起来都很自然(尤其是有.NET背景的童鞋会发现这个跟.NET中的Lambda表达式好像)
Lambda表达式利用了类型推断(type inference)技术:
编译器知道FileFilter只有一个方法accept(),所以accept()方法肯定对应(File f) -> f.isDirectory()
而且accept()方法只有一个File类型的参数,所以(File f) -> f.isDirectory()中的File f就是这个参数了,
.NET把类型推断做得更绝,如果上面用.NET Lambda表达式写法的话是这样的:
File[] dirs = dir.ListFiles(f => f.isDirectory());
即压根就不需要出现File类型指示。
学习链接如下:http://www.cnblogs.com/feichexia/archive/2012/11/15/Java8_LambdaExpression.html
Lambda表达式语法规则
到目前为止Java 8中的Lambda表达式语法规则还没有完全确定。
但这里简单介绍下:
对于前面的:
File dir = new File("/an/dir/");
File[] dirs = dir.listFiles((File f) -> f.isDirectory());
accept()方法返回布尔值,这种情况f.isDirectory()显然也得是布尔值。这很简单。
而对于:
Button bt = new Button();
bt.addActionListener(event -> { ui.showSomething(); });
actionPerformed()方法的返回类型是void,所以需要特殊处理,即在ui.showSomething();左右加上花括号。(想象下不加会怎么样?如果不加的话,若showSomething()方法返回值是整数类型,那么就意味着actionPerformed()返回整数类型,显然不是,所以必须加花括号用来标记)。
如果Lambda表达式主体部分包含多条语句,也必须用花括号,并且return语句不能省。
比如下面这个:
File dir = new File("/an/dir/");
File[] dirs = dir.listFiles((File f) -> {
System.out.println("Log:...");
return f.isDirectory();
}
); 三、IDEA下报错:lambda expressions are not supported at this languagelevel解决:1. File -> Project Structure -> Project -> Project Language Level 选择“8 Lamdas Type Annotations etc”2. 如果没有改选项请先安装和配置JDK1.83. 如果为Maven项目,请将pom.xml中maven-compiler-plugin中<configuration>中的<source>和<target>配置为1.8
相关文章推荐
- lambda expressions are not supported at this language level解决方法
- lambda expressions are not supported at this language level intellij
- lambda expressions are not supported at this language level
- lambda expressions are not supported at this language level
- lambda expressions are not supported at this language level(需设置project language level)
- lambda expressions are not supported at this language level错误
- lambda expressions are not supported at this language level
- 【lamdba表达式】lambda expressions are not supported at this language level
- Lambda expressions are not supported at this language level
- lambda expressions are not supported at this language level
- lambda expressions are not supported at this language level
- 解决lambda expressions are not supported at this language level的问题
- lambda expressions not supported at this language level
- lambda expressions not supported at this language level & Android Studio支持lambda,
- Lambda expressions are not supported at language level '1.6
- 【IntelliJ IDEA】lambda expressions are not supported at language level x.x
- Java “lambda expressions not supported at this language level”
- lambda expression are not supported at this language level (Android Studio中使用lambda表达式)
- java "lambda expressions not supported at this language level"
- diamond types are not supported at this language level