List分组求和

list根据某个字段分组,并且统计另一个字段的总和。

可以实现跟MySql的group by一样的效果。

实体类:

import lombok.Data;

@Data
public class CountVO {

    private String name;
    private Long count;

    public CountVO() {
    }

    public CountVO(String name, Long count) {
        this.name = name;
        this.count = count;
    }
}
public class testDemo {
    public static void main(String[] args) {
        List<CountVO> countVOList = new LinkedList<>();
        countVOList.add(new CountVO("苹果",100L,"红富士"));
        countVOList.add(new CountVO("苹果",130L,"金元帅"));
        countVOList.add(new CountVO("香蕉",120L,"芭蕉"));
        countVOList.add(new CountVO("香蕉",130L,"米蕉"));
        countVOList.add(new CountVO("西瓜",140L,"麒麟瓜"));
        countVOList.add(new CountVO("西瓜",120L,"黑金刚"));

        countVOList = countVOList.stream()
                // 根据name分组求和, 返回的是Map
                // CountVO::getName 需要分组的字段
                // Collectors.summingLong(CountVO::getCount)  统计总数
                .collect(Collectors.groupingBy(CountVO::getName, Collectors.summingLong(CountVO::getCount)))
                //转为List集合
                .entrySet().stream()
                .map(e -> new CountVO( e.getKey(),e.getValue()))
                .collect(Collectors.toList());
        System.out.println(JSON.toJSONString(countVOList));
    }
}


 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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.csdn.net/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.csdn.net/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 ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值