jackOptions{
enabled true
}
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
retrolambda {//指定将源码编译的级别,使用下列代码,会将代码编译到兼容1.6的字节码格式
javaVersion JavaVersion.VERSION_1_6
}
apply plugin: 'me.tatarka.retrolambda'//lambda
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'me.tatarka:gradle-retrolambda:3.7.0' //lambda
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
::输出的时候使用
-> 传递值的时候使用
System.out.print(s);
System.out::println
throwable-> { throwable.printStackTrace()}
Throwable::printStackTrace
() -> body
new Thread(new Runnable() {
@Override
public void run() {
Log.d("log", "lambda test");
}
});
对应lambda
new Thread(() -> Log.d("log", "lambda test");
x -> body
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("log", "lambda test");
}
});
对应lambda
view.setOnClickListener(
v -> Log.d("log", "lambda test"));
(x, y) -> x + y;
List<String> names = Arrays.asList("zhangs", "lisi", "wangw", "dengq");
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
for (String name : names) {
Log.d("log", name);
}
对应lambda
List<String> names = Arrays.asList("zhangs", "lisi", "wangw", "dengq");
Collections.sort(names, (a, b) -> b.compareTo(a));
for (String name : names) {
Log.d("log", name);
}
(int x, int y) -> x + y;
() -> {}
(x, y) -> x + y;
3)单行需要有返回值
(x, y) -> x + y//注意没有分号结尾
Observable.just("Hello", "RxJava")
.map(new Func1<String, String>() {
@Override
public String call(String s) {
return s.toUpperCase();
}
});
Observable.just("Hello", "RxJava")
.map(s -> s.toUpperCase());
(x, y) ->{
x x;
y y;
}
(x, y) ->{
x x;
y y;
return x + y;
}