java中的函数式接口

A.简单概述:

函数式接口:有且仅有一个抽象方法的接口。Java中的函数式编程就是Lambda表达式,所以函数式接口就是可以适用于Lambda使用的接口,只有确保接口中只有一个抽象方法,Java中的Lambda表达式才能顺利进行推导。

B.如何检测接口是否为函数式接口:

在接口定义的上方放上“ @Functionionallnterface ”如果是函数式接口,编译通过,否则编译失败。

注意 :我们自己定义函数式接口时@Functionionallnterface说可选的,就算不写它只要满足函数式接口的条件即可。但是建议加上。

a.代码案例:

package demo122;

/**
 * @author ln
 * @date 2022/1/22
 * @description 函数式接口
 */
public class MyInterfaceDemo {
    public static void main(String[] args) {
        //用作局部变量
        MyInterface my = (() -> System.out.println("函数式接口。。。"));
        my.show();

    }
}
package demo122;
/**
 * @author ln
 * @date 2022/1/22
 * @description 函数式接口(接口)
 */
@FunctionalInterface
public interface MyInterface {
    void show();
}

 

C.函数式接口作为方法的参数:

如果方法的一个参数是函数式接口,我们可以使用Lambda表达式作为参数传递。

a.代码案例:

首先我们在idel中Ctrl + b 跟进查看Runnable接口的源码;

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

我们不难发现,该接口为一个函数式接口。

再看案例:

package demo122;

/**
 * @author ln
 * @date 2022/1/22
 * @description 函数式接口作为方法的参数
 *              1.定义一个类RunnableDemo,在类中提供两个方法
 *                  一个方法是;startThread(Runnable r) 方法参数是一个函数式接口
 *                  一个方法是主方法,在主方法中调用startThread()方法
 */
public class RunnableDemo {
    public static void main(String[] args) {
        //在主方法中调用startThread()方法
        startThread(() -> {
            System.out.println(Thread.currentThread().getName() + "启动啦。。。。");
        });
    }
    //startThread(Runnable r) 方法参数是一个函数式接口
    private static void startThread(Runnable r) {
        //线程的实现方法2
        Thread t = new Thread(r);
        //设置线程名称
        t.setName("我自命名的线程");
        //启动线程
        t.start();
    }
}
我自命名的线程启动啦。。。。

Process finished with exit code 0

D.函数式接口作为方法的放回值:

如果方法的放回值是一个函数式接口,我们可以使用Lambda表达式作为结果返回。

a.代码演示:

/**
 * @author ln
 * @date 2022/1/22
 * @description 函数式接口作为方法的返回值
 * 定义一个类(ComparatorDemo),在类中提供两方法
 * 一个方法是:Comparator<String> getComparator() 方法返回值是一个函数式接口
 * 一个方法是主方法,在主方法中调用getComparator()方法
 */
public class ComparatorDemo {
    public static void main(String[] args) {
        //构造使用场景

        //定义集合,储存字符串元素
        ArrayList<String> array = new ArrayList<>();
        array.add("cccc");
        array.add("aa");
        array.add("b");
        array.add("ddd");

        System.out.println("排序前:" + array);

        Collections.sort(array, getComparator());
        System.out.println("排序后:" + array);

    }

    //Comparator<String> getComparator() 方法返回值是一个函数式接口
    private static Comparator<String> getComparator() {
        return (String s1, String s2) -> {
            return s1.length() - s2.length();
        };
    }

}
排序前:[cccc, aa, b, ddd]
排序后:[b, aa, ddd, cccc]

Process finished with exit code 0

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值