Java8 Stream 分组求和使用笔记

Java8 Stream 分组求和使用笔记

话不多说,直接贴代码,分组使用

class Foo {
    private int code;

    private int count;

    public Foo(int code, int count) {
        this.code = code;
        this.count = count;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
public static void main(String[] args) {
        Foo foo1 = new Foo(1, 2);
        Foo foo2 = new Foo(2, 23);
        Foo foo3 = new Foo(2, 6);
        List<Foo> list = new ArrayList<>(4);
        list.add(foo1);
        list.add(foo2);
        list.add(foo3);
        Map<Integer, List<Foo>> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode));
        List<Foo> list1 = collect.get(1);
        List<Foo> list2 = collect.get(2);
        list1.forEach(e -> System.out.println(e.getCode() + ":" + e.getCount()));
        System.out.println("-----------这里是分界线-----------------------------");
        list2.forEach(e -> System.out.println(e.getCode() + ":" + e.getCount()));
    }

输出结果:

1:2
-----------这里是分界线-----------------------------
2:23
2:6

分组求和使用

public static void main(String[] args) {
        Foo foo1 = new Foo(1, 2);
        Foo foo2 = new Foo(2, 23);
        Foo foo3 = new Foo(2, 6);
        List<Foo> list = new ArrayList<>(4);
        list.add(foo1);
        list.add(foo2);
        list.add(foo3);
        Map<Integer, IntSummaryStatistics> collect = list.stream().collect(Collectors.groupingBy(Foo::getCode, Collectors.summarizingInt(Foo::getCount)));
        IntSummaryStatistics statistics1 = collect.get(1);
        IntSummaryStatistics statistics2 = collect.get(2);
        System.out.println(statistics1.getSum());
        System.out.println(statistics1.getAverage());
        System.out.println(statistics1.getMax());
        System.out.println(statistics1.getMin());
        System.out.println(statistics1.getCount());

        System.out.println(statistics2.getSum());
        System.out.println(statistics2.getAverage());
        System.out.println(statistics2.getMax());
        System.out.println(statistics2.getMin());
        System.out.println(statistics2.getCount());
    }

输出结果:

2
2.0
2
2
1
29
14.5
23
6
2

stream真的是相当的好用,Mark一下,欢迎大神在评论区留下你的Stream骚操作。

  • 11
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
引用\[3\]中提供了一种使用Java 8 Stream进行分组求和的方法。可以使用Collectors.groupingBy()方法将列表按照某个属性进行分组,然后使用Collectors.summingDouble()方法对每个分组进行求和。下面是一个示例代码: ```java Map<String, Double> collect = list.stream() .collect(Collectors.groupingBy(TestVO::getName, Collectors.summingDouble(TestVO::getAge))); System.out.println(collect); ``` 这段代码将根据TestVO对象的name属性对列表进行分组,并对每个分组的age属性进行求和。最后,将分组求和结果存储在一个Map中。你可以根据自己的需求修改属性名和数据类型。 #### 引用[.reference_title] - *1* *2* [java8 stream 过滤、排序、求和分组、去重等方法使用和说明](https://blog.csdn.net/qq_44293888/article/details/107868055)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [java8 Stream分组求和 reducing](https://blog.csdn.net/Guo_jee/article/details/123807115)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值