• JUC并发编程——四大函数式接口(基于狂神说的学习笔记)


    四大函数式接口

    函数式接口:只有一个方法的接口 ,例如:Runnable接口

    Function

    函数型接口,有一个输入参数,有一个输出

    源码:

    /**
     * Represents a function that accepts one argument and produces a result.
     *
     * This is a functional interface
     * whose functional method is apply(Object).
     *
     * @param  the type of the input to the function
     * @param  the type of the result of the function
     *
     * @since 1.8
     */
    @FunctionalInterface
    public interface Function<T, R> {
    
        /**
         * Applies this function to the given argument.
         *
         * @param t the function argument
         * @return the function result
         */
        R apply(T t);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    示例:

    package function;
    
    import java.util.function.Function;
    
    /**
     *
     * Function 函数型接口,有一个输入参数,有一个输出
     * 只要是函数式接口,就可以用lambda表达式
     */
    public class Demo01 {
        public static void main(String[] args) {
    
            // 匿名内部类,工具类,输出输入的结果
    //        Function function = new Function() {
    //            @Override
    //            public String apply(String s) {
    //
    //                return null;
    //            }
    //        };
            // 使用lambda表达式
            Function function = (str)->{
                return str;
            };
            System.out.println(function.apply("abc"));
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    Predicate

    断定型接口:只有一个输入参数,返回值为boolean

    源码:

    /**
     * Represents a predicate (boolean-valued function) of one argument.
     *
     * This is a functional interface
     * whose functional method is test(Object).
     *
     * @param  the type of the input to the predicate
     *
     * @since 1.8
     */
    @FunctionalInterface
    public interface Predicate<T> {
    
        /**
         * Evaluates this predicate on the given argument.
         *
         * @param t the input argument
         * @return {@code true} if the input argument matches the predicate,
         * otherwise {@code false}
         */
        boolean test(T t);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    示例

    package function;
    
    import java.util.function.Predicate;
    
    /**
     *
     * 断定型接口:有一个输入参数,返回值为boolean
     */
    public class Demo02 {
    
        public static void main(String[] args) {
    
            // 判断字符串是否为空
    //        Predicate predicate = new Predicate() {
    //            @Override
    //            public boolean test(String s) {
    //                return s.isEmpty();
    //            }
    //        };
            // 函数型接口+lambda表达式,使代码看起来更加简洁
            Predicate<String> predicate = (s)->{return s.isEmpty();};
            System.out.println(predicate.test(""));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    Consumer

    消费型接口,有一个参数,没有返回值

    源码:

    /**
     * Represents an operation that accepts a single input argument and returns no
     * result. Unlike most other functional interfaces, {@code Consumer} is expected
     * to operate via side-effects.
     *
     * 

    This is afunctional interface * whose functional method is accept(Object). * * @param the type of the input to the operation * * @since 1.8 */ @FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t);

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    示例

    package function;
    
    import javax.lang.model.element.NestingKind;
    import java.util.function.Consumer;
    
    /**
     *
     * Consumer 消费型接口:只有输入,没有返回值
     */
    public class Demo03 {
    
        public static void main(String[] args) {
    //        Consumer consumer = new Consumer() {
    //            @Override
    //            public void accept(String s) {
    //                System.out.println(s);
    //            }
    //        };
    
            Consumer<String> consumer = (s)->{
                System.out.println(s);
            };
            consumer.accept("asd");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    Supplier

    供给型接口:没有参数,只有返回值

    源码:

    /**
     * Represents a supplier of results.
     *
     * There is no requirement that a new or distinct result be returned each
     * time the supplier is invoked.
     *
     *  the type of results supplied by this supplier
     *
     * @since 1.8
     */
    @FunctionalInterface
    public interface Supplier<T> {
    
        /**
         * Gets a result.
         *
         * @return a result
         */
        T get();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    示例

    package function;
    
    import java.util.function.Supplier;
    
    /**
     *
     * 供给型接口:没有参数,只有返回值
     */
    public class Demo04 {
    
        public static void main(String[] args) {
    //        Supplier supplier = new Supplier() {
    //
    //            @Override
    //            public Integer get() {
    //                return 1024;
    //            }
    //        };
    
            Supplier<Integer> supplier  = ()->{
                return 1024;
            };
            System.out.println(supplier.get());
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    为什么要学习函数式接口?

    • 简化编程模型,使代码更加可读易懂

    • 在新版本的框架底层中,函数式接口有大量的应用

  • 相关阅读:
    [iOS]-Block
    一些RabbitMQ面试题
    黑苹果安装心得
    Python使用遗传算法(Evolutionary Algorithm、进化算法)构建优化器获取机器学习模型最优超参数组合、拟合最佳模型、实战+代码
    MATLAB中的脚本和函数有什么区别?
    四、K8S之Deployment
    阿米巴经营管理模式是什么,能做什么,有什么好处和坏处?
    大学英语试卷
    Windows 环境搭建 PostgreSQL 物理复制高可用架构数据库服务
    【数值计算】追赶法
  • 原文地址:https://blog.csdn.net/whale_cat/article/details/133872037