• Java8新特性Lambda表达式详解


    Lambda表达式

    介绍

    Lambda 是一个匿名函数,可以把 Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。使用它可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使 Java 的语言表达能力得到了提升。

    语法格式

    (o1, o2) -> o1-o2;

    (...):Lambda形参列表,若为空则()->{};
    ->: Lambda操作符,箭头操作符
    ...:Lambda体,编写系列表达式

    例子

    • 无参无返回值 ()->{ System.out.println("Lambda") }
    • 只有一个参数但是无返回值 (String str)->{ System.out.println(str) }
    • 参数类型省略,自动推导 (str)->{ System.out.println(str) }
    • 只有一个参数,可省略括号 str->{ System.out.println(str) }
    • 需要两个以上参数,多条语句有返回值
    1. (i, j) -> {
    2. System.out.println(i-j);
    3. return i-j;
    4. }
    • 只有一条语句,return和大括号可以省略 (i,j) -> retrun i-j

    函数式接口

    • 若一个接口中,只声明了一个抽象方法,则此接口就是函数式接口
    • 可以通过Lambda表达式创建该接口的实例对象

    自定义函数式接口

    @FunctionalInterface 用于检查该接口是否为函数式接口

    1. @FunctionalInterface
    2. public interface MyInterface {
    3. void method();
    4. }

    使用

    1. ((MyInterface) () -> System.out.println("123")).method();

    Java内置函数式接口

    位于java.util.function包下

    主要四大接口

    其他接口

    使用

    1. @Test
    2. public void t2() {
    3. t2t("1", str -> {
    4. System.out.println(str + "23");
    5. });
    6. }
    7. public void t2t(String str, Consumer<String> consumer) {
    8. consumer.accept(str);
    9. }
    10. @Test
    11. public void t3() {
    12. List<String> list = new ArrayList<>();
    13. list.add("好的");
    14. list.add("好哒");
    15. list.add("你的");
    16. list.add("你哒");
    17. list.add("我的");
    18. System.out.println(t3t(list, s -> s.contains("的")));
    19. }
    20. //过滤字符串
    21. public List<String> t3t(List<String> list, Predicate<String> predicate) {
    22. List<String> filterList = new ArrayList<>();
    23. for(String s : list){
    24. if(predicate.test(s)){
    25. filterList.add(s);
    26. }
    27. }
    28. return filterList;
    29. }

    方法引用与构造器引用

    方法引用

    方法引用可以看做是 Lambda 表达式深层次的表达。换句话说,方法引用就是 Lambda 表达式,也就是函数式接口的一个实例,通过方法的名字来指向一个方法

    什么时候可以使用方法引用?

    • 当要传递给Lambda体的操作,已经有实现方法了,可以使用方法引用
    • 要求接口中的抽象方法的形参列表返回值类型与方法引用的方法的形参列表和返回值类型相同 (针对前两种情况)
    • 当函数式接口方法的第一个参数是需要引用方法的调用者,并且第二个参数是需要引用方法的参数(或无参数)时:ClassName::methodName (针对最后一种情况)

    使用格式

    使用操作符::分割方法名和类或对象

    • 对象::实例方法名
    1. Consumer<String> c1 = str -> System.out.println(str);
    2. PrintStream out = System.out;
    3. Consumer<String> c2 = out::println;
    4. String str = "123";
    5. Supplier s1 = () -> str.length();
    6. Supplier s2 = str::length;
    • 类::静态方法名
    1. Comparator<Integer> c1 = (t1, t2) -> Integer.compare(t1, t2);
    2. Comparator<Integer> c2 = Integer::compare;
    3. Function<Double, Long> f1 = d -> Math.round(d);
    4. Function<Double, Long> f2 = Math::round;
    • 类::实例方法名
    1. Comparator<Integer> c1 = (t1, t2) -> t1.compareTo(t2);
    2. Comparator<Integer> c2 = Integer::compareTo;
    3. BiPredicate<String, String> b1 = (s1, s2) -> s1.equals(s2);
    4. BiPredicate<String, String> b2 = String::equals;
    5. Function<String, Integer> f1 = str -> str.length();
    6. Function<String,Integer> f2 = String::length;

    构造器和数组引用

    和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致。抽象方法的返回值类型即为构造器所属的类的类型

    使用格式

    方法引用:className ::new 数组引用:数组的类型 [] :: new

    1. //构造器引用
    2. Supplier<String> s1 = () -> new String();
    3. Supplier<String> s2 = String::new; //调用空参构造函数
    4. FunctionString> f1 = str -> new String(str);
    5. FunctionString> f2 = String::new; //调用有参构造函数
    6. BiFunctionString> b1 = (bytes,charset) -> new String(bytes,charset);
    7. BiFunctionString> b2 = String::new;
    8. //数组引用
    9. FunctionString[]> f3 = length -> new String[length];
    10. FunctionString[]> f4 = Stri
  • 相关阅读:
    (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
    易基因|文献科普:DNA甲基化测序揭示DNMT3a在调控T细胞同种异体反应中的关键作用
    《无限可能-快速唤醒你的学习脑》阅读笔记
    采购数智化爆发在即,支出宝“3+2“体系助力企业打造核心竞争优势
    hand_springboot
    在Mysql中一条查询SQL的执行过程是怎样的
    【SpringBoot】SpringBoot整合Mybatis、druid
    5分钟,ArcGIS 简单几步从天地图中提取出建筑物轮廓的矢量数据
    智慧公厕的原理与优势,了解一种更智能的卫生设施
    AI大预言模型——ChatGPT在地学、GIS、气象、农业、生态、环境应用
  • 原文地址:https://blog.csdn.net/LBWNB_Java/article/details/126359165