1.生成map collect
Map<String, ActivityDto> map1= vinActivityList.stream().collect(Collectors.toMap(k->k.getCampaignId(), Function.identity(), (o, n) -> n));
Map<String, ActivityDto> map2= vinActivityList.stream().collect(Collectors.toMap(ActivityDto::getCampaignId, Function.identity(), (o, n) -> n));
2.过滤集合filter
//过滤后返回map
Map<String, ActivityDto> map3= vinActivityList.stream().filter(k -> k.getPackageType()== Constant.PackageType.DIFFPACKAGE.getValue()).collect(Collectors.toMap(DstPkgScriptDto :: getEcuSVer, Function.identity(), (o, n) -> n));
//过滤后返回对象
ActivityDto dto1= vinActivityList.stream().filter(k -> k.getPackageType() == Constant.PackageType.FULLPACKAGE.getValue()).findFirst().orElse(null);
//过滤后返回集合
List<ActivityDto > list2= vinActivityList.stream().filter(e -> DateUtils.isEffectiveDate(new Date(), e.getActivityStartTime(), e.getActivityEndTime())).collect(Collectors.toList());
//去重后返回集合
List<String> distinctList = vinList.stream().distinct().collect(Collectors.toList());
3.返回集合map
//返回集合对象
List<ActivityDto> list1= vinActivityList.stream().map(dst -> {
ActivityDto dto= new ActivityDto ();
//todo
return dto;
}).collect(Collectors.toList());
//返回String 集合
List<String> campaignIdList = vinActivityList.stream().map(ActivityDto::getCampaignId).collect(Collectors.toList());
4.基于stream 删除集合对象
recordDtoList.stream().findFirst().map(vo -> {
if (vo.getCampaignId().equals(entity.getCampaignId())){
recordDtoList.remove(vo);
}
return vo;
});
5.循环forEach
dependVinEntities.stream().forEach(e->{
//查询redis数据
String dependVinActivityList = redisTemplate.opsForValue().get(ENV+Constant.ACTIVITY_DEPEND+e.getVin());
});
6.分组查询
Map<String, List<ScriptEcuDto>> groupEcuByMap = ecuDtoList.stream().collect(Collectors.groupingBy(ScriptEcuDto::getCheckKey));
//统计分组值
Map<String, Long> groupCount = students.stream()
.collect(Collectors.groupingBy(Student::getCourse, Collectors.counting()));
//先排序 再分组
Map<Integer, List<Student>> reversedSortGroupMap = list.stream().sorted(Comparator.comparing(Student::getScore).reversed())
.collect(Collectors.groupingBy(Student::getId));
7.查询是否存在相同数据
List<String> list = rules.stream() // 将List转换为流
.map(e -> e.getEcuName() + e.getIsDemarcate()) // 对每个元素执行映射操作,将EcuName和IsDemarcate拼接成字符串
.collect(Collectors.toMap(e -> e, e -> 1, Integer::sum)) // 将流中的元素收集到一个Map中,键为元素字符串,值为1,若有重复的键则将值累加
.entrySet() // 将Map转换为Set
.stream() // 将Set转换为流
.filter(entry -> entry.getValue() > 1) // 过滤出值大于1的键值对
.map(Map.Entry::getKey) // 对每个键值对执行映射操作,提取键
.collect(Collectors.toList()); // 将流中的元素收集到一个List中
8.排序
// 按年龄正序排序
List<User> sortedList = users.stream()
.sorted(Comparator.comparingInt(User::getAge))
.collect(Collectors.toList());
// 按年龄倒序排序
List<User> reverseSortedList = users.stream()
.sorted(Comparator.comparingInt(User::getAge).reversed())
.collect(Collectors.toList());
9.去重
//根据指定字段去重
List<UserSettingAddForm> list1 = listForm.stream()
.collect(Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(UserSettingAddForm::getSettingCode))
), ArrayList::new));
//先按照指定字段排序,后根据指定字段去重
List<UserSettingAddForm> list2=listForm.stream().sorted(Comparator.comparing(UserSettingAddForm::getSettingUpdateTime).reversed())
.collect(Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(UserSettingAddForm::getSettingCode))
), ArrayList::new));
//整体去重
List<String> collect1 = listForm.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());