java List.stream()使用技巧1——java8中stream用法详解:过滤、求和、排序和数据结构转换操作

在java实际项目开发过程中,存在大量的数据结构需要来回转换,可以使用stream()来简化操作

例如存在如下数据集合(持续更新常用操作):

List<SysNoticeDTO> noticeOldList = noticeService.dynamicSelectNoticeList(diyQuery);
1.单个属性集合:List<Obj>转List<Field>
List<String> nameList = noticeOldList.stream()
        .map(SysNoticeDTO::getStationName).collect(Collectors.toList());
2.map对象转换:List<Obj>转Map<Obj>
Map<Long, SysNoticeDTO> dtoOldMap = noticeOldList.stream()
        .collect(Collectors.toMap(SysNoticeDTO::getOperateId, 
        Function.identity(),(key1,key2) -> key2));
3.数据分组转换: List<Obj>转Map<List<obj>>
Map<Long, List<SysNoticeDTO>> dtoOldMapList = noticeOldList.stream()
        .collect(Collectors.groupingBy(SysNoticeDTO::getOperateId));
4.过滤筛选符合要求的数据集:List<Obj>转List<RequireObj>
List<SysNoticeDTO> dtoListReQuire = noticeOldList.stream()
                    .filter(e -> ((e.getReadStatus().intValue() == 1)
                            && "0".equals(e.getDelFlag())
                            )).collect(Collectors.toList())
5.配合Lambda表达式动态设置List<Obj>属性:

注意:返回值为void

noticeOldList.stream().forEach(e->{
                e.setNoticeTitle("Hello");
                e.setStatus(0);
                e.setCreateTime(new Date());
            });
6.通过自定义字段或者单属性排序
//单属性(Collections.sort方法仅支持List集合,且返回值为void)
List<String> overviewBarNameList = new ArrayList<>(overviewBarCountMap.keySet());
Collections.sort(overviewBarNameList);

//升序
originalList.stream()
                       .sorted(Comparator.comparing(ScrpPlanInputMonthImportInfo::getNoNum))
                        .collect(Collectors.toList());
//降序
originalList.stream()
                     .sorted(Comparator.comparing(ScrpPlanInputMonthImportInfo::getNoNum).reversed())
                        .collect(Collectors.toList());
7.通过自定义字段求和
//BigDecimal求和
BigDecimal decimalSum = staTypeCountObjMap.get(str).stream()
           .map(ScrpPlanInputMonthImportInfo::getSpimdSum)
           .reduce(BigDecimal.ZERO, BigDecimal::add);
//其他基本类型求和
int intSum = originalList.stream()
     .mapToInt(ScrpPlanInputMonthImportInfo::getNoNum).sum();
8.通过自定义分组过滤统计
//先过滤再分组
Map<String, List<BaseReleaseEntity>> baseReleaseMap = 
        releaseList.stream().filter(e -> StringUtils.isNotEmpty(e.getStageid()))
.collect(Collectors.groupingBy(BaseReleaseEntity::getStageid));

//先过滤再统计
int applySum = (int) applyList.stream()
        .filter(e->"2".equals(e.getType())).count();
9.其他的特殊用法

eg1:支持数据转换,精准匹配集合中的数据,例如如下

String strArray = "1,2,5,45,22,52,7";
System.out.println("原始数据:"+ strArray);
int[] toArray = Arrays.stream(strArray.trim().split(","))
      .mapToInt(Integer::parseInt).toArray();
System.out.println("需要匹配的数据:"+ JSON.toJSONString(toArray));
boolean flag = IntStream.of(toArray).anyMatch(x -> x == 2);
if(flag){
   System.out.println("匹配数据成功");
}else {
   System.out.println("匹配数据失败");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值