作为Java函数式编程爱好者,我们都知道方法引用和 Lambda 表达式都必须被赋值,同时赋值需要类型信息才能使编译器保证类型的正确性。

我们先看一个Lambda代码示例:

  1. -> x.toString()

我们清楚这里返回类型必须是 String,但 x 是什么类型呢?

Lambda 表达式包含类型推导(编译器会自动推导出类型信息,避免了程序员显式地声明),编译器必须能够以某种方式推导出 x 的类型以生成正确的代码。

同样方法引用也存在此问题,假设你要传递 System.out :: println 到你正在编写的方法 ,你怎么知道传递给方法的参数的类型?

为了解决上述问题,Java 8 引入了函数式接口,在 java.util.function 包,它包含一组接口,这些接口是 Lambda 表达式和方法引用的目标类型,每个接口只包含一个抽象方法,称为函数式方法。只有确保接口中有且仅有一个抽象方法,Lambda表达式的类型信息才能顺利地进行推导。

在编写接口时,可以使用 @FunctionalInterface 注解强制执行此函数式方法模式:

  1. 在接口上使用注解 @FunctionalInterface ,一旦使用该注解来定义接口,编译器将会强制检查该接口是否确实有且仅有一个抽象方法,否则将会报错。

    1. @FunctionalInterface
      public interface MyFunction {
       /**
        * 自定义的抽象方法
        */

       void run();
      }
  2. 在函数式接口,有且仅有一个抽象方法,Objectpublic方法除外

    1. @FunctionalInterface
      public interface MyFunction {
       
       /**
        * 自定义的抽象方法
        */

       void run();
       
       /**
        * Object的equals方法
        * @param obj
        * @return
        */

       @Override
       boolean equals(Object obj);
       
       /**
        * Object的toString方法
        * @return
        */

       @Override
       String toString();
       
       /**
        * Object的hashCode方法
        * @return
        */

       @Override
       int hashCode();
       
      }
  3. 在函数式接口中,我们可以使用default修饰符定义默认方法,使用static修饰符定义静态方法

    1. @FunctionalInterface
      public interface MyFunction {
       
       /**
        * 自定义的抽象方法
        */

       void run();
       
       /**
        * static修饰符定义静态方法
        */

          static void staticRun() {
              System.out.println("接口中的静态方法");
          }
       
          /**
           * default修饰符定义默认方法
           */

          default void defaultRun() {
              System.out.println("接口中的默认方法");
          }
          
      }
  • 为大家演示下自定义无泛型的函数式接口测试实例:

    1. /**
      * 自定义的无泛型函数式接口
      */

      @FunctionalInterface
      public interface MyFunction {
       
       /**
        * 自定义的抽象方法
        * @param x
        */

       void run(Integer x);
       
          /**
           * default修饰符定义默认方法
           * @param x
           */

          default void defaultMethod(Integer x) {
              System.out.println("接口中的默认方法,接收参数是:" + x);
          }
          
      }

      /**
      * 测试类
      */

      public class MyFunctionTest {

       @Test
       public void functionTest() {
        test(6, (x) -> System.out.println("接口中的抽象run方法,接收参数是:" + x));
       }
       
       public void test(int n, MyFunction function) {
        System.out.println(n);
        function.defaultMethod(n);
        function.run(n);
       }
       
      }

    输出结果:

    1. 6
      接口中的默认方法,接收参数是:6
      接口中的抽象run方法,接收参数是:6
  • 为大家演示下自定义有泛型的函数式接口测试实例:

    1. /**
       * 自定义的有泛型函数式接口
       */

      @FunctionalInterface
      public interface MyFunctionGeneric<T> {

       /**
        * 转换值
        * @param t
        * @return
        */

       convertValue(T t);
       
      }

      /**
      * 测试类
      */

      public class MyFunctionGenericTest {

       @Test
       public void convertValueTest() {
        String result = toLowerCase((x) -> x.toLowerCase(), "ABC");
        System.out.println(result);
       }
       
       public String toLowerCase(MyFunctionGeneric<String> functionGeneric, String value) {
        return functionGeneric.convertValue(value);
       }
       
      }

    输出结果:

    1. abc

    注意:作为参数传递 Lambda 表达式:为了将 Lambda 表达式作为参数传递,接收Lambda 表达式的参数类型必须是与该 Lambda 表达式兼容的函数式接口
    的类型。

