分组
对list分组,并返回Map结构
Map<String, List<Equipment>> collect = equipmentList.stream().collect(Collectors.groupingBy(Equipment::getState));
两次分组,返回双重的map结构:
Map<EquipmentIndexType, Map<EquipmentState, List<Equipment>>> collect = equipmentList.stream().collect(Collectors.groupingBy(Equipment::getIndexType, Collectors.groupingBy(v -> v.getState())));
分组后根据某个属性再转换为map
Map<EquipmentIndexType, Map<String, VO>> indexTypeMapMap = statisticMapper.getSafeMonthAlarmAnalysis(reqVO).stream().collect(Collectors.groupingBy(VO::getIndexType,Collectors.toMap(VO::getMonth, v -> v)));
对list分组,并返回各个分组的统计个数
Map<String, Long> map = equipmentList.stream().collect(Collectors.groupingBy(Equipment::getType, Collectors.counting()));
多个字段分组
Map<String, List<TbSapBankFlowDictionary>> sapDicMap = sapDictionaryList.stream().collect(Collectors.groupingBy(v -> v.getPayCode() + "_" + v.getReceiveCode()));
过滤:
条件过滤
taskList.stream().filter(v -> null != v.getEndTime()).collect(Collectors.toList());
条件过滤并统计复合条件的个数
taskList.stream().filter(v -> null != v.getEndTime()).collect(Collectors.toList()).size();
去重
distinct()
String voucherNo = res.getETFIDOC().getItem().stream().map(ZFZETFIDOC::getBELNR).distinct().collect(Collectors.joining(","));
实体转换
一种list实体类转为另一种list实体类
List<SysOrganizationUser> organizationUserList = v.stream().map(orgId -> {
SysOrganizationUser organizationUser = new SysOrganizationUser();
organizationUser.setUserId(userInfo.getId());
organizationUser.setOrganizationId(orgId);
organizationUser.setCreateTime(new Date());
organizationUser.setCreateUser(UserUtil.getUserId());
return organizationUser;
}).collect(Collectors.toList());
List<T> 转 Map<String, List<T>>
Map<Integer, List<Student>> map = stu.stream().collect(Collectors.groupingBy(e -> e.getId()));
Map相关操作
list结构根据某个字段属性转Map,某个属性作为Map的key
// v -> v.getId()指作为map的key的字段,v -> v是指作为value的字段,(v1, v2) -> v1)是指发生key冲突时选择哪一个value作为map的value
Map<Long, DataTemplateItem> item = items.stream().collect(Collectors.toMap(v -> v.getId(), v -> v, (v1, v2) -> v1));
字符串拼接
// Collectors.joining
String set = vo.getList().stream().map(v -> v.getItemId().toString()).collect(Collectors.joining(","));
根据某一对象字段去重
//1、
List<OralDetectionOss> ossList = detectionOssList
.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(
Comparator.comparing(OralDetectionOss::getType)
)
), ArrayList::new
)
);
//2 重写实体equals、hashcode方法
使用infoList.stream().distinct().collect(Collectors.toList());
字段求和
BigDecimal sumAmount = Optional.ofNullable(moneyDetailDtos).orElse(Collections.emptyList()).stream().map(MoneyDetailDto::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
持续更新中。。。