当前位置:网站首页>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);
});
}
参考资料:
边栏推荐
- 网上说的挖矿究竟是什么? 挖矿系统开发详解介绍
- Understand JDBC in one article
- DVWA installation tutorial (understand what you don't understand · in detail)
- kali is an essential artifact for information security
- SQL Server安装教程
- SQL Server 数据库之生成与执行 SQL 脚本
- uni-app installs components using npm commands
- misc-file steganography of CTF
- 在线sql编辑查询工具sql-editor
- Mysql 客户端常见异常分析
猜你喜欢

FastAPI 快速入门

《MySQL高级篇》四、索引的存储结构

MySQL - Function and Constraint Commands

uni-app installs components using npm commands

MySQL window function

3 minutes to tell you how to become a hacker | Zero foundation to hacker introductory guide, you only need to master these five skills

Detailed MySQL-Explain

Remember a Mailpress plugin RCE vulnerability recurrence
Misc of CTF - other types of steganography

SQL Server database generation and execution of SQL scripts
随机推荐
kali is an essential artifact for information security
[MATLAB] Image Processing - Recognition of Traffic Signs
sql中 exists的用法
Misc of CTF-Memory Analysis (Volatility)
Reasons and solutions for Invalid bound statement (not found)
CTF misc-audio and video steganography
sqli-labs shooting range SQL injection learning Less-1
GraphQL(一)基础介绍及应用示例
phpok website vulnerability exploitation analysis
misc-file steganography of CTF
FastAPI 快速入门
SSTI range
SQL Server安装教程
2022CISCNmisc
《MySQL高级篇》四、索引的存储结构
Thread state of five
C#下大批量一键空投实现
TDengineGUI无法连接TDengine
批量自动归集
互联网商城盲盒app为何如此火爆