首先总览下四大函数式接口的特点说明:

接口 参数类型 返回类型 方法 说明
Consumer T void void accept(T t) 消费型接口,对类型T参数操作,无返回结果
Supplier T T get() 供给型接口,创造T类型参数
Function T R R apply(T t) 函数型接口,对类型T参数操作,返回R类型参数
Predicate T boolean boolean test(T t) 断言型接口,对类型T进行条件筛选操作

消费型接口Consumer<T>

java.util.function.Consumer<T> 接口是消费一个数据,其数据类型由泛型决定。

接口源码:

  1. package java.util.function;

    import java.util.Objects;

    @FunctionalInterface
    public interface Consumer<T> {

        void accept(T t);

        default Consumer<T> andThen(Consumer<? super T> after) {
            Objects.requireNonNull(after);
            return (T t) -> { accept(t); after.accept(t); };
        }
    }
  1. 抽象方法:void accept(T t),接收并消费一个指定泛型的数据,无需返回结果。
  2. 默认方法:default Consumer<T> andThen(Consumer<? super T> after),如果一个方法的参数和返回值全都是 Consumer 类型,那么就可以实现效果:消费数据的时候,首先做一个操作,然后再做一个操作,实现组合
  1. public class ConsumerTest {

     /**
      * 先计算总分,再计算平均分
      */

     @Test
     public void calculate() {
      Integer[] fraction = new Integer[] { 65, 76, 85, 92, 88, 99 };
      consumer(fraction, x -> System.out.println(Arrays.stream(x).mapToInt(Integer::intValue).sum()),
        y -> System.out.println(Arrays.stream(y).mapToInt(Integer::intValue).average().getAsDouble()));
     }
     
     public void consumer(Integer[] fraction, Consumer<Integer[]> x, Consumer<Integer[]> y) {
      x.andThen(y).accept(fraction);
     }
     
    }

输出结果:

  1. 505
    84.16666666666667

由于Consumerdefault方法所带来的嵌套调用(连锁调用),对行为的抽象的函数式编程理念,展示的淋漓尽致。

其他的消费型函数式接口汇总说明:

接口名称 方法名称 方法签名
DoubleConsumer accept (double) -> void
IntConsumer accept (int) -> void
LongConsumer accept (long) -> void
ObjDoubleConsumer accept (T, double) -> void
ObjIntConsumer accept (T, int) -> void
ObjLongConsumer accept (T, long) -> void

供给型接口Supplier<T>

java.util.function.Supplier<T> 接口仅包含一个无参的方法: T get() ,用来获取一个泛型参数指定类型的对象数据。

接口源码:

  1. package java.util.function;

    @FunctionalInterface
    public interface Supplier<T> {
        get();
    }

由于这是一个函数式接口,意味着对应的Lambda表达式需要对外提供一个符合泛型类型的对象数据。

  1. public class SupplierTest {

     public int getMax(Supplier<Integer> supplier) {
      return supplier.get();
     }
     
     /**
      * 获取数组元素最大值
      */

     @Test
     public void getMaxTest() {
      Integer[] data = new Integer[] { 5, 4, 6, 3, 2, 1 };
      int result = getMax(() -> {
       int max = 0;
       for (int i = 0; i < data.length; i++) {
        max = Math.max(max, data[i]);
       }
       return max;
      });
      System.out.println(result);
     }
     
    }

其他的供给型函数式接口汇总说明:

