• 函数式接口概述、作为方法的参数、作为方法的返回值及函数式接口Supplier介绍


    目录

    一、函数式接口概述

    二、函数式接口作为方法的参数

    三、函数式接口作为方法的返回值

    四、函数式接口Supplier

    五、Supplier接口练习:获取最大值


    一、函数式接口概述

    概念:

    有且仅有一个抽象方法的接口

    如何检测一个接口是不是函数式接口

    @Functionallnterface

    放在接口定义的上方:如果接口是函数式接口,编译通过;如果不是,编译失败

    注意事项:

    我们自己定义函数式接口的时候,@Functionllnterface是可选的,就算不写这个注解,只要保证满足函数式接口定义的条件,也照样是函数式接口,但是,建议加上该注解

    二、函数式接口作为方法的参数

    需求描述:

    ● 定义一个类(RunnableDemo),在类中提供两个方法

    ● 一个方法是:startThread(Runnable r)方法参数Runnable是一个函数式接口

    ● 一个方法是主方法,在主方法中调用startThread方法

    代码演示:

    1. public class RunnableDemo {
    2. public static void main(String[] args) {
    3. //在主方法中调用startThread方法
    4. //匿名内部类的方式
    5. startThread(new Runnable() {
    6. @Override
    7. public void run() {
    8. System.out.println(Thread.currentThread().getName() + "线程启动了");
    9. }
    10. });
    11. //Lambda方式
    12. startThread(() -> System.out.println(Thread.currentThread().getName() + "线
    13. 程启动了"));
    14. }
    15. private static void startThread(Runnable r) {
    16. new Thread(r).start();
    17. }
    18. }

    三、函数式接口作为方法的返回值

    需求描述:

    ● 定义一个类(ComparatorDemo),在类中提供两个方法

    ● 一个方法是:Compartor getComparator()方法返回值Comparator是已给函数式接口

    ● 一个方法是主方法,在主方法中调用getComparator方法

    代码演示:

    1. public class ComparatorDemo {
    2. public static void main(String[] args) {
    3. //定义集合,存储字符串元素
    4. ArrayList array = new ArrayList();
    5. array.add("cccc");
    6. array.add("aa");
    7. array.add("b");
    8. array.add("ddd");
    9. System.out.println("排序前:" + array);
    10. Collections.sort(array, getComparator());
    11. System.out.println("排序后:" + array);
    12. }
    13. private static Comparator getComparator() {
    14. //匿名内部类的方式实现
    15. return new Comparator() {
    16. @Override
    17. public int compare(String s1, String s2) {
    18. return s1.length()-s2.length();
    19. }
    20. };
    21. //Lambda方式实现
    22. return (s1, s2) -> s1.length() - s2.length();
    23. }
    24. }

    四、函数式接口Supplier

    Supplier接口:

    Supplier接口也被称为生产型接口,如果我们指定了接口的泛型是什么类型,那么接口中的get方法就会生产什么类型的数据供我们使用、

    常用方法:只有一个无参的方法

    方法名说明
    T get()按照某种实现逻辑(由Lambda表达式实现)返回一个数据

    代码演示:

    1. public class SupplierDemo {
    2. public static void main(String[] args) {
    3. String s = getString(() -> "小林");
    4. System.out.println(s);
    5. Integer i = getInteger(() -> 20);
    6. System.out.println(i);
    7. }
    8. //定义一个方法,返回一个整数数据
    9. private static Integer getInteger(Supplier sup) {
    10. return sup.get();
    11. }
    12. //定义一个方法,返回一个字符串数据
    13. private static String getString(Supplier sup) {
    14. return sup.get();
    15. }
    16. }

    五、Supplier接口练习:获取最大值

    案例需求:

    定义一个类(SupplierTest),在类中提供两个方法

    一个方法是:int getMax(Supplier sup)用于返回一int数组中的最大值

    一个方法是主方法,在主方法中调用getMax方法

    示例代码:

    1. public class SupplierTest {
    2. public static void main(String[] args) {
    3. //定义一个int数组
    4. int[] arr = {19, 50, 28, 37, 46};
    5. int maxValue = getMax(()-> {
    6. int max = arr[0];
    7. for(int i=1; i
    8. if(arr[i] > max) {
    9. max = arr[i];
    10. }
    11. }
    12. return max;
    13. });
    14. System.out.println(maxValue);
    15. }
    16. //返回一个int数组中的最大值
    17. private static int getMax(Supplier sup) {
    18. return sup.get();
    19. }
    20. }
  • 相关阅读:
    【爬虫】data: image/png; base64图片数据
    【华为机试真题Java】用户调度问题
    UNet涉及的重点函数记录
    Redis 数据迁移篇之redis-shake工具使用手册
    K8S中POD的控制器
    京东面试题:ElasticSearch深度分页解决方案
    只考一门数据结构!安徽工程大学计算机考研
    【延展Extension的使用场景 Objective-C语言】
    Windows模拟器推荐
    安卓中轻量级数据存储方案分析探讨
  • 原文地址:https://blog.csdn.net/m0_61961937/article/details/126901388