Java Lambda List转Map代码实例
作者:龙凌云端 时间:2022-05-24 20:15:42
在有些开发场景,需要对 List 对象列表进行过滤处理,并将有用的数据存放到Map中。
例如:告警对象,包含告警uuid(alarmUuid) 和 设备uuid(objUuid),需要对 objUuid = -1的告警进行过滤,并将过滤后告警数据的alarmUuid和 objUuid以键值对的形式保存到Map中。
1、告警对象定义如下:
/**
* Created by Miracle Luna on 2020/3/16
*/
public class AlarmInfoResponse {
private String alarmUuid;
private String objUuid;
public AlarmInfoResponse(String alarmUuid, String objUuid) {
this.alarmUuid = alarmUuid;
this.objUuid = objUuid;
}
public String getAlarmUuid() {
return alarmUuid;
}
public void setAlarmUuid(String alarmUuid) {
this.alarmUuid = alarmUuid;
}
public String getObjUuid() {
return objUuid;
}
public void setObjUuid(String objUuid) {
this.objUuid = objUuid;
}
@Override
public String toString() {
return "AlarmInfoResponse{" +
"alarmUuid='" + alarmUuid + '\'' +
", objUuid='" + objUuid + '\'' +
'}';
}
}
2、过滤代码如下:
/**
* Created by Miracle Luna on 2020/3/16
*/
public class LambdaFilterListToMap {
public static void main(String[] args) {
List<AlarmInfoResponse> alarmInfoResponseList = new ArrayList<>();
AlarmInfoResponse response0 = new AlarmInfoResponse("alarm0", "-1");
AlarmInfoResponse response1 = new AlarmInfoResponse("alarm1", "1");
AlarmInfoResponse response2 = new AlarmInfoResponse("alarm2", "2");
AlarmInfoResponse response3 = new AlarmInfoResponse("alarm3", "3");
alarmInfoResponseList.add(response0);
alarmInfoResponseList.add(response1);
alarmInfoResponseList.add(response2);
alarmInfoResponseList.add(response3);
// 方式1:先使用foreach遍历(遍历过程中条件判断)
Map<String, String> alarmObjUuidMap1 = new HashMap<>();
alarmInfoResponseList.forEach(alarmInfoResponse -> {
if(!"-1".equals(alarmInfoResponse.getObjUuid())) {
alarmObjUuidMap1.put(alarmInfoResponse.getAlarmUuid(), alarmInfoResponse.getObjUuid());
}
});
System.out.println("============= 方式1 ====================");
alarmObjUuidMap1.forEach((alarmUuid, objUuid) -> System.out.println(alarmUuid + " : " + objUuid));
// 方式2:使用流过滤,再使用foreach遍历
Map<String, String> alarmObjUuidMap2 = new HashMap<>();
alarmInfoResponseList.stream().
filter(alarmInfoResponse -> !"-1".equals(alarmInfoResponse.getObjUuid())).
forEach(alarmInfoResponse -> alarmObjUuidMap2.put(alarmInfoResponse.getAlarmUuid(), alarmInfoResponse.getObjUuid()));
System.out.println("\n============= 方式2 ====================");
alarmObjUuidMap2.forEach((alarmUuid, objUuid) -> System.out.println(alarmUuid + " : " + objUuid));
}
}
3、运行结果如下:
============= 方式1 ====================
alarm2 : 2
alarm1 : 1
alarm3 : 3============= 方式2 ====================
alarm2 : 2
alarm1 : 1
alarm3 : 3
来源:https://www.cnblogs.com/miracle-luna/p/12508281.html
标签:Java,Lambda,List,Map
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring创建Bean的6种方式详解
2022-07-18 19:10:13
![](https://img.aspxhome.com/file/2023/1/69911_0s.png)
JAVA十大排序算法之快速排序详解
2022-06-08 16:09:11
![](https://img.aspxhome.com/file/2023/5/69315_0s.png)
java按钮控件数组实现计算器界面示例分享
2021-09-12 22:37:59
深入理解 Java、Kotlin、Go 的线程和协程
2022-05-04 12:39:14
![](https://img.aspxhome.com/file/2023/8/70288_0s.jpg)
java实现Base64加密解密算法
2023-11-25 08:07:27
MyBatis SpringMVC整合实现步骤详解
2023-02-02 20:33:07
Java concurrency之公平锁(二)_动力节点Java学院整理
2023-11-24 21:14:54
java线程池详解及代码介绍
2023-09-16 07:21:57
![](https://img.aspxhome.com/file/2023/7/76517_0s.png)
java static块和构造函数的实例详解
2021-10-04 07:30:17
利用Spring Boot和JPA创建GraphQL API
2023-04-01 07:11:41
![](https://img.aspxhome.com/file/2023/7/69917_0s.png)
C#异步调用的好处和方法分享
2023-04-08 11:10:56
分析讲解SpringMVC注解配置如何实现
2023-10-30 17:23:58
使用Java实现qq邮箱发送邮件
2023-10-14 18:24:36
![](https://img.aspxhome.com/file/2023/1/58561_0s.jpg)
java中Cookie被禁用后Session追踪问题
2023-10-17 13:44:32
C#中Json反序列化的实现方法
2022-07-16 04:34:19
Java Swing中JTable渲染器与编辑器用法示例
2022-11-02 09:23:13
![](https://img.aspxhome.com/file/2023/1/68181_0s.jpg)
一文带你搞懂Java8的LocalDateTime
2023-11-10 02:02:22
![](https://img.aspxhome.com/file/2023/4/59234_0s.png)
Java多线程之彻底搞懂线程池
2023-12-18 23:54:09
![](https://img.aspxhome.com/file/2023/5/62135_0s.png)
设计模式在Spring框架中的应用汇总
2023-10-22 19:20:09
SpringBoot如何获取Kafka的Topic列表
2023-11-26 16:01:52
![](https://img.aspxhome.com/file/2023/2/60242_0s.png)