接口名称 方法名称 方法签名
BooleanSupplier getAsBoolean () -> boolean
DoubleSupplier getAsDouble () -> double
IntSupplier getAsInt () -> int
LongSupplier getAsLong () -> long

函数型接口Function

java.util.function.Function<T,R> 接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。

接口源码:

  1. package java.util.function;

    import java.util.Objects;

    @FunctionalInterface
    public interface Function<T, R> {

        apply(T t);

        default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
            Objects.requireNonNull(before);
            return (V v) -> apply(before.apply(v));
        }

        default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
            Objects.requireNonNull(after);
            return (T t) -> after.apply(apply(t));
        }

        static <T> Function<T, T> identity() {
            return t -> t;
        }
    }
  1. 抽象方法 apply(T t):该方法接收入参是一个泛型T对象,并返回一个泛型T对象。

  2. 默认方法

    andThen(Function<? super R, ? extends V> after):该方法接受一个行为,并将父方法处理过的结果作为参数再处理。

    compose(Function<? super V, ? extends T> before):该方法正好与andThen相反,它是先自己处理然后将结果作为参数传给父方法执行。

    1. @Test
      public void andThenAndComposeTest() {
          // 计算公式相同
          Function<Integer, Integer> andThen1 = x -> x + 1;
          Function<Integer, Integer> andThen2 = x -> x * 2;
          Function<Integer, Integer> compose1 = y -> y + 1;
          Function<Integer, Integer> compose2 = y -> y * 2;
          // 注意调用的先后顺序
          // 传入参数2后,先执行andThen1计算,将结果再传入andThen2计算
          System.out.println(andThen1.andThen(andThen2).apply(2));
          // 传入参数2后,先执行compose2计算,将结果再传入compose1计算
          System.out.println(compose1.compose(compose2).apply(2));
      }

    输出结果:

    1. 6
      5
  3. 静态方法identity():获取到一个输入参数和返回结果一样的Function实例。

来一个自驾九寨沟的代码示例:

  1. public class FunctionTest {
     
     @Test
     public void findByFunctionTest() {
      Function<BigDecimal, BigDecimal> getMoney = m -> m.add(new BigDecimal(1000));
      BigDecimal totalCost = getMoney.apply(new BigDecimal(500));
      System.out.println("张三的钱包原本只有500元,自驾川西得去银行再取1000元,取钱后张三钱包总共有" +           Function.identity().apply(totalCost) + "元");
      BigDecimal surplus = cost(totalCost, (m) -> {
       System.out.println("第二天出发前发现油不足,加油前有" + m + "元");
       BigDecimal lubricate = m.subtract(new BigDecimal(300));
       System.out.println("加油300后还剩余" + lubricate + "元");
       return lubricate;
      }, (m) -> {
       System.out.println("到达景区门口,买景区票前有" + m + "元");
       BigDecimal tickets = m.subtract(new BigDecimal(290));
       System.out.println("买景区票290后还剩余" + tickets + "元");
       return tickets;
      });
      System.out.println("最后张三返程到家还剩余" + surplus + "元");
     }

     public BigDecimal cost(BigDecimal money, Function<BigDecimal, BigDecimal> lubricateCost,
       Function<BigDecimal, BigDecimal> ticketsCost)
     
    {
      Function<BigDecimal, BigDecimal> firstNight = (m) -> {
       System.out.println("第一晚在成都住宿前有" + m + "元");
       BigDecimal first = m.subtract(new BigDecimal(200));
       System.out.println("交完200住宿费还剩余" + first + "元");
       return first;
      };
      Function<BigDecimal, BigDecimal> secondNight = (m) -> {
       System.out.println("第二晚在九寨县住宿前有" + m + "元");
       BigDecimal second = m.subtract(new BigDecimal(200));
       System.out.println("交完200住宿费还剩余" + second + "元");
       return second;
      };
      return lubricateCost.andThen(ticketsCost).andThen(secondNight).compose(firstNight).apply(money);
     }

    }

