当前位置:网站首页>Function 函数式接口及应用
Function 函数式接口及应用
2022-07-30 05:43:00 【若明天不见】
Lambda表达式和函数式编程接口
Lambda表达式是整个Java 8体系中最让人期待的特性(Java 8 新特性终极指南),它允许我们將函数作为一个方法的参数传递到方法体中或者將一段代码作为数据,这些概念有过函数式编程经验的会比较熟悉,很多基于Java虚拟机平台的语言(Groovy、Scala…)都引入了Lambda表达式。
Arrays.asList( "a", "b", "d" ).forEach( e -> System.out.println( e ) );
语言设计者们在现存的功能中如何更友好的支持Lambda表达式上做出了大量的思考。因此出现了函数式接口的概念,一个函数接口中只能声明一个单独的函数。因此,它可以隐式的转换为Lambda表达式。java.lang.Runnable和java.util.concurrent.Callable就是两个很好的函数式接口的例子。
Lambda表达式是Java 8最大的卖点,它潜在的吸引越来越多的开发者使用这个伟大的平台并且提供了先进的纯Java支持的函数式编程的概念。更多细节请参考官方文档,也可参考另一篇浅显易懂的文章。
Function 函数式接口
使用注解@FunctionalInterface
标识,并且只包含一个抽象方法的接口是函数式接口。函数式接口主要分为Supplier
供给型函数、Consumer
消费型函数、Runnable
无参无返回型函数和Function
有参有返回型函数。
Function
可以看作转换型函数,表现形式为接收一个参数,并返回一个值。Supplier
、Consumer
和Runnable
可以看作Function
的一种特殊表现形式
Supplier供给型函数
Supplier
的表现形式为不接受参数、只返回数据
Consumer消费型函数
Consumer
消费型函数和Supplier
刚好相反。Consumer
接收一个参数,没有返回值
Runnable无参无返回型函数
Runnable
的表现形式为即没有参数也没有返回值
Function 函数式接口应用
使用Function
函数式接口来通用业务封装,调用时将函数作为参数传递,即可封装对应的业务异常及运行时异常
public static <T> Result<T> of(Logger log, String interfaceName, Object params, Supplier<T> supplier) {
try {
return Result.success(supplier.get());
} catch (QueryException e) {
ResultCode code = ResultCode.getMessageByCode(e.getRescode());
if (code.isLogError()) {
log.error("invoke error, interfaceName:{}, params:{}, rescode:{}", interfaceName, params, e.getRescode());
} else {
log.warn("invoke error, interfaceName:{}, params:{}, rescode:{}", interfaceName, params, e.getRescode());
}
return Result.error(e.getRescode(), code.getMsg());
} catch (ResultException e) {
log.error("result column mapping error, interfaceName:{}, params:{}", interfaceName, params, e);
return Result.error(ResultCode.ERROR);
} catch (Exception e) {
log.error("invoke error, interfaceName:{}, params:{}, msg:{}", interfaceName, params, e.getMessage(), e);
return Result.error(ResultCode.SERVER_ERROR);
}
}
函数调用
public Result<Info> getInfoById(Long id, Long num) {
return SaWebdbResult.of(LOGGER, "getInfoById", Arrays.asList(id, num), () -> {
Map<Long, Info> infoMap = getInfoByIdEx(Lists.newArrayList(id), num);
return infoMap.get(id);
});
}
参考资料:
边栏推荐
- Misc of CTF - other types of steganography
- Misc of CTF-image steganography
- mysql is not an internal or external command, nor is it a runnable program or batch file to resolve
- "MySQL Advanced Chapter" four, the storage structure of the index
- A Spark task tuning 】 【 one day suddenly slow down how to solve
- PHP-fpm
- CTF misc-audio and video steganography
- vulnhub-XXE ctf security question
- sql中 exists的用法
- Blind injection, error injection, wide byte injection, stack injection study notes
猜你喜欢
随机推荐
The number of warehouse 】 data quality
[HCTF 2018]admin
Volatility memory forensics - command shows
Jackson serialization failure problem - oracle data return type can't find the corresponding Serializer
Powerhouse Cup Preliminary WP
Flink PostgreSQL CDC配置和常见问题
"MySQL Advanced Chapter" four, the storage structure of the index
Misc of CTF-Memory Analysis (Volatility)
MySQL存储引擎
SSTI range
【数仓】数据仓库高频面试题题英文版(1)
A Spark task tuning 】 【 one day suddenly slow down how to solve
[Mini Program Project Development--Jingdong Mall] Classification Navigation Area of uni-app
uncategorized SQLException; SQL state [null]; error code [0]; sql injection violation, syntax error
标准输入输出流(System.in,System.out)
Deserialization character escape
CTF之misc-内存分析(Volatility)
FastAPI Quick Start
CTF之misc-音视频隐写
冒泡排序、选择排序、插入排序、快速排序