lambda表达式之jvm提供的接口

ndmtzwdx / 2024-10-23 / 原文

一、java内置函数式接口:断言式接口

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/*
    java内置函数式接口:断言式接口
    只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰

    @FunctionalInterface
    public interface Predicate<T> {
        boolean test(T t);
    }

 */
public class StaffDemo {
    private static final List<Staff> list = Arrays.asList(
            new Staff("sj1001", "李刚", 18, 35000),
            new Staff("sj1002", "钱志强", 13, 20000),
            new Staff("sj1003", "江川", 24, 50000),
            new Staff("sj1004", "祝帅", 16, 21000),
            new Staff("sj1005", "吴问强", 8, 8000)
    );

    public static void main(String[] args) {
        // 需求1:过滤出薪资大于30000的员工
        List<Staff> staffs1 = filterWithCondition(list, s -> s.getSalary() > 30000);
        for (Staff staff : staffs1) {
            System.out.println(staff);
        }

        System.out.println("--------------------------------");
        // 需求2:过滤出年龄小于18的员工
        //使用lambda表达式改写
        List<Staff> staffs2 = filterWithCondition(list, s -> s.getAge() < 18);

        for (Staff staff : staffs2) {
            System.out.println(staff);
        }


    }


    public static List<Staff> filterWithCondition(List<Staff> list, Predicate<Staff> predicate) {
        ArrayList<Staff> list2 = new ArrayList<>();
        for (Staff staff : list) {
            if (predicate.test(staff)) {
                list2.add(staff);
            }
        }
        return list2;
    }
}

二、java内置函数式接口:函数型接口

import java.util.function.Function;

/*
    java内置函数式接口:函数型接口
    只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰
    @FunctionalInterface
    public interface Function<T, R> {
        R apply(T t);
    }
 */
public class StaffDemo {
    public static void main(String[] args) {
        Staff s = new Staff("sj1001", "李刚", 18, 35000);
        show1(s, s1 -> s1.getName() + ":真帅!");
    }

    public static void show1(Staff staff, Function<Staff, String> function) {
        String s = function.apply(staff);
        System.out.println(s);
    }
}

三、java内置函数式接口:供给型接口

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;

/*
    java内置函数式接口:供给型接口
    只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰
    @FunctionalInterface
    public interface Supplier<T> {
        T get();
    }

 */
public class Demo {
    public static void main(String[] args) {
        //随机生成10个1-100的数据到集合返回
        List<Integer> list = getList(() -> new Random().nextInt(100) + 1);
        for (Integer i : list) {
            System.out.println(i);
        }

    }

    public static List<Integer> getList(Supplier<Integer> supplier){
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=1;i<11;i++){
            list.add(supplier.get());
        }
        return list;
    }


}

四、java内置函数式接口:消费型接口


import java.util.function.Consumer;

/*
    java内置函数式接口:消费型接口
    只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰
    @FunctionalInterface
    public interface Consumer<T> {
        void accept(T t);
    }
 */
public class Demo {
    public static void main(String[] args) {
        Staff s = new Staff("sj1001", "李刚", 18, 35000);
        fun1(s, liGang -> System.out.println("员工编号:" + liGang.getId() + ", 姓名:" + liGang.getName() + ", 年龄:" + liGang.getAge() + ", 薪资:" + liGang.getSalary()));
    }

    public static void fun1(Staff staff, Consumer<Staff> consumer) {
        consumer.accept(staff);
    }
}