首页 > 其他分享 >Lambda表达式Stream流的用法示例

Lambda表达式Stream流的用法示例

时间:2022-11-24 08:33:06浏览次数:41  
标签:Stream stream 示例 List filter collect isNotNull Lambda

1、使用简单示例:

@Override
public List<CategoryEntity> listWithTree() {
//1、查询所有的分类数据
List<CategoryEntity> entities = categoryDao.selectList(null);
//2、组装成父子的树形结构
//2.1、找到所有的一级分类
//List<CategoryEntity> parentCollect = entities.stream().filter(CategoryEntity -> CategoryEntity.getParentCid() == 0).collect(Collectors.toList());

List<CategoryEntity> collect = entities.stream().filter(s -> {
return s.getParentCid() == 0;
}).collect(Collectors.toList());
return collect;
}

2、元素判空,避免空指针:

List<FtqDTO> list = environmentMonitoringService.queryElevatorStatus(stationId,regionName,deviceType,deviceName);

//判断设备是否在线
List<FtqDTO> collect = list.stream().filter(s-> {
boolean isNotNull= false;
if(s.getModifyTime() != null) {
isNotNull = deviceOnLine.isIn24H(s.getModifyTime());
}
return isNotNull;
}).collect(Collectors.toList());
 

 

标签:Stream,stream,示例,List,filter,collect,isNotNull,Lambda
From: https://www.cnblogs.com/sensenh/p/16920739.html

相关文章