Java常用的流处理方式

1、按属性分组

对 list<dto> 按某个字段分组

Map<String, List<TbEvaluationOrderInfo>> orderMap;
//按department部门属性分组
if (BusConstant.EvaStatType.DEPARTMENT.equals(evaluationOrderReq.getType())) {
	orderMap = allList.stream().collect(Collectors.groupingBy(TbEvaluationOrderInfo::getDepartment));
} else {
	//按industry行业属性分组
	orderMap = allList.stream().collect(Collectors.groupingBy(TbEvaluationOrderInfo::getIndustry));
}
2、按字段求和

List<dto>按照某个字段求和
(1)字段是Integer类型:list.stream().mapToInt(TbEvaluationOrderInfo::getStayTime).sum()
(2)字段非Integer类型:list.stream().mapToInt(e -> Integer.parseInt(e.getStayTime())).sum()
 

3、按字段排序
 List<dto>按某个字段排序
(1)字段是数字类型:list.stream().sorted(Comparator.comparing(EvaluationOrderDetailVo::getTotal).reversed()).collect(Collectors.toList()) --其中reversed()表示倒序
(2)字段是String类型:按照 DevQualityBean 的 getValue() 值倒叙排序(如果要升序排序,就将-1和1交换一下)
Collections.sort(list, new Comparator<DevQualityBean>() {
            @Override
            public int compare(DevQualityBean o1, DevQualityBean o2) {
                //这里的Float可以换成Integer、Double等数据类型,根据getValue()实际的值来确定
                if (Float.parseFloat(o1.getValue()) > Float.parseFloat(o2.getValue())) {
                    return -1;
                } else if (Float.parseFloat(o1.getValue()) == Float.parseFloat(o2.getValue())) {
                    return 0;
                }
                return 1;
            }
        });
 4、去除某个属性组成新的list

(1)全部取:list.stream().map(TbProcessCoincidenceInfo::getDepartment).collect(Collectors.toList());
(2)去重取:list.stream().map(TbProcessCoincidenceInfo::getDepartment).distin().collect(Collectors.toList());

5、按照某个属性字段去重

//按照 TbEvaluationOrderInfo 的 getDepartment 字段去重
    orderList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TbEvaluationOrderInfo::getDepartment))), ArrayList::new
        ));

6、根据某一对象属性求最大值、最小值、平均值、总和、个数

(1)字段是数字类型:IntSummaryStatistics statistics = resultList.stream().mapToInt(EvaluationOrderDetailVo::getTotal).summaryStatistics();
(2)字段是String类型:IntSummaryStatistics resultNum = list.stream().mapToInt((item)->Integer.parseInt(item.getValue())).summaryStatistics();
            System.out.println(resultNum.getMax());
            System.out.println(resultNum.getMin());
            System.out.println(resultNum.getAverage());
            System.out.println(resultNum.getCount());
            System.out.println(resultNum.getSum());    

7、取某两个属性变成Map

Map<String, String> dictionaryMap = dictionary.stream().collect(Collectors.toMap(TbDictionary::getId, TbDictionary::getName)); 

8、取两个list的差集

//总设备 List<DeviceDetail> allDeviceList;
//在线设备
List<DeviceDetail> onlineDeviceList;
//计算离线设备(所有的 - 在线的):按照字段deviceCode设备编码取差

List<DeviceDetail> offlineDeviceList = allDeviceList.stream().filter(item -> !onlineDeviceList.stream().map(all -> all.getDeviceCode()).collect(Collectors.toList()).contains(item.getDeviceCode())) .collect(Collectors.toList());

9、在一个List里面找到第一个符合条件的数据就返回
ArrayList<People> peopleList = Lists.newArrayList();
    peopleList.add(new People(1, "小王", 1));
    peopleList.add(new People(3, "小李", 3));
    peopleList.add(new People(2, "小张", 2));
    peopleList.add(new People(4, "小皇", 4));
    People people = peopleList.stream().filter(c -> c.getJgid() % 2 == 0).findFirst().orElse(null);
10、list转成另外一个list

//List<dto1>转换成List<dto2>

List<Region> list;

List<RegionDto> list2 =  list.stream().map(dto -> {
                RegionDto regionDto = new RegionDto();
                BeanUtil.copyProperties(dto, regionDto);
                regionDto.setLabel(dto.getName());
                regionDto.setIcon("h-icon-info_organization");
                regionDto.setIsLeaf(false);
                regionDto.setChildren(new ArrayList<>());
                return regionDto;
            }).collect(Collectors.toList()); 

 11、list<String>转成List<Integer>

List<String> statusListStr = Arrays.asList(status.split(","));
List<Integer> statusList = new ArrayList<>();
CollectionUtils.collect(statusListStr, new Transformer() {
      @Override
      public Object transform(Object o) {
            return Integer.valueOf(o.toString());
       }
 }, statusList);

12、map按照key值排序

//按照属性ctrId将doorList分组成map

Map<Integer, List<ViewDoor>> doorMap = doorList.stream().collect(Collectors.groupingBy(ViewDoor::getCtrId));
//对map按照ctrId进行排序

Map<Integer, List<ViewDoor>> doorMapSort = new LinkedHashMap<>();
    doorMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> doorMapSort.put(x.getKey(), x.getValue()));

 13、遍历map

for(Map.Entry<String, List<TbEvaluationOrderInfo>> entry : orderMap.entrySet()) {
     List<TbEvaluationOrderInfo> tempList = entry.getValue();
}

  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值