本质:作为函数式接口的实例
语法格式1: 无参,无返回值
@Test
public void format1() {
Runnable oldF = new Runnable() {
@Override
public void run() {
System.out.println("Hello Lambda");
}
};
oldF.run();
System.out.println("*******************************");
Runnable newF = () -> {System.out.println("Hello Lambda");};
newF.run();
}
语法格式2: 有参数无返回值
@Test
public void format2() {
Consumer<String> okdF = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println("Hello " + s);
}
};
okdF.accept("old");
System.out.println("*******************************");
Consumer<String> newF = (String s) -> {System.out.println("Hello " + s);};
//数据类型可以省略,因为可由编译器推断得出,成为 类型推断
Consumer<String> newF2 = (s) -> {System.out.println("Hello " + s);};
//只有一个参数,()可以省略
Consumer<String> newF3 = s -> {System.out.println("Hello " + s);};
newF.accept("new");
newF2.accept("new");
newF3 .accept("new");
}
语法格式3: 需要多个参数,多条执行语句,有返回值
@Test
public void format3() {
Comparator<Integer> oldF = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
System.out.println(o1);
System.out.println(o2);
return o1.compareTo(o2);
}
};
System.out.println(oldF.compare(1, 2));
System.out.println("*******************************");
Comparator<Integer> newF = (Integer o1, Integer o2) -> {
System.out.println(o1);
System.out.println(o2);
return o1.compareTo(o2);
};
System.out.println(newF.compare(4, 3));
System.out.println("*******************************");
//如果lambda体内只有一条语句,return 和 {} 都可以省略
Comparator<Integer> newF2 = (Integer o1, Integer o2) -> o1.compareTo(o2);
System.out.println(newF2.compare(5, 6));
}
函数式接口
如果一个接口中只声明了一个抽象方法,则此接口就是函数式接口
@FunctionalInterface
方法引用
当要传递给lambda体的操作已经有实现的方法论,可以使用方法引用
通过方法的名字指向一个方法
要求: 实现接口的抽象方法的参数列表和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致(第一种和第二种)
格式: 使用::
将类(或对象)与方法名隔开来
- 对象
::
实例方法名 - 类
::
静态方法名 - 类
::
实例方法名
//对象 :: 实例方法名
@Test
public void ffquote() {
Consumer<String> oldQ = s -> System.out.println(s);
oldQ.accept("北京");
System.out.println("*******************************");
PrintStream ps = System.out;
Consumer<String> newQ = ps::println;
newQ.accept("南京");
}
//类 :: 静态方法名
@Test
public void ffquote2() {
Comparator<Integer> oldQ = (o1, o2) -> Integer.compare(o1, o2);
System.out.println(oldQ.compare(1, 2));
System.out.println("*******************************");
Comparator<Integer> newQ = Integer::compare;
System.out.println(newQ.compare(2, 1));
}
//类 :: 实例方法名
@Test
public void ffquote3() {
Comparator<String> oldQ = (s1, s2) -> s1.compareTo(s2);
System.out.println(oldQ.compare("abc", "abd"));
System.out.println("*******************************");
//s1.compareTo(s2) 第一个参数是调用者,第二个参数是参数
Comparator<String> newQ = String::compareTo;
System.out.println(newQ.compare("abd", "abc"));
}
构造器引用
和方法引用类似,函数式接口的抽象方法的形参列表和构造器形参列表一直,抽象方法返回值类型即为构造器所属类的类型
@Test
public void conquote1() {
Supplier<Lambda> oldF= new Supplier<Lambda>() {
@Override
public Lambda get() {
return new Lambda();
}
};
System.out.println(oldF.getClass().getName());
System.out.println("*******************************");
Supplier<Lambda> oldCq = () -> new Lambda();
System.out.println(oldCq.getClass().getName());
System.out.println("*******************************");
Supplier<Lambda> newCq = Lambda::new;
System.out.println(newCq.getClass().getName());
}
数组引用
@Test
public void arrquote() {
Function<Integer, String[]> oldfarr = new Function<Integer, String[]>() {
@Override
public String[] apply(Integer integer) {
return new String[integer];
}
};
System.out.println(oldfarr.apply(5).length);
System.out.println("*******************************");
Function<Integer, String[]> oldfarrq = integer -> new String[integer];
System.out.println(oldfarrq.apply(5).length);
System.out.println("*******************************");
Function<Integer, String[]> newfarrq = String[] :: new;
System.out.println(oldfarrq.apply(5).length);
}