【JDK源码】函数式接口

简介

在这里插入图片描述

  • 所谓函数式接口,指的是只有一个抽象方法的接口。(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.ChangeListener

java.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pass night

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值