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(","));

浙公网安备 33010602011771号