Java8新特性Lambda表达式详解

2,278 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第4天,点击查看活动详情

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) }
  • 需要两个以上参数,多条语句有返回值
	(i, j) -> {
            System.out.println(i-j);
            return i-j;
        }
  • 只有一条语句,return和大括号可以省略 (i,j) -> retrun i-j

函数式接口

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

自定义函数式接口

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

@FunctionalInterface
public interface MyInterface {
    void method();
}

使用

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

Java内置函数式接口

位于java.util.function包下

主要四大接口

image-1654479774780

其他接口

image-1654481564092

使用

@Test
    public void t2() {
        t2t("1", str -> {
            System.out.println(str + "23");
        });
    }

    public void t2t(String str, Consumer<String> consumer) {
        consumer.accept(str);
    }

    @Test
    public void t3() {
        List<String> list = new ArrayList<>();
        list.add("好的");
        list.add("好哒");
        list.add("你的");
        list.add("你哒");
        list.add("我的");
        System.out.println(t3t(list, s -> s.contains("的")));
    }

    //过滤字符串
    public List<String> t3t(List<String> list, Predicate<String> predicate) {
        List<String> filterList = new ArrayList<>();
        for(String s : list){
            if(predicate.test(s)){
                filterList.add(s);
            }
        }
        return filterList;
    }

方法引用与构造器引用

方法引用

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

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

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

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

  • 对象::实例方法名
        Consumer<String> c1 = str -> System.out.println(str);

        PrintStream out = System.out; 
        Consumer<String> c2 = out::println;
        
        String str = "123";
        Supplier<Integer> s1 = () -> str.length();

        Supplier<Integer> s2 = str::length;
  • 类::静态方法名
        Comparator<Integer> c1 = (t1, t2) -> Integer.compare(t1, t2);
        Comparator<Integer> c2 = Integer::compare;

        Function<Double, Long> f1 = d -> Math.round(d);
        Function<Double, Long> f2 = Math::round;
  • 类::实例方法名
        Comparator<Integer> c1 = (t1, t2) -> t1.compareTo(t2);
        Comparator<Integer> c2 = Integer::compareTo;

        BiPredicate<String, String> b1 = (s1, s2) -> s1.equals(s2);
        BiPredicate<String, String> b2 = String::equals;

        Function<String, Integer> f1 = str -> str.length();
        Function<String,Integer> f2 = String::length;

构造器和数组引用

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

使用格式

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

        //构造器引用
        Supplier<String> s1 = () -> new String();
        Supplier<String> s2 = String::new;  //调用空参构造函数
        Function<char[], String> f1 = str -> new String(str);
        Function<char[], String> f2 = String::new;  //调用有参构造函数

        BiFunction<byte[], Charset,String> b1 = (bytes,charset) -> new String(bytes,charset);
        BiFunction<byte[], Charset,String> b2 = String::new;

        //数组引用
        Function<Integer,String[]> f3 = length -> new String[length];
        Function<Integer,String[]> f4 = String[]::new;