• Java函数式接口


    函数式接口

    有且只有一个抽象方法的接口被称为函数式接口。

    @FunctionalInterface注解

    Java 8 为函数式接口引入一个注解 @FunctionalInterface 。该注解用于一个接口的定义上,一旦使用该注解来定义接口,编译器将会强制检查改接口是否确实有且仅有一个抽象方法,否则将会报错。该注解不是必须的,只要符合函数式接口的定义,那改接口就是函数式接口。

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface FunctionalInterface {}
    
    • 1
    • 2
    • 3
    • 4

    函数式接口格式

    修饰符 interface 接口名称{
        //public abstract 可以不写 编译器自动加上
        public abstract 返回值 方法名称(参数列表)
        // 其他方式 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    常用函数式接口

    Function: 函数型接口

    R apply(T t),传入一个参数,返回想要的结果。

      Function<Integer, String> f1 = f -> String.valueOf(f * 3);
      Function<String, Integer> f2 = f -> Integer.valueOf(f) * 3;
    
      System.out.println(f1.apply(2));//6
      System.out.println(f2.apply("2"));//6
    
    • 1
    • 2
    • 3
    • 4
    • 5

    compose(Function before),先执行compose方法参数before中的apply方法,然后将执行结果传递给调用compose函数中的apply方法在执行。

      Function<Integer, String> f1 = f -> String.valueOf(f * 3);
      Function<String, Integer> f2 = f -> Integer.valueOf(f) * 3;
    
      System.out.println(f1.compose(f2).apply("2"));//18
      System.out.println(f2.compose(f1).apply(2));//18
    
    • 1
    • 2
    • 3
    • 4
    • 5

    andThen(Function after),先执行调用andThen函数的apply方法,然后在将执行结果传递给andThen方法after参数中的apply方法在执行。它和compose方法整好是相反的执行顺序。

      Function<Integer, String> f1 = f -> String.valueOf(f * 3);
      Function<String, Integer> f2 = f -> Integer.valueOf(f) * 3;
    
      System.out.println(f1.andThen(f2).apply(2));
      System.out.println(f2.andThen(f1).apply("2"));
    
    • 1
    • 2
    • 3
    • 4
    • 5
      Function<Integer, Integer> f1 = f -> f + 1;
      Function<Integer, Integer> f2 = f -> f * 3;
    
      System.out.println(f1.compose(f2).apply(2));//7 -> f2.apply -> f1.apply -> 2*3 -> 6+1
      System.out.println(f2.compose(f1).apply(2));//9 -> f1.apply -> f2.apply -> 2+1 -> 3*3
    
      System.out.println(f1.andThen(f2).apply(2));//9 -> f1.apply -> f2.apply -> 2+1 -> 3*3
      System.out.println(f2.andThen(f1).apply(2));//7 -> f2.apply -> f1.apply -> 2*3 -> 6+1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Consumer:消费型接口

    void accept(T t),接收一个参数进行消费,但无需返回结果。

     Consumer<String> c1 = s -> {
        String[] split = s.split(",");
        System.out.println("key:" + split[0] + ",value=" + split[1]);
     };
     c1.accept("k,v");//key:k,value=v
    
    • 1
    • 2
    • 3
    • 4
    • 5

    andThen(Consumer after),先消费然后在消费,先执行调用andThen接口的accept方法,然后在执行andThen方法参数after中的accept方法。

      Consumer<String> c1 = s -> {
          System.out.println("c1");
      };
    
      Consumer<String> c2 = s -> {
          System.out.println("c2");
      };
    
      c1.andThen(c2).accept("1");// c1 -> c2
      c2.andThen(c1).accept("1");// c2 -> c1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Supplier: 供给型接口

    T get(),无参数,有返回值。

     Supplier<String> s = () -> "Hello World";
    
     System.out.println(s.get());//Hello World
    
    • 1
    • 2
    • 3

    Predicate : 断言型接口

    boolean test(T t),传入一个参数,返回一个布尔值。

     Predicate<Integer> p1 = p -> p > 0;
    
     System.out.println(p1.test(1));//true
    
    • 1
    • 2
    • 3

    and(Predicate other),相当于逻辑运算符中的&&,当两个Predicate函数的返回结果都为true时才返回true

     Predicate<Integer> p1 = p -> p > 0;
     Predicate<Integer> p2 = p -> p > 1;
    
     System.out.println(p1.and(p2).test(1));//false
     System.out.println(p1.and(p2).test(2));//true
    
    • 1
    • 2
    • 3
    • 4
    • 5

    or(Predicate other) ,相当于逻辑运算符中的||,当两个Predicate函数的返回结果有一个为true则返回true,否则返回false

     Predicate<Integer> p1 = p -> p > 0;
     Predicate<Integer> p2 = p -> p > 1;
    
     System.out.println(p1.or(p2).test(1));//true
     System.out.println(p1.or(p2).test(2));//true
    
    • 1
    • 2
    • 3
    • 4
    • 5

    negate() 方法的意思就是取反

     Predicate<Integer> p1 = p -> p > 0;
     Predicate<Integer> p2 = p -> p > 1;
    
     System.out.println(p1.negate().test(1));//false
     System.out.println(p2.negate().test(2));//false
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Bi类型接口

    BiConsumerBiFunctionBiPrediateConsumerFunctionPredicate 的扩展,可以传入多个参数,没有 BiSupplier 是因为 Supplier 没有入参。在这里插入图片描述

  • 相关阅读:
    css:详解BFC块级格式化上下文
    Linux之权限【读、写、执行】【详细总结】
    2024.4.19作业
    智能垃圾桶丨悦享便捷生活
    2023腾讯云标准型S5云服务器简单测评,比较值!
    python如何将代码制作成可以pip的库,将自己的python代码打包成库,让别人pip安装调用?
    Android 开发——环境搭建
    安装 laravel 遇到的错误和解决方案
    [附源码]JAVA毕业设计户籍管理系统设计(系统+LW)
    入门级微单反性能对比
  • 原文地址:https://blog.csdn.net/D_A_I_H_A_O/article/details/126455951