zoukankan      html  css  js  c++  java
  • 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流式计算

  • 相关阅读:
    送给有缘的人,2007年9月20日可买入股票
    上次9.19推荐的兰太实业停牌的公告,有持有的朋友可查看
    送给有缘的人,2007年9月24日可买入股票
    对古越龙山的惆怅
    将PDA矢量图控件开源
    读取PE文件的导入表
    [非原创] 用于将真彩色图像降级为索引图像的八叉树算法
    读取PE文件的资源表
    [VC6] 图像文件格式数据查看器
    关于 AlphaBlend 和 32bpp 的反锯齿图标
  • 原文地址:https://www.cnblogs.com/junlinsky/p/13443309.html
Copyright © 2011-2022 走看看