Java8函数式接口取代if-else

Background

  • 函数式接口是JDK8的一个非常重要新特性,使用起来可以极大的简化代码。
  • 函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。
  • 函数式接口可以被隐式转换为 lambda 表达式。

1、四大函数式接口

  • 消费型接口:Consumer
    在这里插入图片描述

  • 供给型接口:Supplier
    在这里插入图片描述

  • 函数型接口:Function
    在这里插入图片描述

  • 断言型接口:Predicate
    在这里插入图片描述

2、处理异常

  • 定义函数
    /**
     * 异常处理接口
     **/
    @FunctionalInterface
    public interface ExHandler {

        /**
         * 异常处理
         *
         * @param message 异常信息
         * @return void
         **/
        void throwMessage(String message);

    }
  • 编写判断方法
    /**
     * 如果参数为 true 则抛出异常
     *
     * @param param 参数
     * @return com.cloudansys.test.WlfUtil.ExHandler
     **/
    public static ExHandler isTrue(boolean param) {

        return (errorMessage) -> {
            if (param) {
                throw new RuntimeException(errorMessage);
            }
        };
    }
  • 使用方式
    @Test
    public void test1() {
        WlfUtil.isTrue(false).throwMessage("wlf产生异常");
    }

3、处理null

  • 定义函数
    /**
     * null 处理接口
     */
    @FunctionalInterface
    public interface NullHandler<T extends Object> {

        /**
         * null 处理
         *
         * @param action     null 时执行消费操作
         * @param nullAction 非 null 时执行其他的操作
         * @return void
         **/
        void handle(Consumer<? super T> action, Runnable nullAction);

    }
  • 编写判断方法
    /**
     * 参数为 true 或 false 时,分别进行不同的操作
     *
     * @param param 参数
     * @return com.cloudansys.test.WlfUtil.NullHandler
     **/
    public static NullHandler<?> isNull(String param) {

        return (consumer, runnable) -> {
            if (null == param) {
                runnable.run();
            } else {
                consumer.accept(param);
            }
        };
    }
  • 使用方式
    @Test
    public void test2() {
        WlfUtil.isTorF(true).handle(() -> {
            System.out.println("是wlf");
        }, () -> {
            System.out.println("不是wlf");
        });
    }

4、处理if

  • 定义函数
    /**
     * if 处理接口
     **/
    @FunctionalInterface
    public interface IfHandler {

        /**
         * if 处理
         *
         * @param trueHandler  为true时要进行的操作
         * @param falseHandler 为false时要进行的操作
         * @return void
         **/
        void handle(Runnable trueHandler, Runnable falseHandler);

    }
  • 编写判断方法
    /**
     * 参数为 true 或 false 时,分别进行不同的操作
     *
     * @param param 参数
     * @return com.cloudansys.test.WlfUtil.IfHandler
     **/
    public static IfHandler isTorF(boolean param) {

        return (trueHandle, falseHandle) -> {
            if (param) {
                trueHandle.run();
            } else {
                falseHandle.run();
            }
        };
    }
  • 使用方式
    @Test
    public void test3() {
        WlfUtil.isNull(null).handle(System.out::println, () -> {
            System.out.println("wlf是null");
        });
    }
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
函数式接口可以帮助解决if-else的问题。在Java中,函数式接口是指只包含一个抽象方法的接口。通过使用lambda表达式或方法引用,可以将代码逻辑作为参数传递给函数式接口的方法,从而避免使用if-else语句。 以下是一个简单的示例,演示如何使用函数式接口来处理if-else逻辑: ```java @FunctionalInterface interface Action { void perform(String input); } class Processor { public static void process(String input, Action action) { action.perform(input); } } public class Main { public static void main(String[] args) { String input = "example"; Processor.process(input, (str) -> { if (str.equals("example")) { System.out.println("Do something for example"); } else if (str.equals("another example")) { System.out.println("Do something for another example"); } else { System.out.println("Do something else"); } }); } } ``` 在上面的示例中,我们定义了一个函数式接口`Action`,它有一个抽象方法`perform`,该方法接受一个String类型的参数。`Processor`类中的`process`方法接受一个`Action`对象作为参数,并调用其`perform`方法。 在`main`方法中,我们使用lambda表达式作为参数传递给`process`方法。根据输入的不同,我们可以定义不同的代码逻辑来执行相应的操作。这样,我们就可以避免使用冗长的if-else语句。 通过使用函数式接口,我们可以将代码逻辑作为参数传递,实现更灵活和可扩展的解决方案。同时,它还提高了代码的可读性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WaiSaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值