小工具知识网

当前位置: 首页 > 行业知识 > Java常用的流处理方式

行业知识

Java常用的流处理方式

2026-10-31 04:56:57 来源:互联网转载
1、按属性分组

对 list ���某个字段分组

()
Map 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按照某个字段求和

(1)字段是Integer类型:list.stream().mapToInt(TbEvaluationOrderInfo::getStayTime).sum()

()

(2)字段非Integer类型:list.stream().mapToInt(e -> Integer.parseInt(e.getStayTime())).sum()

 

3、按字段排序
 List按某个字段排序

(1)字段是数字类型:list.stream().sorted(Comparator.comparing(EvaluationOrderDetailVo::getTotal).reversed()).collect(Collectors.toList()) --其中reversed()表示倒序

(2)字段是String类型:按照 DevQualityBean 的 getValue() 值倒叙排序(如果要升序排序,就将-1和1交换一下)

Collections.sort(list, new Comparator() {

            @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 dictionaryMap = dictionary.stream().collect(Collectors.toMap(TbDictionary::getId, TbDictionary::getName)); 

8、取两个list的差集

//总设备 List allDeviceList;

//在线设备

List onlineDeviceList;

//计算离线设备(所有的 - 在线的):按照字段deviceCode设备编码取差

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

9、在一个List里面找到第一个符合条件的数据就返回
ArrayList 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转换成List

List list;

List 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转成List

List statusListStr = Arrays.asList(status.split(","));

List 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 doorMap = doorList.stream().collect(Collectors.groupingBy(ViewDoor::getCtrId));

//对map按照ctrId进行排序

Map doorMapSort = new LinkedHashMap();

    doorMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> doorMapSort.put(x.getKey(), x.getValue()));

 13、遍历map

for(Map.Entry entry : orderMap.entrySet()) {

     List tempList = entry.getValue();

}

上一篇:打造醒目导航栏,提升用户体验——导航栏设计指南!

下一篇:怎么制作网网页?