基于stream实现集合操作

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());
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值