lambda表达式的写法1

ndmtzwdx / 2024-10-23 / 原文

一、lambda表达式的含义

Lambda 表达式是 Java 8 引入的一种简洁的语法,用于表示匿名函数或传递行为。它使得我们可以更简洁地表达代码中的行为和函数逻辑,特别是在使用函数式接口时(如 Consumer、Supplier、Function<T, R> 等)。Lambda 表达式可以大大简化代码,尤其是当我们需要为接口的单个抽象方法提供实现时。

二、基本的写法

interface Inter1 {
    void fun1();
}

interface Inter2 {
    void fun1(String s);
}

interface Inter3 {
    String fun1(String s1, String s2);
}

public class LambdaDemo {
    public static void main(String[] args) {
        /**
         * 场景1
         */
        //无参数,无返回值
//        show1(()->{
//            System.out.println("好好学习,天天向上!");
//        });
        // 如果方法逻辑体只有一行的时候,可以将大括号省略
//        show1(()-> System.out.println("好好学习,天天向上!"));

        /**
         * 场景2
         */
        // 有一个参数,无返回值
//        show2((String s)->{
//            System.out.println(s+": 江川");
//        });
        // 左边的参数类型可以省略不写, jvm会做自动类型上下文推断
//        show2((s) -> System.out.println(s + ": 江川"));
        // 若只有一个参数,小括号可以省略不写
//        show2(s -> System.out.println(s + ": 江川"));

        /**
         * 场景3
         */
        //有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
//        show3((String s1,String s2)->{
//            return s1+"-数加-"+s2;
//        });
        // 若Lambda 体中只有一条语句,return和大括号都可以省略不写
//        show3((s1, s2) -> s1 + "-数加-" + s2);

    }

    public static void show3(Inter3 inter3) {
        System.out.println(inter3.fun1("hello", "world"));
    }

    public static void show2(Inter2 inter2) {
        inter2.fun1("hello");
    }

    public static void show1(Inter1 inter1) {
        inter1.fun1();
    }
}

三、具体实例

1.先创建一个Staff类,里面有基本的成员

public class Staff {
    private String id;
    private String name;
    private int age;
    private int salary;

    public Staff() {
    }

    public Staff(String id, String name, int age, int salary) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
}

2.进行测试使用lambda表达式

1.Staff类同上,这里就不重复写了

2.编写接口

public interface FilterStaff {
     boolean fun(Staff staff);
}

3.案例

import java.util.ArrayList;

import java.util.List;

public class StaffDemo1 {

    public static void main(String[] args) {
        ArrayList<Staff> list1 = new ArrayList<>();
        list1.add(new Staff("sj1001","彭于晏",23,30000));
        list1.add(new Staff("sj1002","吴彦祖",13,10000));
        list1.add(new Staff("sj1003","胡歌",21,24000));
        list1.add(new Staff("sj1004","章若楠",18,26000));
        list1.add(new Staff("sj1005","杨幂",26,35000));
        //需求:过滤出薪资大于20000的人的信息
        List<Staff> list2= FilterWithCollection(list1,s-> s.getSalary()>20000);
        for (Staff staff : list2) {
            System.out.println(staff);
        }
    }

    public static List<Staff> FilterWithCollection(List<Staff> staff,FilterStaff filterStaff){
        ArrayList<Staff> staff1 = new ArrayList<>();
        for (Staff staff2 : staff) {
            if(filterStaff.fun(staff2)){
                staff1.add(staff2);
            }
        }
        return staff1;
    }
}