当前位置:网站首页>lambda expression
lambda expression
2022-07-29 22:35:00 【51CTO】
lambda表达式是java8A new new feature has been introduced,类似于js中的闭包,The purpose is to provide a functional programming-like syntax to simplify our coding
一,lambda基本语法
其实lambdaThe expression is very similar to the original method of writing a method.,只是,lambdaExpressions can make our original approach simpler to code,更加清晰明了
基本结构:
Common basic spelling can have:
二,函数式接口
java中的lambdaExpressions are based on functional interfaces
什么是函数式接口?
函数式接口(FunctionalInterface)就是只有一个方法的接口,The purpose of this type of interface is to perform a single operation,These functional interfaces are generally [email protected]
EG:
Usually we create threads usingRunnable接口就是一个函数式接口
The original way of creating threads:
接下来我们使用lambda表达式创建线程:
Is it simpler and clearer?
注意看Thread的参数,It turns out that we need to pass aRunableImplementing classes or writing methods as anonymous inner classes,但是现在,我们可以使用lambdaexpressions as function arguments,由此可见,Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)
Earlier we wrote a pressjsonArrayA way to sort a field,But the comparator in this method is written in the traditional way,现在我们来用lambda试着简化一下:
原函数:
使用lambda表达式简化代码:
JDK中的函数式接口
In order to directly use the existing class library Lambda 表达式,Java 8 Previously there were some interfaces that had been marked as functional interfaces:
- java.lang.Runnable
- java.util.Comparator
- java.util.concurrent.Callable
- java.io.FileFilter
- java.security.PrivilegedAction
- java.beans.PropertyChangeListener
Java 8 A new package has been added to the java.util.function,Brings commonly used functional interfaces:
- Function<T, R> - 函数:输入 T 输出 R
- BiFunction<T, U, R> - 函数:输入 T 和 U 输出 R 对象
- Predicate - 断言/判断:输入 T 输出 boolean
- BiPredicate<T, U> - 断言/判断:输入 T 和 U 输出 boolean
- Supplier - 生产者:无输入,输出 T
- Consumer - 消费者:输入 T,无输出
- BiConsumer<T, U> - 消费者:输入 T 和 U 无输出
- UnaryOperator - unit operation:输入 T 输出 T
- BinaryOperator - 二元运算:输入 T 和 T 输出 T
Is also the basic types of function is increased the more specific interface,包括:BooleanSupplier, DoubleBinaryOperator, DoubleConsumer, DoubleFunction, DoublePredicate, DoubleSupplier, DoubleToIntFunction, DoubleToLongFunction, DoubleUnaryOperator, IntBinaryOperator, IntConsumer, IntFunction, IntPredicate, IntSupplier, IntToDoubleFunction, IntToLongFunction, IntUnaryOperator, LongBinaryOperator, LongConsumer, LongFunction, LongPredicate, LongSupplier, LongToDoubleFunction, LongToIntFunction, LongUnaryOperator, ToDoubleBiFunction<T, U>, ToDoubleFunction, ToIntBiFunction<T, U>, ToIntFunction, ToLongBiFunction<T, U>, ToLongFunction .Combined with the above functional interface,To these basic types of functional interface by the name of the class can see the effect of the interface
Throughout the above functional interface,In fact, it can be divided into four categories:
- 消费型(有参数,无返回值)
- 生成型(无参数,有返回值)
- 一般型(参数,return value has)
- 断言型(输出布尔型)
边栏推荐
猜你喜欢

D. Rain(思维/线性代数/差分数组)

24小时伦敦金走势图分析

《张卫国的夏天》欢乐来袭,黄磊、刘奕君携手演绎“冤种”兄弟

5V升压充电8.4V芯片

【板栗糖GIS】arcmap—如何在表格空值处进行批量求和

解决reudx中的异步问题 applyMiddleware thunk

南信大提出TIPCB,一个简单但有效的用于基于文本的人员搜索的基于部分的卷积baseline

linux使用脚本安装redis

Official announcement!Suzhou Wujiang Development Zone launches electronic labor contract platform

GTK在drawable区域绘图
随机推荐
GBASE 8s 如何估算索引使用多少空间
PyQt5学习一(环境搭建)
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
Numpy数组处理(二)
方法的传递
品牌广告投放平台的中台化应用与实践
GBASE 8s 通过临时表提升排序性能
亚马逊登录参数metadata1,encryptedPwd逆向分析
容器网络硬核技术内幕 (小结-中)
SAP MIGO 报错-在例程WERT_SIMULIEREN字段NEUER_PREIS中字段溢出
linux install redis using script
Chrome浏览器打印flash log
解决报错 WARNING: IPv4 forwarding is disabled. Networking will not work.
SQL教程之性能不仅仅是查询
容器网络硬核技术内幕 (25) 知微知彰,知柔知刚 (中)
【板栗糖GIS】arcmap—标注太长,如何换行显示
5 V booster charge 8.4 V chip
E. XOR Tree(树形dp/异或/最近公共祖先)
GTK在drawable区域绘图
华为畅享50 Pro评测:HarmonyOS加持 更流畅更安全