java的list实现分组求和

本文介绍了如何使用Python并行流API对`DebtCust`对象列表按部门和客户号分组,计算每个组内的债务总和,适用于大数据场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对list的数据按某些属性进行分组,然后对组内某些属性求和,类似sql的group by功能。

例如以下以机构和客户号组合分组,对debt求和,最后形成一个新的list 

 public static void main(String[] args) {
        DebtCust d1 = new DebtCust().setDeptId(1L).setCustCode("1").setDebt(new BigDecimal("5.1"));
        DebtCust d2 = new DebtCust().setDeptId(2L).setCustCode("2").setDebt(new BigDecimal("5.1"));
        DebtCust d3 = new DebtCust().setDeptId(1L).setCustCode("1").setDebt(new BigDecimal("5.0"));
        DebtCust d4 = new DebtCust().setDeptId(3L).setCustCode("1").setDebt(new BigDecimal("5.0"));
        DebtCust d5 = new DebtCust().setDeptId(2L).setCustCode("2").setDebt(new BigDecimal("5.1"));
        List<DebtCust> debtCustList = Arrays.asList(new DebtCust[]{d1, d2, d3, d4, d5});
        List<DebtCust> newList = new ArrayList<>();
        debtCustList.parallelStream().collect(Collectors.groupingBy(d -> d.getDeptId() + d.getCustCode(), Collectors.toList()))
                .forEach((id, groupList) -> {
                    groupList.stream().reduce(
                            (a, b) -> new DebtCust()
                                            .setDeptId(a.getDeptId())
                                            .setCustCode(a.getCustCode())
                                            .setDebt(a.getDebt().add(b.getDebt()))
                    ).ifPresent(newList::add);
                });
        System.out.println(newList);
    }

tips:本方法仅适用大数据量统计(不适用于超大对象,例如list长度100w)

记录下看到的金句:买好的,不如买得好,买得好不如卖得巧。

所谓的价值投资,依然离不开择时,不得不承认择时是困难的,专业的芒格巴菲等智者也有失误的时候,所以,不轻易择时。

Java中,可以使用Stream的groupingBy和summingLong方法来实现List分组求和。首先,使用groupingBy方法按照指定的属性进行分组,然后使用summingLong方法对每个分组的元素进行求和。最后,可以通过entrySet方法将结果取出并保存到对应的列表中。 示例代码如下: 1. 首先,创建一个Map对象,用于存储分组求和的结果: Map<String, Long> map = list.stream() .collect(Collectors.groupingBy(TbSteelSemiProduct::getQlName, Collectors.summingLong(TbSteelSemiProduct::getGjjgSjsl))); 2. 然后,使用entrySet方法将map集合中的映射关系取出,并将结果保存到对应的列表中: Set<Entry<String, Long>> entrySet = map.entrySet(); for (Entry<String, Long> entry : entrySet) { xList.add(entry.getKey()); yList.add(entry.getValue().toString()); } 另外,如果需要对List中的其他类型进行求和,可以使用summingInt或mapToInt方法。summingInt方法对整型属性进行求和,而mapToInt方法将对象转换为整型后进行求和。 示例代码如下: 1. 使用summingInt方法对User对象的年龄属性进行求和: int ageSum = userList.stream() .collect(Collectors.summingInt(User::getAge)); 2. 使用mapToInt方法将User对象的年龄属性转换为整型后进行求和: int ageSumTwo = userList.stream() .mapToInt(User::getAge) .sum(); 这样就可以实现Java List分组求和操作了。引用提供了基于Stream的groupingBy和summingLong方法的示例代码,而引用提供了关于Java 8 Stream自定义分组求和并排序的实现的详细介绍。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Java 8中 直接通过List进行分组求和](https://blog.youkuaiyun.com/qq_16165363/article/details/117704861)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [java8 stream自定义分组求和并排序的实现](https://download.youkuaiyun.com/download/weixin_38613330/12743652)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值