Stream的常用方法
List 转 Map<String,T>
1
Map<String, MesProductionPlanConfigDTO> dtoMap4MesProductionPlanConfig = dtoList4MesProductionPlanConfig.stream().collect(Collectors.toMap(MesProductionPlanConfigDTO::getId, v -> v, (v1, v2) -> v1));
单个字段排序
1
List<MesProcessText> tmpSortAscList4MesProcessText = existList4MesProcessText.stream().sorted(Comparator.comparing(MesProcessText::getSort)).collect(Collectors.toList());
多字段排序
1
Map<String, List<MesProductionPlanDayStationTimeDTO>> batchMap4MesProductionPlanDayStationTimeDTO = list4MesProductionPlanDayStationTimeDTO.stream().sorted(Comparator.comparing((MesProductionPlanDayStationTimeDTO day) -> day.getMesProductionPlan().getPlanStartTime()).thenComparing((MesProductionPlanDayStationTimeDTO day1) -> day1.getMesProductionPlan().getSortNum())).collect(Collectors.groupingBy(i -> i.getMesProductionPlan().getProductionBatchId(), LinkedHashMap::new, Collectors.toList()));
List转Map<String,List
> 1
Map<String, List<MesProcessLevelConfig>> mesProcessLevelConfigLinkMap = list.stream().collect(Collectors.groupingBy(MesProcessLevelConfig::getMatMgtId, LinkedHashMap::new, Collectors.toList()));
提取Set
1
Set<String> mesPrdOrdOptsIds = mesPrdOrdOptsList.stream().map(MesPrdOrdOpts::getId).collect(Collectors.toSet());
List转多key的Map
1
Map<String, Map<String, List<MesProcessTooling>>> existMapMapSortAscList4MesProcessTooling = existList4MesProcessTooling.stream().sorted(Comparator.comparing(MesProcessTooling::getRowNumber)).collect(Collectors.groupingBy(MesProcessTooling::getResourceType, Collectors.groupingBy(MesProcessTooling::getToolingTypeId)));
字段条件过滤
1
Map<String, List<MesProductionPlan>> mergeNotFullMap = list4WorkStationMesProductionPlan.stream().filter(item -> "NO".equals(item.getMergeFullFlag())).sorted(Comparator.comparing(MesProductionPlan::getPlanStartTime)).collect(Collectors.groupingBy(MesProductionPlan::getMergeType, LinkedHashMap::new, Collectors.toList()));
- 标题: Stream的常用方法
- 作者: Henry
- 创建于 : 2023-05-07 20:58:48
- 更新于 : 2023-08-07 21:39:52
- 链接: https://mybetterworks.github.io/2023/05/07/Stream的常用方法/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论