# Function 函数式接口

使用注解:@FunctionalInterface 标识,并且只包含一个 抽象方法 的接口是 函数式接口函数式接口 主要分为 supplier 供给类型函数,Consumer 消费型函数,Runnable 无参无返回型函数和 Function 有参又返回型函数。

Function 可以看做转换型函数。

supplier 供给型函数

supplier 的表现形式为不接受参数,只返回数据

img

# Supplier 函数

Consumber 消费型函数

Consumber 消费型函数和 Supplier 刚好相反,Consumber 接收一个参数,没有返回值。

img

Consumber 消费型函数

# Runnable 无参无返回型函数

Runnable 的表现形式为既没有参数也没有返回值

img

Runnable 无参无返回型函数

Function 函数的表现形式为接收一个参数,并返回一个值。Supplier,Consumer 和 Runnable 可以看做 Function 的一种特殊表现形式

img

@FunctionalInterface

# 使用小技巧

# 处理抛出异常的 if

  1. 定义函数

    定义一个抛出异常的形式的 函数式接口 ,这个接口只有参数没有返回值是个 消费型接口。

    @FunctionalInterface
    public interface ThrowExceptionFunction
    {
        void throwMessage(String message);
    }
  2. 编写判断方法

    创建工具类,并创建一个 isTrue 方法,方法的返回值为刚才定义的 函数式接口,ThrowExceptionFunction 的接口实现逻辑为当参数 b 为 false 时抛出异常

    public class FunctionUtils
    {
        public static ThrowExceptionFunction isTrue(boolean b)
        {
            return errorMessage ->
            {
                if (!b)
                {
                    throw new RuntimeException(errorMessage);
                }
            };
        }
    }
  3. 使用方式

    调用工具类参数后,调用 函数式接口 的 throwMessage 方法传入异常信息,当出入的参数为 false 是正常执行

    public static void main(String[] args)
    {
       FunctionUtils.isTrue(false).throwMessage("报错了");
    }

    打印结果:

    image-20240402092802512

    public static void main(String[] args)
    {
       FunctionUtils.isTrue(true).throwMessage("报错了");
    }

    image-20240402092838100

# 处理 if 分支操作

  1. 定义函数式接口

    创建一个名为 BranchHandle 的函数式接口,接口的参数为两个 Runnable 接口。这两个 Runnable 的接口分别代表了为 true 或 false 时要进行的操作

    @FunctionalInterface
    public interface BranchHandle
    {
        /**
         *  分支操作
         * @param trueHandle 为 true 时要进行的操作
         * @param falseHandle 为 false 时要进行的操作
         */
        void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);
    }
  2. 编写判断方法

    在工具类创建一个名为 isTrueOrFalse 的方法,方法返回值为刚才定义的 函数式接口 - BranchHandle。

    public static BranchHandle isTrueOrFalse(boolean b)
    {
       return (trueHandle, falseHandle) -> {
          if (b)
          {
             trueHandle.run();
          } else
          {
             falseHandle.run();
          }
       };
    }
  3. 使用方式

    参数为 true 时,执行 trueHandle,参数为 false 时,执行 falseHandle

    public static void main(String[] args)
    {
       FunctionUtils.isTrueOrFalse(true).trueOrFalseHandle(
          () ->
          {
             System.out.println("既然没报错");
          }, () ->
          {
             System.out.println("又报错了");
          }
       );
    }

    打印结果:

    image-20240402093740772

    public static void main(String[] args)
    {
       FunctionUtils.isTrueOrFalse(false).trueOrFalseHandle(
          () ->
          {
             System.out.println("既然没报错");
          }, () ->
          {
             System.out.println("又报错了");
          }
       );
    }

    打印结果:

    image-20240402093815667

# 判断是否有值

  1. 定义函数

    创建一个名为 PresentOrElseHandler 的函数式接口,接口的参数一个为 Consumer 接口。一个为 Runnable 分别代表值不为空时执行消费操作和值为空时执行的其它操作

    @FunctionalInterface
    public interface PresentOrElseHandler<T extends Object>
    {
        /**
         * 值不为空时执行消费操作
         * 值为空时执行其它操作
         * @param action  值不为空时执行消费操作
         * @param emptyAction 值为空时执行其它操作
         */
        void presentOrElseHandle(Consumer<? super T> action, Runnable emptyAction);
    }
  2. 编写判断方法

    在工具类创建一个名为 isBlankOrNoBlank 的方法,方法的返回值为刚才定义的 函数式接口 - PresentOrElseHandler

    public static PresentOrElseHandler<?> isBlankOrNoBlank(String str)
    {
       return (consumer, runnable) ->
       {
          if (str == null || str.length() == 0)
          {
             runnable.run();
          }
          else
          {
             consumer.accept(str);
          }
       };
    }
  3. 使用方式

    调用工具类参数后,调用 函数式接口 的 PresentOrElseHandle 方法传入一个 Consumer 和 Runnable 参数不为空时,打印参数

    public static void main(String[] args)
    {
       FunctionUtils.isBlankOrNoBlank("hello").presentOrElseHandle(System.out::println, () -> {
          System.out.println("报错了");
       });
    }

    打印结果:

    image-20240402094834029

    传入一个空值

    FunctionUtils.isBlankOrNoBlank("").presentOrElseHandle(System.out::println, () -> {
       System.out.println("报错了");
    });

    打印结果:

    image-20240402094923939