简介
- 所谓函数式接口,指的是只有一个抽象方法的接口。(default method 已有实现)
- 函数式接口可以被隐式转换为Lambda表达式。
- 函数式接口可以用
@FunctionalInterface
注解标识。 - 只能标注在interface上
JDK中的一些函数式接口
java.lang.Runnable
java.util.concurrent.Callable
java.security.PrivilegedAction
java.util.Comparator
java.io.FileFilter
java.nio.file.PathMatcher
java.lang.reflect.InvocationHandler
java.beans.PropertyChangeListener
java.awt.event.ActionListener
javax.swing.event.ChangeListenerjava.util.function.*
声明
package Base;
import org.junit.Test;
@FunctionalInterface
interface FunctionInterface {
void method1();
/*
* 如果被@FunctionalInterface标注,则只能有一个abstract method
* Multiple non-overriding abstract methods found in interface Base.FunctionInterfaceTest
*/
// void method2(); // compiler error
}
public class FunctionInterfaceTest {
@Test
public void test1(){
FunctionInterface lambda1 = () ->{
System.out.println("this is function interface1");
};
FunctionInterface lambda2 = () ->{
System.out.println("this is function interface2");
};
lambda1.method1();
lambda2.method1();
}
}
运行结果
this is function interface1
this is function interface2
Process finished with exit code 0
使用
案例1:Thread中使用
public void testLambda(){
// anonymous inner class
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("anonymous inner class implementation");
}
}).start();
// lambda
new Thread(() -> System.out.println("lambda implementation")).start();
}
运行结果
anonymous inner class implementation
lambda implementation
Process finished with exit code 0
案例2:Comsumer中使用
需求
- 定义一个字符串数组,存储每一个人的信息如:“张三,20,郑州市”,存储5个人的信息
- 使用Consumer接口,按照指定的格式进行打印输出:姓名:张三;年龄:20;地址:郑州市
- 要求将打印姓名的动作作为第一个Consumer接口的规则
- 将打印年龄的动作作为第二个Consumer接口的规则
- 将打印地址的动作作为第三个Consumer接口的规则。
- 最终将三个Consumer接口按照规定的顺序拼接输出出来。
代码
// 规则
public static void consumers(String[] arr, Consumer<String> con1, Consumer<String> con2, Consumer<String> con3) {
// 操作arr数组当中的每一个元素
for (String str : arr) {
con1.andThen(con2).andThen(con3).accept(str);// 定义了消费的先后的顺序
}
}
public static void main(String[] args) {
// 定义一个字符串数组
String[] arr = {"李四,20,南阳市", "张三,20,郑州市", "小孙,20,开封市", "小丽,20,信阳市", "小赵,20,洛阳市"};
// 调用consumers方法,由于Consumer接口是一个函数式接口,所以可以使用Lambda
consumers(arr, one -> System.out.print("姓名:" + one.split(",")[0] + ";"),
two -> System.out.print("年龄:" + two.split(",")[1] + ";"),
three -> System.out.println("地址:" + three.split(",")[2]));
}
运行结果
// 规则
private static void consumers(String[] arr, Consumer<String> con1, Consumer<String> con2, Consumer<String> con3) {
// 操作arr数组当中的每一个元素
for (String str : arr) {
con1.andThen(con2).andThen(con3).accept(str);// 定义了消费的先后的顺序
}
}
@Test
public void testConsumer() {
// 定义一个字符串数组
String[] arr = {"李四,20,南阳市", "张三,20,郑州市", "小孙,20,开封市", "小丽,20,信阳市", "小赵,20,洛阳市"};
// 调用consumers方法,由于Consumer接口是一个函数式接口,所以可以使用Lambda
consumers(arr, one -> System.out.print("姓名:" + one.split(",")[0] + ";"),
two -> System.out.print("年龄:" + two.split(",")[1] + ";"),
three -> System.out.println("地址:" + three.split(",")[2]));
}
引用
https://blog.csdn.net/lkforce/article/details/98744598
https://blog.csdn.net/qq_44473694/article/details/111401328