当前位置: 首页> 文旅> 美景 > 函数式编程 lambda表达式

函数式编程 lambda表达式

时间:2025/7/9 11:24:53来源:https://blog.csdn.net/Mwt258/article/details/141967633 浏览次数:0次

函数式编程 lambda表达式

文章目录

  • 函数式编程 lambda表达式
    • 1. 函数对象-lambda表达式
    • 2. 函数接口
    • 3. 函数分类

1. 函数对象-lambda表达式

语法:

({参数}) -> 返回结果

举个栗子🌰:

// 传入参数s,并输出结果s,无返回值
(String s) -> System.out.pringln(s)// 传入参数s,将s转为int类型并返回结果(int)
(String s) -> Integer.parseInt(s)// 无参数,随机生成数字并返回结果(int)
() -> ThreadLocalRandom.current().nextInt()// 传入参数stu,判断stu对象的性别是否为男性并返回结果(boolean)    
(Student stu) -> stu.sex.equals("男") 

参数可以为空,返回结果也可以为空

以上述例子为例,若已经选好了函数接口,如用Consumer接收lambda函数对象,则参数类可通过上下文判断, 对此参数类名可以不写:

// 传入参数s,并输出结果s,无返回值
s -> System.out.println(s)// 传入参数s,将s转为int类型并返回结果(int)
s -> Integer.parseInt(s)// 无参数,随机生成数字并返回结果(int)
() -> ThreadLocalRandom.current().nextInt()// 传入参数stu,判断stu对象的性别是否为男性并返回结果(boolean)  
stu -> stu.sex.equals("男") 

:若存在多行代码,右侧返回结果需要加上{}并使用return返回数据:

(Integer a) -> {int b = a + 1;return b;} 

2. 函数接口

作用:函数接口用来接收lambda函数对象

举个栗子🌰:

@FunctionalInterface //该注解用来表示该接口为函数式接口,若参数或返回结果有问题会抛异常
interface Test<T, O> {O ret(T t);
}

这个函数式接口可以接收(带一个参数,且有一个返回值)的函数对象,如下:

import java.util.List;
import java.util.function.Consumer;public class Exercise {public static void main(String[] args) {Test<String, Integer> test = number -> Integer.parseInt(number);Integer ret = test.ret("25");System.out.println(ret);}@FunctionalInterfaceinterface Test<T, O> {O ret(T t);}
}

  1. 函数对象是在调用函数接口的方法时才进行创建
  2. 自定义函数接口中只能有一个抽象方法

3. 函数分类

jdk为我们提供了一些封装好的函数接口,可用来接受函数对象:

  • Consumer:接收一个参数,无返回值

    import java.util.List;
    import java.util.function.Consumer;public class Exercise {public static void main(String[] args) {consume(List.of(1, 2, 3, 4, 5), number -> System.out.println(number));}/*** 使用Consumer* @param consumer 接收函数对象,接收一个参数且无返回值*/static void consume(List<Integer> list, Consumer<Integer> consumer) {for (Integer number : list) {consumer.accept(number); //该方法已由jdk封装好,可直接调用}}}// 输出结果
    1
    2
    3
    4
    5
    
  • Function:接收一个参数,返回一个返回值

    import java.util.List;
    import java.util.function.Consumer;
    import java.util.function.Function;public class Exercise {public static void main(String[] args) {Integer ret = function("5", str -> Integer.parseInt(str));System.out.println(ret + 25);}/*** 使用Function* @param function 传入一个字符串,将字符串转为整数并返回这个整数*/static Integer function(String s, Function<String, Integer> function) {return function.apply(s);}
    }// 输出结果 
    30
    
  • Supplier:无参数,返回一个返回值

    import java.util.List;
    import java.util.concurrent.ThreadLocalRandom;
    import java.util.function.Consumer;
    import java.util.function.Function;
    import java.util.function.Supplier;public class Exercise {public static void main(String[] args) {Integer result = supplier(() -> ThreadLocalRandom.current().nextInt(1, 10));System.out.println(result);}/*** 使用Supplier 无参数,返回一个 1~10 随机生成的数字* @param supplier*/static Integer supplier(Supplier<Integer> supplier) {return supplier.get();}
    }//输出结果
    1~10 随机值
    
  • Predicate:接收一个参数,返回一个布尔值

    import java.util.List;
    import java.util.concurrent.ThreadLocalRandom;
    import java.util.function.Consumer;
    import java.util.function.Function;
    import java.util.function.Predicate;
    import java.util.function.Supplier;public class Exercise {public static void main(String[] args) {boolean flag = predicate("11111", str -> str.equals("11111"));if (flag) {System.out.println("密码正确");}}/*** 使用Predicate * @param predicate 函数对象,传入一个参数,返回一个布尔值*/static boolean predicate(String s, Predicate<String> predicate) {return predicate.test(s);}
    }// 输出结果
    密码正确
    
  • Operator:接收参数与返回值的类型相同

  • Runnable:无参数,无返回值

同时,若给每个函数 加上前缀,如:BinaryFunction,表示接收两个参数,提供一个返回值

依次类推,可以添加的前缀有:Unary 一元,Binary 二元,Ternary 三元

:Function到了三元需要自己构造函数接口,其它函数可使用jdk提供好的函数接口:

@FunctionalInterface
inerface TernaryFunction<T1, T2, T3, O> {O apply(T1 a, T2 b, T3 c);
}
关键字:函数式编程 lambda表达式

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: