当前位置:网站首页>Function functional interface and application
Function functional interface and application
2022-07-30 06:55:00 【If I don't see you tomorrow】
LambdaExpressions and functional programming interfaces
Lambda表达式是整个Java 8The most anticipated feature of the system(Java 8 新特性终极指南),It allows us to pass a function as a method parameter into the method body or a piece of code as data,These concepts will be familiar to those with functional programming experience,很多基于JavaThe language of the virtual machine platform(Groovy、Scala…)都引入了Lambda表达式.
Arrays.asList( "a", "b", "d" ).forEach( e -> System.out.println( e ) );
How language designers can be more friendly to support existing featuresLambdaA lot of thought has been put into expressions.Hence the concept of functional interfaces,Only one single function can be declared in a functional interface.因此,它可以隐式的转换为Lambda表达式.java.lang.Runnable和java.util.concurrent.CallableJust two good examples of functional interfaces.
Lambda表达式是Java 8最大的卖点,It potentially attracts more and more developers to use this great platform and offers advanced pureJavaConcepts supported by functional programming.更多细节请参考官方文档,You can also refer to another easy-to-understand article文章.
Function 函数式接口
使用注解@FunctionalInterface标识,并且只包含一个抽象方法的接口是函数式接口.函数式接口主要分为Supplier供给型函数、Consumer消费型函数、Runnable无参无返回型函数和Function有参有返回型函数.
Function可以看作转换型函数,The form of expression is to receive a parameter,并返回一个值.Supplier、Consumer和Runnable可以看作Function的一种特殊表现形式

Supplier供给型函数
Supplier的表现形式为不接受参数、只返回数据

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

Runnable无参无返回型函数
Runnable的表现形式为即没有参数也没有返回值

Function functional interface application
使用FunctionFunctional interface to general business encapsulation,Pass the function as a parameter when calling,The corresponding business exceptions and runtime exceptions can be encapsulated
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);
});
}
参考资料:
边栏推荐
- uni-app: The use of uni-icons and how to customize icons
- Offensive and defensive world easy_web
- SQL Server database generation and execution of SQL scripts
- [HCTF 2018]admin
- [Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat
- The Request request body is repackaged to solve the problem that the request body can only be obtained once
- Extraction of BaseDAO
- Detailed introduction to the usage of Nacos configuration center
- Arthas 命令解析(watch/tt/sc)
- MySQL - 函数及约束命令
猜你喜欢

Jackson 序列化失败问题-oracle数据返回类型找不到对应的Serializer
Go简单实现协程池
Misc of CTF - other types of steganography

Reasons and solutions for Invalid bound statement (not found)

Monstache执行Monstache - f配置。toml出错不存在处理器类型和名称(附件)(= parse_exc类型

二十一、Kotlin进阶学习:实现简单的网络访问封装

FastAPI Quick Start

Offensive and defensive world easy_web

Blind injection, error injection, wide byte injection, stack injection study notes
Misc of CTF-image steganography
随机推荐
Extraction of BaseDAO
Defense Ideas for a Type of SMS Vulnerability
sql concat()函数
C#中使用OleDb操作access数据库
mysql删除表中重复数据,(只保留一行)
Obtain geographic location and coordinates according to ip address (offline method)
nodejs PM2监控及报警邮件发送(二)
protobuf编码及网络通信应用(一)
[MATLAB]图像处理——交通标志的识别
Request请求体重新封装,解决请求体只能获取一次的问题
oracle行转列、列转行总结
TDengine cluster construction
【SQL】first_value 应用场景 - 首单 or 复购
JVM学习(二) 垃圾收集器
Misc-traffic analysis of CTF
Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc
POI工具类
C#下利用开源NPlot绘制股票十字交叉线
Detailed introduction to the usage of Nacos configuration center
sql concat() function