有偿问答
面经分享
技术探讨
资料领取
登录
Java 8 中的这个接口真好用!
社长
1年前
⋅ 639 阅读
在开发过程中经常会使用`if...else...`进行判断抛出异常、分支处理等操作。这些`if...else...`充斥在代码中严重影响了代码代码的美观,这时我们可以利用Java 8的Function接口来消灭`if...else...`。 ``` if (...){ throw new RuntimeException("出现异常了"); } if (...){ doSomething(); } else { doOther(); } ``` `Function` 函数式接口 ---------------- 使用注解`@FunctionalInterface`标识,并且只包含一个`抽象方法`的接口是`函数式接口`。`函数式接口`主要分为`Supplier`供给型函数、`Consumer`消费型函数、`Runnable`无参无返回型函数和`Function`有参有返回型函数。 > `Function`可以看作转换型函数 ### `Supplier`供给型函数 `Supplier`的表现形式为不接受参数、只返回数据 ![](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3e3500e28bca4115a02df9bac53b9c2e~tplv-k3u1fbpfcp-watermark.awebp?) ### `Consumer`消费型函数 `Consumer`消费型函数和`Supplier`刚好相反。`Consumer`接收一个参数,没有返回值 ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a3ec2cc9c1f2490b958ca75fafc90536~tplv-k3u1fbpfcp-watermark.awebp?) ### `Runnable`无参无返回型函数 `Runnable`的表现形式为即没有参数也没有返回值 ![](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1f86d3a921f148b7adc997d556105bde~tplv-k3u1fbpfcp-watermark.awebp?) ### `Function`函数的表现形式为接收一个参数,并返回一个值。`Supplier`、`Consumer`和`Runnable`可以看作`Function`的一种特殊表现形式 ![](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b189bb657ddb4fd99bd2167c030fd65d~tplv-k3u1fbpfcp-watermark.awebp?) 使用小技巧 ----- ### 处理抛出异常的 if 1. 定义函数 定义一个抛出异常的形式的`函数式接口`, 这个接口只有参数没有返回值是个`消费型接口` ``` /** * 抛异常接口 **/ @FunctionalInterface public interface ThrowExceptionFunction { /** * 抛出异常信息 * * @param message 异常信息 * @return void **/ void throwMessage(String message); } ``` 2. 编写判断方法 创建工具类`VUtils`并创建一个`isTure`方法,方法的返回值为刚才定义的`函数式接口`-`ThrowExceptionFunction`。`ThrowExceptionFunction`的接口实现逻辑为当参数`b`为`true`时抛出异常 ``` /** * 如果参数为true抛出异常 * * @param b * @return com.example.demo.func.ThrowExceptionFunction **/ public static ThrowExceptionFunction isTure(boolean b){ return (errorMessage) -> { if (b){ throw new RuntimeException(errorMessage); } }; } ``` 3. 使用方式 调用工具类参数参数后,调用`函数式接口`的`throwMessage`方法传入异常信息。 当出入的参数为`false`时正常执行 ![](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4011a765bc944e7b8b32cfc5139908fe~tplv-k3u1fbpfcp-watermark.awebp?) 当出入的参数为`true`时抛出异常 ![](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/dc024cf67dac43bca7ad54c5f742790d~tplv-k3u1fbpfcp-watermark.awebp?) ### 处理 if 分支操作 1. 定义函数式接口 创建一个名为`BranchHandle`的函数式接口,接口的参数为两个`Runnable`接口。这两个两个`Runnable`接口分别代表了为`true`或`false`时要进行的操作 ``` /** * 分支处理接口 **/ @FunctionalInterface public interface BranchHandle { /** * 分支操作 * * @param trueHandle 为true时要进行的操作 * @param falseHandle 为false时要进行的操作 * @return void **/ void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle); } ``` 2. 编写判断方法 创建一个名为`isTureOrFalse`的方法,方法的返回值为刚才定义的`函数式接口`-`BranchHandle`。 ``` /** * 参数为true或false时,分别进行不同的操作 * * @param b * @return com.example.demo.func.BranchHandle **/ public static BranchHandle isTureOrFalse(boolean b){ return (trueHandle, falseHandle) -> { if (b){ trueHandle.run(); } else { falseHandle.run(); } }; } ``` 3. 使用方式 参数为`true`时,执行`trueHandle` ![](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4600bf82d0844698afc60176eeee955e~tplv-k3u1fbpfcp-watermark.awebp?) 参数为`false`时,执行`falseHandle` ![](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3c402e1b93f94ff3b84653dc875ab556~tplv-k3u1fbpfcp-watermark.awebp?) ### 如果存在值执行消费操作,否则执行基于空的操作 1. 定义函数 创建一个名为`PresentOrElseHandler`的函数式接口,接口的参数一个为`Consumer`接口。一个为`Runnable`, 分别代表值不为空时执行消费操作和值为空时执行的其他操作 ``` /** * 空值与非空值分支处理 */ public interface PresentOrElseHandler
{ /** * 值不为空时执行消费操作 * 值为空时执行其他的操作 * * @param action 值不为空时,执行的消费操作 * @param emptyAction 值为空时,执行的操作 * @return void **/ void presentOrElseHandle(Consumer super T> action, Runnable emptyAction); } ``` 2. 编写判断方法 创建一个名为`isBlankOrNoBlank`的方法,方法的返回值为刚才定义的`函数式接口`-`PresentOrElseHandler`。 ``` /** * 参数为true或false时,分别进行不同的操作 * * @param b * @return com.example.demo.func.BranchHandle **/ 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` 参数不为空时,打印参数 ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/273afa8119ed4e79a6c8b1bde1df826a~tplv-k3u1fbpfcp-watermark.awebp?) 参数不为空时 ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0e8c356c60604fb4ae80b8ef5f974fa9~tplv-k3u1fbpfcp-watermark.awebp?) 结尾 -- `Function`函数式接口是`java 8`非常重要的特性,利用好`Function`函数可以极大的简化代码。 > 作者:不可食用盐 > > https://juejin.cn/post/7011435192803917831
阅读全部
全部评论:
0
条
我有话说:
@
发送
-- 目录 --
关注官方公众号:
Java问答社
接收最新有赏问答推送!
最新发布
1.
SpringBoot 接口数据加解密技巧,so easy!
2.
一个依赖搞定 Spring Boot 反爬虫,防止接口盗刷!
3.
Java8 Stream 极大简化了代码,它是如何实现的?
4.
马上大四了,秋招还是春招好?先找工作还是找实习?
5.
万字详解 Linux 常用指令(值得收藏)
6.
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
最新评论
部署文档没有了,您能提供下吗
部署文档没有了,能提供下吗
我测你的🐎
源码从哪里获取请问
想学
那篇石墨文档 没有权限查看哇