浅谈Java中Lambda表达式的相关操作
作者:红旗下的小兵 时间:2023-09-17 12:00:58
为什么要使用Lambda?
可以对一个接口进行非常简洁的实现。
Lambda对接口的要求?
接口中定义的抽象方法有且只有一个才可以。
传统实现一个接口需要这样做:
方法一:
// 实现接口,同时必须重写接口中抽象方法
class Test implements IntrfacefN {
@Override
public void getUser(int a, int b) {
}
}
// @FunctionalInterface 注解意思:函数式接口,用来做规范,有这个注解,说明此接口有且只有一个抽象方法!!!
@FunctionalInterface
interface IntrfacefN{
public void getUser(int a, int b);
}
方法二:
匿名表达式
public class Lamda {
public static void main(String[] args) {
// 匿名表达式实现接口
IntrfacefN intrfacefN1 = new IntrfacefN(){
@Override
public void getUser(int a, int b) {
}
};
}
}
使用Lambda -> 只关注参数和方法体(返回值类型不需要写、类型不需要写)
public class Lamda {
public static void main(String[] args) {
// 实现接口,后边匿名函数就是重写的方法!
IntrfacefN intrfacefN = (int a, int b) -> System.out.println(a-b);
intrfacefN.getUser(1, 2);
}
}
不定参
@FunctionalInterface
interface IntrfacefN{
public void getUser(int... a);
}
public class Lamda {
public static void main(String[] args) {
IntrfacefN intrfacefN = (int ...a) -> {
for (int i = 0; i < a.length; i ++) {
System.out.println(a[i]);
}
};
intrfacefN.getUser(1, 2);
}
}
可省略的部分
参数类型
IntrfacefN intrfacefN = (a, b) -> System.out.println(a-b);
小括号
前提只有一个参数情况
IntrfacefN intrfacefN = a -> System.out.println(a);
方法大括号
方法体只有一句代码
IntrfacefN intrfacefN = (a, b) -> System.out.println(a-b);
返回return
如果大括号中只有一条返回语句,则return 也可以省略
IntrfacefN intrfacefN = (a, b) -> {
return a-b
};
// 省略之后写法:
IntrfacefN intrfacefN = (a, b) -> a-b;
高级部分
方法的引用
将一个Lambda表达式的实现指向一个已实现的方法,这样做相当于公共逻辑部分的抽离,实现复用。
public class Lamda {
public static void main(String[] args) {
IntrfacefN intrfacefN = (a, b) -> add(a, b);
intrfacefN.getUser(1, 2);
}
public static void add(int a, int b) {
System.out.println(a+b);
}
}
@FunctionalInterface
interface IntrfacefN{
public void getUser(int a, int b);
}
还有更简洁的实现:
方法隶属者:语法 - 方法隶属者::方法名
补充下:这个方法隶属者,主要看方法是类方法还是对象方法,如果是类 - 方法类::方法名 ,如果是对象方法 - new 方法类::方法名
public class Lamda {
public static void main(String[] args) {
IntrfacefN intrfacefN = Lamda::add;
intrfacefN.getUser(1, 2);
}
public static void add(int a, int b) {
System.out.println(a+b);
}
}
@FunctionalInterface
interface IntrfacefN{
public void getUser(int a, int b);
}
来源:https://blog.csdn.net/qq_42778001/article/details/118354490
标签:Java,Lambda,表达式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现在子线程中更新Activity中UI的方法
2022-12-16 23:57:46
C# 泛型集合的自定义类型排序的实现
2022-11-18 20:35:32
![](https://img.aspxhome.com/file/2023/3/87033_0s.png)
SpringMVC4 + MyBatis3 + SQL Server 2014整合教程(含增删改查分页)
2023-09-27 01:47:32
![](https://img.aspxhome.com/file/2023/6/89666_0s.png)
Spring bean配置单例或多例模式方式
2023-01-18 04:03:57
![](https://img.aspxhome.com/file/2023/3/63643_0s.jpg)
C#下实现创建和删除目录的实例代码
2021-10-21 16:04:31
详解LeakCanary分析内存泄露如何实现
2022-12-03 22:04:40
![](https://img.aspxhome.com/file/2023/3/105983_0s.png)
如何使用C#修改本地Windows系统时间
2022-05-06 08:23:00
![](https://img.aspxhome.com/file/2023/6/77276_0s.png)
Spring框架构造注入type属性实例详解
2021-10-04 19:45:19
![](https://img.aspxhome.com/file/2023/6/105966_0s.png)
android判断手机是否安装地图应用实现跳转到该地图应用
2022-11-30 11:01:45
![](https://img.aspxhome.com/file/2023/3/138573_0s.png)
基于spring 方法级缓存的多种实现
2022-12-14 18:31:38
SimpleCommand实现图片下载(二)
2023-05-21 00:25:17
springboot中使用FastJson解决long类型在js中失去精度的问题
2022-04-05 05:15:27
Minio与SpringBoot使用okhttp3问题解决
2021-06-25 19:17:08
![](https://img.aspxhome.com/file/2023/1/83501_0s.jpg)
Android BottomNavigationView结合ViewPager实现底部导航栏步骤详解
2021-09-01 00:42:38
Android系统view与SurfaceView的基本使用及区别分析
2023-04-08 00:33:26
![](https://img.aspxhome.com/file/2023/2/104622_0s.png)
Jenkins Pipeline 部署 SpringBoot 应用的教程详解
2022-09-26 14:06:27
![](https://img.aspxhome.com/file/2023/0/61240_0s.png)
Java中JDBC连接池的基本原理及实现方式
2022-10-17 10:27:03
![](https://img.aspxhome.com/file/2023/8/88758_0s.jpg)
redis实现队列的阻塞、延时、发布和订阅
2021-07-02 10:56:19
![](https://img.aspxhome.com/file/2023/3/122323_0s.png)
C#实现对字符串进行大小写切换的方法
2021-07-24 03:30:30
JAVA基于Arrays.sort()实现数组升序和降序
2022-03-11 13:09:48