当前位置:网站首页>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);
});
}
参考资料:
边栏推荐
- "MySQL Advanced Chapter" four, the storage structure of the index
- MySQL - Function and Constraint Commands
- CTFSHOW command execution [web29-web124] unfinished to be continued
- Detailed MySQL-Explain
- Obtain geographic location and coordinates according to ip address (offline method)
- 正则表达式语法详解及实用实例
- 史上超强最常用SQL语句大全
- C# WPF中监听窗口大小变化事件
- MySQL 数据类型及占用空间
- 第一个WebAssembly程序
猜你喜欢

protobuf编码及网络通信应用(一)

JVM学习(二) 垃圾收集器

SSTI range
![[MATLAB]图像处理——交通标志的识别](/img/45/1a5797a17ebf6db965a64c85e0f037.png)
[MATLAB]图像处理——交通标志的识别

mysql is not an internal or external command, nor is it a runnable program or batch file to resolve

npm run serve starts error npm ERR Missing script "serve"

3 minutes to tell you how to become a hacker | Zero foundation to hacker introductory guide, you only need to master these five skills
![CTFSHOW command execution [web29-web124] unfinished to be continued](/img/89/786fbe65af4c9f269530bf2d08e1a0.png)
CTFSHOW command execution [web29-web124] unfinished to be continued

【Spark】Spark 高频面试题英语版(1)

mysql删除表中重复数据,(只保留一行)
随机推荐
mysql is not an internal or external command, nor is it a runnable program or batch file to resolve
uni-app installs components using npm commands
sql concat()函数
A Spark task tuning 】 【 one day suddenly slow down how to solve
MySQL - Multi-table query and case detailed explanation
DVWA installation tutorial (understand what you don't understand · in detail)
oracle row to column, column to row summary
shardingsphere 分库分表及配置示例
Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)
npm安装和npm安装——保存
CTF misc-audio and video steganography
根据ip地址获取地理位置及坐标(离线方式)
MySQL achievement method 】 【 5 words, single table SQL queries
Arrays工具类的使用
Extraction of BaseDAO
Awd summary
Online sql editing query tool sql-editor
[PASECA2019]honey_shop
MySQL 5.7 安装教程(全步骤、保姆级教程)
SQL Server database generation and execution of SQL scripts