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流式计算

  • 相关阅读:
    [ACM] hdu 1671 Phone List (特里)
    Android 记录的(MediaRecorder)而播放(MediaPlayer)
    菜鸟进阶Android Touch事件传递(四)
    九度oj题目&amp;吉大考研11年机试题全解
    怎样取消shutdown关机命令?-shutdown命令的使用解析
    怎样下载并编译Android4.0内核源代码goldfish(图文)
    三角函数图像
    HTML里面Textarea换行总结
    java中使用队列:java.util.Queue
    ContentProvider简单介绍
  • 原文地址:https://www.cnblogs.com/junlinsky/p/13443309.html
Copyright © 2011-2022 走看看