zoukankan      html  css  js  c++  java
  • Java8 内置的函数式接口

    1.Java8 内置的四大核心函数式接口
    (1)Consumer<T> : 消费型接口
             void accept(T t);

    (2)Supplier<T> : 供给型接口
       T get();

    (3)Function<T, R> : 函数型接口
       R apply(T t);

    (4)Predicate<T> : 断言型接口
       boolean test(T t);

    2.四大核心函数式接口示例

    /**
         * 有参数无返回
         * Consumer<T> 消费型接口
         *     void accept(T t);
         */
        @Test
        public void test1(){
            happy(1000, (m) -> System.out.println("KTV消费"+m+"元"));
        }
        
        public void happy(double money, Consumer<Double> con){
            con.accept(money);
        }
        
        /**
         * 无参有返回
         * Supplier<T> 供给型接口
         * 生成一个指定长度的随机数集合
         *     T get();
         * 需求:生成一个指定长度的随机数集合
         */
        @Test
        public void test2(){
            List<Integer> list = getList(10, () -> {
                Random r = new Random();
                return r.nextInt(100);
            });
            
            for(Integer num : list) {
                System.out.println(num);
            }
        }
        
        public List<Integer> getList(int num, Supplier<Integer> su){
            List<Integer> list = new ArrayList<>();
            
            for(int i = 0; i < num; i++){
                list.add(su.get());
            }
            return list;
        }
        
        /**
         * 有参有返回
         * Function<T, R> : 函数型接口
         *     R apply(T t);
         * 需求:处理字符串
         */
        @Test
        public void test3() {
            //去掉字符串前后空格
            String str = strHandler("		 hello world   ", (x) -> x.trim());
            System.out.println(str);
            
            //将字符串转成大写
            str = strHandler(str, (x) -> x.toUpperCase());
            System.out.println(str);
        }
        
        //处理字符串
        public String strHandler(String str, Function<String, String> fun){
            return fun.apply(str);
        }
        
        /**
         * 判断
         * Predicate<T> : 断言型接口
         *     boolean test(T t);
         * 需求:筛选出指定条件的字符串
         */
        @Test
        public void test4() {
            List<String> list = Arrays.asList("java", "css", "jquery", "android", "ios", "python", "c++");
            List<String> strList = filterStr(list, (t) -> t.length() > 3);
            for(String str : strList){
                System.out.println(str);
            }
        }
        
        //筛选字符串
        public List<String> filterStr(List<String> list, Predicate<String> pre){
            List<String> strList = new ArrayList<>();
            
            for(String str : list) {
                if(pre.test(str)){
                    strList.add(str);
                }
            }
            return strList;
        }

    3.扩展函数式接口 四大函数四接口的子接口

    (1)BiFunction<T, U, R> 参数类型:T, U  返回类型: 用途:对类型为T, U参数应用操作,返回R类型的结果。包含方法为:R apply(T t, U u);

      (2)  UnaryOperator<T>(Funtion子接口)  参数类型:T 返回类型:T 用途:对类型为T的对象进行一元运算,并返回T类型的结果。包含方法为:T apply(T t);

      (3)  BinaryOperator<T>(BiFunction 子接口) 参数类型 :T, T 返回类型:T  用途:对类型为T的对象进行二元运算,并返回T类型的结果。包含方法为:T apply(T t1, T t2);

      (4)  BiConsumer<T, U> 参数类型:T, U  返回类型:void 用途:对类型为T,U的参数应用操作。包含方法为:void accept(T t, U u);

      (5)  ToIntFunction<T>,ToLongFunction<T>,ToDoubleFunction<T> 参数类型:T 返回类型:int,long,double 用途:分别计算 int,long,double值的函数

      (6)  IntFunction<R>,LongFunction<R>,DoubleFunction<R> 参数类型:int,long,double 返回类型:R 用途:参数分别为int,long,double类型的函数

  • 相关阅读:
    Flask ~~“一见钟情之初体验”(Web表单)
    Flask ~~“一见钟情之初体验”(Flask~~过滤~包含~继承~宏)
    Flask ~~“一见钟情之初体验”(flask的网页模板介绍及使用①)
    Flask ~~“一见钟情之初体验”(flask的request模块的属性及上下文)
    Flask ~~“一见钟情之初体验”(cookie与session简单使用)
    Flask 入门(补充)~~~“一见钟情之初体验”
    表单中的单文件点击和拖拽上传
    HTML5存储之 indexedDB
    JavaScript中的 offset, client,scroll
    js 获取 url 里面的参数
  • 原文地址:https://www.cnblogs.com/gyli20170901/p/9797240.html
Copyright © 2011-2022 走看看