当前位置:网站首页>Lambda表达式:一篇文章带你通透
Lambda表达式:一篇文章带你通透
2022-07-02 22:09:00 【小趴菜学java】
一,JDK8引入Lambda表达式
JDk1.8以后接口中可以存在普通方法,不仅仅只有抽象方法,该方法是被 default 修饰的,并且该方法,他的实现子类都可以调用该方法。
在接口中定义普通方法,一般是用来修改之前版本的存在的不足。开发人员几乎使用不上。
dafault关键字在接口中表示普通方法,不能省略。

二,函数式接口:一个接口有且只有一个抽象方法,就为函数式接口。
使用@FunctionalInterface来检验是否是函数式接口,只能有一个,多了不行,少了也不行

1,通过匿名内部类,来实现函数式接口


2,Lambda表达式,来实现该接口


三,lambda表达式
Lambda表达式是基于函数是接口,如果接口中有多个或者没有接口,就不会一一对应的找到了该接口中的抽象方法。Lambda表达式就会变易出错。
只保留方法的参数和方法体的是实现。
Lambda表达式的四种形式:
1,无返回值,无参数

2,无返回值有参数

3,有返回值无参数

4,无返回值无参数
四,Lambda表达式的特点:
1,如果方法的实现只有一行代码,可以省略{}

2,若方法的参数只有一个,那么小括号可以省略
3,可以省略表达式参数的类型(要么全省略,要么全部不省略)

4,如果方法体有返回值并且方法体只有一行,可以省略{},return

边栏推荐
- [Luogu p1541] tortoise chess [DP]
- Gas station [problem analysis - > problem conversion - > greed]
- pytorch训练CPU占用持续增长(bug)
- 【喜欢的诗词】好了歌
- WebRTC音视频采集和播放示例及MediaStream媒体流解析
- Higher order operation of bits
- Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
- [micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
- Mathematical modeling -- graph and network models and methods (I)
- How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
猜你喜欢

Graphic view frame

LeetCode 968. Monitor binary tree
![[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?](/img/b5/e746dd115995e82c93f667c58a601c.png)
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?

性能优化----严苛模式
![[LeetCode] 反转字符串中的单词 III【557】](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[LeetCode] 反转字符串中的单词 III【557】

Oracle cursor

LeetCode 968. 监控二叉树

Niuke: Dragon and dungeon games

海思3559万能平台搭建:在截获的YUV图像上旋转操作
![NC24325 [USACO 2012 Mar S]Flowerpot](/img/cf/86acbcb524b3af0999ce887c877781.png)
NC24325 [USACO 2012 Mar S]Flowerpot
随机推荐
U++ learning notes - relaxation
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
Introduction and response to high concurrency
Go语言sqlx库操作SQLite3数据库增删改查
Jatpack------LiveData
[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
Qt QProgressBar详解
UE4 UI adaptive screen
NC50965 Largest Rectangle in a Histogram
Array advanced improvement
LeetCode 968. 监控二叉树
[leetcode] reverse string [344]
Jielizhi, production line assembly link [chapter]
悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
存储单位换算
[leetcode] number of palindromes [9]
The threshold value of fusing proportion cannot be changed with sentinel, and setting the slow call proportion has no effect
To myself who is about to work
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
位的高阶运算