zoukankan      html  css  js  c++  java
  • Java-8内置的核心函数式接口接口

      * Java8内置的四大核心函数式接口
         
         * Consumer《T》: 消费型接口
         * void accept(T t);
         *
         * Supplier《T》:供给型接口
         * T get();
         *
         * Function《T, R》: 函数型接口
         * R apply(T t);
         *
         * Predicate《T》: 断言型接口:
         * boolean test(T t);

    public static 《T》 List《T》 filter2(List《T》 list, Predicate《T》 p) {
       List《T》 result = new ArrayList《》();
        for (T e : list) {
           if (p.test(e)) {
             result.add(e);
           }
         }
    return result;
    }

    避免装箱和拆箱

     

    Java泛型仅针对引用类型,如果使用Function,会将代码中的int进行装箱,从而在性能上付出代价。java.util.function包针对基本类型的int、double和long提供支持,当输入或/和输出为基本类型时,可以避免自动装箱的操作。

    核心函数接口

    简化或二元拓展

    基本类型

    Function ,T ->R

    共25个接口

     

    IntFunction,int->R

    LongFunction

    DoubleFunction

    IntToDoubleFunction, int->double

    IntToLongFunction

    LongToDoubleFunction,

    LongToIntFunction,

    DoubleToIntFunction

    DoubleToLongFunction

    ToIntFunction, T->int

    ToDoubleFunction,

    ToLongFunction

    BiFunction ,(T,U) ->R

    ToIntBiFunction,

    ToLongBiFunction,

    ToDoubleBiFunction

    UnaryOperator, T->T

    IntUnaryOperator,

    LongUnaryOperator,

    DoubleUnaryOperator

    BinaryOperator (T,T) ->T 

    IntBinaryOperator,

    LongBinaryOperator,

    DoubleBinaryOperator

    Predicate  T->boolean 

    共5个接口

     

    IntPredicate,

    LongPredicate,

    DoublePredicate

    BiPredicate  (L,R)->boolean

     

    Consumer, T->void

    共8个接口

     

    IntConsumer,

    LongConsumer,

    DoubleConsumer

    BiConsumer  (T,U)->void

    ObjIntConsumer,

    ObjLongConsumer,

    ObjDoubleConsumer

    Supplier ()->T

    共5个接口

     

    BooleanSupplier,

    IntSupplier,

    LongSupplier,

    DoubleSupplier


    package java_8;
        import org.junit.Test;
        import java.util.ArrayList;
        import java.util.Arrays;
        import java.util.List;
        import java.util.function.Consumer;
        import java.util.function.Predicate;
        import java.util.function.Supplier;
        import java.util.function.Function;
       
        public class TestLambda4 {
            //predicate《T》 断言型接口:
            @Test
            public void test4(){
                List《String》 list = Arrays.asList("Hello","Lambda","Go",";
                list = filterStr(list, s-》s.contains("o"));
                list.forEach(System.out::println);
            }
            //需求:将满足条件的字符串添加到集合中
            public List《String》 filterStr(List《String》 list, Predicate《String》 predicate) {
                List《String》 stringList = new ArrayList《》();
                for (String str : list) {
                    if (predicate.test(str))
                        stringList.add(str);
                }
                return stringList;
            }
            //-----------------------------------------------------------
            //Function《T,R》函数型接口
            @Test
            public void test3() {
                String str = strHandler("huangyichun", s -》 s.toUpperCase());
                System.out.println(str);
            }
            //需求用于处理字符串
            public String strHandler(String str, Function《String, String》 fun) {
                return fun.apply(str);
            }
            //-------------------------------------------------
            //Supplier《T》供给型接口:
            @Test
            public void test2() {
                List《Integer》 list = getNumList(10, () -》 (int) (Math.random() * 100));
                list.forEach(System.out::println);
            }
            //需求:产生指定个整数,并放入集合中
            public List《Integer》 getNumList(int num, Supplier《Integer》 sup) {
                List《Integer》 list = new ArrayList《》();
                for (int i = 0; i 《 num; i++) {
                    list.add(sup.get());
                }
                return list;
            }
            //-----------------------------------------------------------
            //Consumer《T》 消费型接口
            @Test
            public void test1() {
                happy(10000, m -》 System.out.println(m));
            }
            public void happy(double money, Consumer《Double》 con) {
                con.accept(money);
            }
        }
    复制代码
  • 相关阅读:
    vue打包报错
    css实现平行四边形
    js计算两个天数的差值
    创建vue项目的第一步——之安装vue 命令更新了
    Vue-router详解路由
    Vue-axios 在vue cli中封装
    jQuery-自己封装的弹框
    vue-上传文件
    vue-axios当只调用vue.js又需要axios请求多时
    Vant-UI移动端时间选择框
  • 原文地址:https://www.cnblogs.com/luckForever/p/7254105.html
Copyright © 2011-2022 走看看