基于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());
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());
});
