• 4大函数式接口


    4大函数式接口

    新时代的程序员:lambda表达式、链式编程、函数式接口、Stream流式计算

    函数式接口: 只有一个方法的接口

    image-20200803232120873

    Function接口

    源码:

    @FunctionalInterface
    public interface Function<T, R> {
    
        /**
         * Applies this function to the given argument.
         *
         * @param t the function argument
         * @return the function result
         * 传入T返回R
         */
        R apply(T t);
    

    实例:

    /**
     * Function 函数型接口, 有一个输入参数,有一个输出
     * 只要是 函数型接口 可以 用 lambda表达式简化
     */
    public class Demo01 {
        public static void main(String[] args) {
            //
    //        Function<String,String> function = new Function<String,String>() {
    //            @Override
    //            public String apply(String str) {
    //                return str;
    //            }
    //        };
    
            Function<String,String> function = str->{return str;};
    
            System.out.println(function.apply("asd"));
        }
    }
    

    断定型接口:Predicate

    只有一个参数返回boolean

    /**
     * 断定型接口:有一个输入参数,返回值只能是 布尔值!
     */
    public class Demo02 {
        public static void main(String[] args) {
            // 判断字符串是否为空
    //        Predicate<String> predicate = new Predicate<String>(){
    ////            @Override
    ////            public boolean test(String str) {
    ////                return str.isEmpty();
    ////            }
    ////        };
    
            Predicate<String> predicate = (str)->{return str.isEmpty(); };
            System.out.println(predicate.test(""));
    
        }
    }
    

    Consumer 消费型接口

    只有输入没有返回值

    /**
     * Consumer 消费型接口: 只有输入,没有返回值
     */
    public class Demo03 {
        public static void main(String[] args) {
    //        Consumer<String> consumer = new Consumer<String>() {
    //            @Override
    //            public void accept(String str) {
    //                System.out.println(str);
    //            }
    //        };
            Consumer<String> consumer = (str)->{System.out.println(str);};
            consumer.accept("sdadasd");
    
        }
    }
    

    Supplier 供给型接口

    没有参数,只有返回值

    /**
     * Supplier 供给型接口 没有参数,只有返回值
     */
    public class Demo04 {
        public static void main(String[] args) {
    //        Supplier supplier = new Supplier<Integer>() {
    //            @Override
    //            public Integer get() {
    //                System.out.println("get()");
    //                return 1024;
    //            }
    //        };
    
            Supplier supplier = ()->{ return 1024; };
            System.out.println(supplier.get());
        }
    }
    

    视频参考https://www.bilibili.com/video/BV1B7411L7tE
    上一篇:线程池
    下一篇:Stream流式计算

  • 相关阅读:
    Beginning Math and Physics For Game Programmers (Wendy Stahler 著)
    The Best Books on Game Dev
    Vector Math for 3D Computer Graphics (Bradley Kjell 著)
    2019年1月
    2018年12月
    Flocks,Herds and Schools: A Distributed Behavioral Model
    2018年11月
    2018年10月
    Tomcat基本
    对比Python中_,__,xx__xx
  • 原文地址:https://www.cnblogs.com/junlinsky/p/13443309.html
Copyright © 2020-2023  润新知