• Java8函数式接口的使用


    参考;https://blog.csdn.net/icarusliu/article/details/79495534

    例子1:

        public  static void   testflatmap(){
            Stream<String> s = Stream.of("test", "t1", "t2", "teeeee", "aaaa");
            s.flatMap(n -> Stream.of(n.split(""))).forEach(System.out::println);
        }
    
        /**
         * BiFunction<Integer,Integer,Integer>  两个输入参数Integer,Integer  ,一个输出参数
         * 有输入,输出 (x,y)->x+y
         */
        public  static Integer   testfuntion(BiFunction<Integer,Integer,Integer> f, int a, int b){
    
            return   f.apply(a,b);
        }
    
        /**
         *一个输入,一个输出参数
         */
        public  static Integer   testfuntion2(Function<Integer,Integer> f, int a){
            return   f.apply(a);
        }
    
        /**
         *一个输入,没有输出 Consumer  (x)->x*4
         * @param f
         * @param a
         * @return
         */
        public  static void   testfuntion3(Consumer<Integer> f, int a){
            f.accept(a);
        }
    
        /**
         * 2个输入,没有输出 BiConsumer (x,y)->print(x+y)
         * @return
         */
        public  static void   testfuntion3(BiConsumer<Integer,Integer> f, int a,int b){
               f.accept(a,b);
        }
    
        public static void main(String[] args) {
    //        consumerTest();
    //        testflatmap();
            Integer result = testfuntion((x, y) -> x + y, 1, 3);
            System.out.println(result);
        }

    Java函数式接口:

    1 Consumer
    Consumer是一个函数式编程接口; 顾名思义,Consumer的意思就是消费,即针对某个东西我们来使用它,因此它包含有一个有输入而无输出的accept接口方法
    除accept方法,它还包含有andThen这个方法;
    其定义如下:

    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }

    可见这个方法就是指定在调用当前Consumer后是否还要调用其它的Consumer;
    使用示例:

    public static void consumerTest() {
        Consumer f = System.out::println;
        Consumer f2 = n -> System.out.println(n + "-F2");
    
        //执行完F后再执行F2的Accept方法
        f.andThen(f2).accept("test");
    
        //连续执行F的Accept方法
        f.andThen(f).andThen(f).andThen(f).accept("test1");
    }


    2 Function
    Function也是一个函数式编程接口;它代表的含义是“函数”,而函数经常是有输入输出的,因此它含有一个apply方法,包含一个输入与一个输出
    除apply方法外,它还有compose与andThen及indentity三个方法,其使用见下述示例;

    /**
     * Function测试
     */
    public static void functionTest() {
        Function<Integer, Integer> f = s -> s++;
        Function<Integer, Integer> g = s -> s * 2;
    
        /**
         * 下面表示在执行F时,先执行G,并且执行F时使用G的输出当作输入。
         * 相当于以下代码:
         * Integer a = g.apply(1);
         * System.out.println(f.apply(a));
         */
        System.out.println(f.compose(g).apply(1));
    
        /**
         * 表示执行F的Apply后使用其返回的值当作输入再执行G的Apply;
         * 相当于以下代码
         * Integer a = f.apply(1);
         * System.out.println(g.apply(a));
         */
        System.out.println(f.andThen(g).apply(1));
    
        /**
         * identity方法会返回一个不进行任何处理的Function,即输出与输入值相等; 
         */
        System.out.println(Function.identity().apply("a"));
    }

    3 Predicate
    Predicate为函数式接口,predicate的中文意思是“断定”,即判断的意思,判断某个东西是否满足某种条件; 因此它包含test方法,根据输入值来做逻辑判断,其结果为True或者False。
    它的使用方法示例如下:

    /**
     * Predicate测试
     */
    private static void predicateTest() {
        Predicate<String> p = o -> o.equals("test");
        Predicate<String> g = o -> o.startsWith("t");
    
        /**
         * negate: 用于对原来的Predicate做取反处理;
         * 如当调用p.test("test")为True时,调用p.negate().test("test")就会是False;
         */
        Assert.assertFalse(p.negate().test("test"));
    
        /**
         * and: 针对同一输入值,多个Predicate均返回True时返回True,否则返回False;
         */
        Assert.assertTrue(p.and(g).test("test"));
    
        /**
         * or: 针对同一输入值,多个Predicate只要有一个返回True则返回True,否则返回False
         */
        Assert.assertTrue(p.or(g).test("ta"));
    }
  • 相关阅读:
    F. Journey
    D. Divide
    C. Counting Pair
    A. A Big Dinner
    E
    D -Sale
    第十三课 历史记录画笔工具
    第十二课 文字工具
    第十一课 模糊工具、海绵工具、仿制图章工具
    第十课 切片工具 修复画笔工具 修补工具 颜色替换工具
  • 原文地址:https://www.cnblogs.com/lshan/p/12527360.html
Copyright © 2020-2023  润新知