java8Stream操作集锦
将List中某个字段取出形成新的List,场景,角色授权
List<Integer> userRoleList = sysUserRoles.stream()
.map(SysUserRole::getRoleId).collect(Collectors.toList());
将List中某几个字段取出形成新的List,场景,list中的数据是从多个表中查出结合而成,需要筛选出两个表中字段相同值不同的数据
for (OrderDeviceVO o :
list) {
List<OrderDeviceDependVO> child = orderDeviceMap.get(o.getId())
.stream().map(
orderDeviceVO -> new OrderDeviceDependVO(orderDeviceVO.getDimName()
,orderDeviceVO.getSourceId()
,orderDeviceVO.getDeviceCode()
,orderDeviceVO.getChannelCode())
)
.collect(Collectors.toList());
}
根据List中某个字段分组
//获得excel数据
List<ExcelEntity> excelEntities = readExcel();
//根据设备编码进行分组
Map<String, List<ExcelEntity>> collect = excelEntities.stream().collect(Collectors.groupingBy(ExcelEntity::getDeviceCode));
根据List中某个字段分组形成Map,场景,List中某一列的值某一个字段值是重复的,需要统计此重复值的数量有多少
Map<String,List<OrderDeviceVO>> orderDeviceMap = list.stream()
.collect(Collectors.groupingBy(OrderDeviceVO::getId));
过滤list中某些不想要的值,形成新的list,过滤条件就是写想要留下的表达式,要不为空的就判断不为空
List<AltgorithmExcelEntity> notnull = center.stream().filter(r-> StringUtils.isNotBlank(r.getTenantName())).collect(Collectors.toList());
对list中某几个字段同时分组,形成新的map
Function<AltgorithmExcelEntity,List<String>> compositeKey = altgorithmExcelEntity -> Arrays.asList(altgorithmExcelEntity.getAlgorithmCode(),altgorithmExcelEntity.getChannelCode(),altgorithmExcelEntity.getDeviceCode());
Map<List<String>,List<AltgorithmExcelEntity>> groupMap =
sublist.stream().collect(Collectors.groupingBy(compositeKey,Collectors.toList()));
将list中某个字段用逗号连接形成字符串并去重
String collect = list.stream().map(ZongExcelEntity::getProductName)
.collect(Collectors.joining(","));
String norepeat = Arrays.stream(collect.split(","))
.distinct()
.collect(Collectors.joining(","));
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示