输出结果:

  1. 张三的钱包原本只有500元,自驾川西得去银行再取1000元,取钱后张三钱包总共有1500
    第一晚在成都住宿前有1500
    交完200住宿费还剩余1300
    第二天出发前发现油不足,加油前有1300
    加油300后还剩余1000
    到达景区门口,买景区票前有1000
    买景区票290后还剩余710
    第二晚在九寨县住宿前有710
    交完200住宿费还剩余510
    最后张三返程到家还剩余510

其他的函数型函数式接口汇总说明:

接口名称 方法名称 方法签名
BiFunction apply (T, U) -> R
DoubleFunction apply (double) -> R
DoubleToIntFunction applyAsInt (double) -> int
DoubleToLongFunction applyAsLong (double) -> long
IntFunction apply (int) -> R
IntToDoubleFunction applyAsDouble (int) -> double
IntToLongFunction applyAsLong (int) -> long
LongFunction apply (long) -> R
LongToDoubleFunction applyAsDouble (long) -> double
LongToIntFunction applyAsInt (long) -> int
ToDoubleFunction applyAsDouble (T) -> double
ToDoubleBiFunction applyAsDouble (T, U) -> double
ToIntFunction applyAsInt (T) -> int
ToIntBiFunction applyAsInt (T, U) -> int
ToLongFunction applyAsLong (T) -> long
ToLongBiFunction applyAsLong (T, U) -> long

断言型接口Predicate<T>

java.util.function.Predicate<T> 接口中包含一个抽象方法: boolean test(T t) ,用于条件判断的场景。默认方法:and or nagte (取反)。

接口源码:

  1. package java.util.function;

    import java.util.Objects;

    @FunctionalInterface
    public interface Predicate<T> {

        boolean test(T t);

        default Predicate<T> and(Predicate<? super T> other) {
            Objects.requireNonNull(other);
            return (t) -> test(t) && other.test(t);
        }

        default Predicate<T> negate() {
            return (t) -> !test(t);
        }

        default Predicate<T> or(Predicate<? super T> other) {
            Objects.requireNonNull(other);
            return (t) -> test(t) || other.test(t);
        }

        static <T> Predicate<T> isEqual(Object targetRef) {
            return (null == targetRef)
                    ? Objects::isNull
                    : object -> targetRef.equals(object);
        }
    }

既然是条件判断,就会存在与、或、非三种常见的逻辑关系。其中将两个 Predicate 条件使用逻辑连接起来实现并且的效果时,类始于 Consumer接口 andThen()函数 其他三个雷同。

  1. public class PredicateTest {
     /**
      * 查找在渝北的Jack
      */

     @Test
     public void findByPredicateTest() {
      List<User> list = Lists.newArrayList(new User("Johnson", "渝北"), new User("Tom", "渝中"), new User("Jack", "渝北"));
      getNameAndAddress(list, (x) -> x.getAddress().equals("渝北"), (x) -> x.getName().equals("Jack"));
     }
     
     public void getNameAndAddress(List<User> users, Predicate<User> name, Predicate<User> address) {
      users.stream().filter(user -> name.and(address).test(user)).forEach(user -> System.out.println(user.toString()));
     }
    }

输出结果:

  1. User [name=Jack, address=渝北]

其他的断言型函数式接口汇总说明:

接口名称 方法名称 方法签名
BiPredicate test (T, U) -> boolean
DoublePredicate test (double) -> boolean
IntPredicate test (int) -> boolean
LongPredicate test (long) -> boolean

Lambda 表达式和方法引用并没有将 Java 转换成函数式语言,而是提供了对函数式编程的支持。这对 Java 来说是一个巨大的改进,因为这允许你编写更简洁明了,易于理解的代码。

版权声明:本文为JohnsonYang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/JohnsonYang/p/13790733.html