java8有新的特点就是lambda表达式,它是吸收了scala函数式编程的思想的
java的lambda:其实是简化了实现匿名函数的过程。
引进lambda表达式后,我们发现它大大的简化java实现匿名函数的代码量。
如下的代码所示:
package cn.dss.lambda;
public class Program {
public static void main(String[] args) {
MyCompartor myCompartor = new MyCompartor();
Comparatir comparatir1 = new Comparatir() {
// 匿名函数
@Override
public int compare(int a, int b) {
// TODO Auto-generated method stub
return 0;
}
};
// 使用lambda 表达式来完成上述的匿名函数,
Comparatir comparatir2 = (a, b) -> a - b;
/**
* 使用lambda的条件?
* 要求接口中的抽象方法只能是一个。
*
*/
}
}
class MyCompartor implements Comparatir {
@Override
public int compare(int a, int b) {
// TODO Auto-generated method stub
return 0;
}
}
@FunctionalInterface //修饰函数式接口(接口中的抽象 方法只有一个)
interface Comparatir {
int compare(int a, int b);
// void pprint();
}
下面我们来看看lambda对接口的实现过程:
如下定义了6个接口,每个接口都是不一样 的,我们来使用lambda表达式对接口进行实现
我们首先来创建好我们的接口。
package cn.dss.lambda.interfacedemo;
@FunctionalInterface
public interface demo01 {
void test();
}
package cn.dss.lambda.interfacedemo;
@FunctionalInterface
public interface demo2 {
void test(int x);
}
package cn.dss.lambda.interfacedemo;
@FunctionalInterface
public interface demo3 {
void test(int x, int y);
}
package cn.dss.lambda.interfacedemo;
@FunctionalInterface
public interface demo4 {
int test();
}
package cn.dss.lambda.interfacedemo;
@FunctionalInterface
public interface demo5 {
int test(int x);
}
package cn.dss.lambda.interfacedemo;
public interface demo6 {
int test(int a,int b);
}
接口创建好了,接下来我们使用lambda表达式对接口进行实现了,如下的代码:
下面的代码,还涉及到一些lambda表达式的一些语法的简化过程
请注意下面的代码中的一些注释,观察lambda表达式的简化。
package cn.dss.lambda;
import cn.dss.lambda.interfacedemo.demo01;
import cn.dss.lambda.interfacedemo.demo2;
import cn.dss.lambda.interfacedemo.demo3;
import cn.dss.lambda.interfacedemo.demo4;
import cn.dss.lambda.interfacedemo.demo5;
import cn.dss.lambda.interfacedemo.demo6;
public class Test {
public static void main(String[] args) {
/**
* 无参数无返回。 ()参数列表 {}方法体 -> :参数列表与方法体的分隔。
*/
// demo01 demo = () -> {
// System.out.println("人才啊!");
// };
// 简化1:lamba语法简化。如下:
demo01 demo = () -> System.out.println("人才啊!");
demo.test();
/**
* 有参数,无返回值的
*
*/
// demo2 demo2 = (int x) -> {
// System.out.println(x + "endplan!");
// };
// 简化2:类型可以省略,要一起省。
// demo2 demo2 = (x) -> {
// System.out.println(x + "endplan!");
// };
// 简化 3:只有一个参数可以省略()这个如下
demo2 demo2 = x -> {
System.out.println("人才啊!");
};
demo2.test(2);
/**
* 有参数,无返回值!
*/
demo3 demo3 = (int x, int y) -> {
System.out.println(x + y + " ");
};
demo3.test(1, 2);
/**
* 无参数,有返回值!
*/
// demo4 demo4 = () -> {
// return 2;
// };
// 简化4: 只有一条的return 语句时。return可以省略。
demo4 demo4 = () -> 3;
/**
* 无参数,有返回值!
*/
int x = demo4.test();
System.out.println("x" + x);
/**
* 一个参数,有返回值!
*/
demo5 demo5 = (int a) -> {
return a + 1;
};
System.out.println(demo5.test(3));
/**
* 多个参数,有返回值!
*/
demo6 demo6 = (int a, int b) -> {
return a + b;
};
System.out.println(demo6.test(3, 3));
}
}
方法引用
package cn.dss.lambda;
import cn.dss.lambda.interfacedemo.demo01;
public class Test2 {
public static void main(String[] args) {
demo01 demo1 = () -> {
System.out.println("hahah!");
};
// 把要实现的interface使用static来进行实现,接下来进行调用即可。
// 避免多次实现,有利于后期的代码维护。
demo01 demo2 = () -> demo01impl();
// 如果是static修饰的话,可以使用类名::方法名称
demo01 demo3 = Test2::demo01impl;
}
private static void demo01impl() {
System.out.println("hhahahad!");
}
}