lambda表达式的写法2

ndmtzwdx / 2024-10-23 / 原文

一、类名::静态方法名

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 +
                '}';
    }
}
import java.util.Arrays;
import java.util.List;

interface Inter{
    List<Staff> getStaff(Staff... staffs);
}

public class Demo1 {
    public static void main(String[] args) {
        Staff s1 = new Staff("sj1001", "李刚", 18, 35000);
        Staff s2 = new Staff("sj1002", "钱志强", 13, 20000);
        Staff s3 = new Staff("sj1003", "江川", 24, 50000);
        Staff s4 = new Staff("sj1004", "祝帅", 16, 21000);
        Staff s5 = new Staff("sj1005", "吴问强", 8, 8000);
//        List<Staff> staffList = fun1(staffs -> Arrays.asList(staffs), s1, s2, s3, s4, s5);
        /**
         *  假如 Lambda 表达式符合如下格式:
         *      ([变量1, 变量2, ...]) -> 类名.静态方法名([变量1, 变量2, ...])
         * 我们可以简写成如下格式:
         *      类名::静态方法名
         */
        List<Staff> staffList = fun1(Arrays::asList, s1, s2, s3, s4, s5);
        for (Staff staff : staffList) {
            System.out.println(staff);
        }

    }

    public static List<Staff> fun1(Inter inter, Staff... staffs){
        return inter.getStaff(staffs);
    }
}

二、对象引用::方法名

/**
 * 假如 Lambda 表达式符合如下格式:
 * ([变量1, 变量2, ...]) -> 对象引用.方法名([变量1, 变量2, ...])
 * 我们可以简写成如下格式:
 * 对象引用::方法名
 */
class Demo2 {
    public String show(String s1, String s2) {
        return s1 + "-数加-" + s2;
    }
}

interface Inter{
    String fun1(String a,String b);
}

public class Demo {
    public static void main(String[] args) {
        Demo2 demo2 = new Demo2();

//        show2("hello","ligang",(s1,s2)->demo2.show(s1,s2));
        show2("hello","ligang",demo2::show);
    }

    public static void show2(String s1,String s2,Inter inter){
        String s = inter.fun1(s1, s2);
        System.out.println(s);
    }
}

三、变量1对应的类名::实例方法

/**
 * 假如我们的 Lambda 表达式符合如下格式:
 *      (变量1[, 变量2, ...]) -> 变量1.实例方法([变量2, ...])
 * 那么我们的代码就可以简写成:
 *      变量1对应的类名::实例方法
 */
interface Inter {
    String fun1(String s1, int i, int j);
}

public class Demo {
    public static void main(String[] args) {
//        show1((s1, i, j) -> s1.substring(i, j));
        show1(String::substring);
    }

    public static void show1(Inter inter) {
        String s = inter.fun1("李刚是世界上最有男人味的男人", 8, 11);
        System.out.println(s);
    }
}

四、类名::new

interface Inter{
    Staff fun1(String s1,String s2,int i1,int i2);
}

public class Demo {
    public static void main(String[] args) {
//        show("sj1001","吴问强",18,100000, (s1,s2,i1,i2)->new Staff(s1,s2,i1,i2));
        /**
         *  假如我们的 Lambda 表达式符合如下格式:
         * ([变量1, 变量2, ...]) -> new 类名([变量1, 变量2, ...])
         * 我们就可以简写成如下格式:
         * 类名::new
         */
        show("sj1001","吴问强",18,100000, Staff::new);
    }

    public static void show(String s1,String s2,int i1,int i2,Inter inter){
        Staff staff = inter.fun1(s1, s2, i1, i2);
        System.out.println(staff);
    }
}

五、元素类型[] :: new

import java.util.Arrays;

interface Inter{
    int[] fun(int i);
}

public class Demo {
    public static void main(String[] args) {
//        show(7, i->new int[i]);

        /**
         *  假如我们的 Lambda 表达式符合如下格式:
         *      (变量) -> new 元素类型[变量]
         * 我们就可以简写成如下格式:
         *      元素类型[] :: new
         */
        show(7, int[]::new);
    }
    public static void show(int i,Inter inter){
        int[] ints = inter.fun(i);
        System.out.println(ints.length);
    }
}