• Lambda表达式的四大内置核心函数式接口


    1、消费型接口

    package com.zh.lambda;
    
    import java.util.function.Consumer;
    import org.junit.Test;
    
    /**
     * @desc Consumer<T>消费型接口
     * @author zhanh247
     */
    public class ConsumerTest {
    
        @Test
        public void test() {
            happy(10000d, (x) -> System.out.println(x));
        }
    
        private void happy(Double money, Consumer<Double> consumer) {
            consumer.accept(money);
        }
    
    }

    2、供给型接口

    package com.zh.lambda;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.function.Supplier;
    import org.junit.Test;
    
    /**
     * @desc Supplier<T>供给型接口
     * @author zhanh247
     */
    public class SupplierTest {
    
        @Test
        public void test() {
            List<Integer> nums = getNums(10, () -> (int) (Math.random() * 100));
            for (Integer integer : nums) {
                System.out.println(integer);
            }
        }
    
        private List<Integer> getNums(Integer num, Supplier<Integer> supplier) {
            List<Integer> numList = new ArrayList<Integer>();
            for (int i = 0; i < num; i++) {
                Integer n = supplier.get();
                numList.add(n);
            }
            return numList;
        }
    
    }

    3、断言型接口

    package com.zh.lambda;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.function.Predicate;
    import org.junit.Test;
    
    /**
     * @desc Predicate<T>断言型接口
     * @author zhanh247
     */
    public class PredicateTest {
    
        @Test
        public void test() {
            List<String> strLists = Arrays.asList("asdfasdf", "cccc", "hello world!", "ddd", "22", "www", "ok");
            List<String> filterStr = filterStr(strLists, (x) -> x.length() > 5);
            for (String str : filterStr) {
                System.out.println(str);
            }
        }
    
        private List<String> filterStr(List<String> strLists, Predicate<String> predicate) {
            List<String> lists = new ArrayList<String>();
            for (String str : strLists) {
                if (predicate.test(str)) {
                    lists.add(str);
                }
            }
            return lists;
        }
    }

    4、函数式接口

    package com.zh.lambda;
    
    import java.util.function.Function;
    import org.junit.Test;
    
    /**
     * @desc Function<T,R>函数式接口
     * @author zhanh247
     */
    public class FunctionTest {
    
        @Test
        public void test() {
            String str = strHandle("asdfasdfasedf", (x) -> x.substring(3, 5));
            System.out.println(str);
        }
    
        private String strHandle(String str, Function<String, String> function) {
            return function.apply(str);
        }
    
    }

    5、方法引用

    package com.zh.lambda.ref;
    
    import java.util.Comparator;
    import java.util.function.BiFunction;
    import java.util.function.BiPredicate;
    import java.util.function.Function;
    import java.util.function.Supplier;
    import org.junit.Test;
    
    /**
     * @desc 方法引用
     * @author zhanh247
     */
    public class LambdaRefTest {
    
        @Test
        public void test() {
            // 1、对象:实例方法名
            Employee employee = new Employee();
            Supplier<String> supplier = () -> employee.getName();
            String string = supplier.get();
            System.out.println(string);
    
            Supplier<Integer> supplier2 = employee::getAge;
            Integer string2 = supplier2.get();
            System.out.println(string2);
    
            // 2、静态方法名
            Comparator<Integer> com1 = (x, y) -> Integer.compare(x, y);
            Comparator<Integer> com2 = Integer::compare;
    
            // 3、类:实例方法名
            BiPredicate<String, String> bp1 = (x, y) -> x.equals(y);
            BiPredicate<String, String> bp2 = String::equals;
    
            // 4、构造器引用
            Supplier<Employee> su1 = () -> new Employee();
            Supplier<Employee> su2 = Employee::new;
    
            Function<Integer, Employee> fu1 = (x) -> new Employee(x);
            Function<Integer, Employee> fu2 = Employee::new;
    
            BiFunction<Integer, String, Employee> bf1 = (x, y) -> new Employee(x, y);
            BiFunction<Integer, String, Employee> bf2 = Employee::new;
            Employee apply = bf2.apply(10, "hello");
            System.out.println(apply);
    
            // 5、数组引用
            Function<Integer, String[]> func1 = (x) -> new String[x];
            String[] apply1 = func1.apply(10);
            System.out.println(apply1.length);
    
            Function<Integer, String[]> func2 = String[]::new;
            String[] apply2 = func2.apply(20);
            System.out.println(apply2.length);
    
        }
    
    }

    6、实体类

    package com.zh.lambda.ref;
    
    public class Employee {
    
        private Integer age;
        private String name;
        private double salary;
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getSalary() {
            return salary;
        }
    
        public void setSalary(double salary) {
            this.salary = salary;
        }
    
        public Employee() {
    
        }
    
        public Employee(Integer age) {
            this.age = age;
        }
    
        public Employee(Integer age, String name) {
            this.age = age;
            this.name = name;
        }
    
        public Employee(Integer age, String name, double salary) {
            this.age = age;
            this.name = name;
            this.salary = salary;
        }
    
        @Override
        public String toString() {
            return "Employee [age=" + age + ", name=" + name + ", salary=" + salary + "]";
        }
    
    }
  • 相关阅读:
    crystal report 用存储过程的问题。
    新的开始—2014
    C#基础(二)——C#中的构造函数
    C#基础(一)——C#中反斜杠/n与/r的区别
    Html基础(一)
    yield关键字, default关键字, 别名关键字
    让 wpf tabcontrol 延缓初始化每个tab item content
    MVC,MVP,MVVM(补充)
    Focus scope in WPF
    Wpf ItemsControl 开启UI Virtualization 的条件
  • 原文地址:https://www.cnblogs.com/zhanh247/p/11845832.html
Copyright © 2020-2023  润新知