像Java一样用Java lambdas分组和汇总对象? [英] Group by and sum objects like in SQL with Java lambdas?

查看:82
本文介绍了像Java一样用Java lambdas分组和汇总对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 Foo ,包含以下字段:

I have a class Foo with these fields:


id:int / name; String / targetCost:BigDecimal / actualCost:BigDecimal

id:int / name;String / targetCost:BigDecimal / actualCost:BigDecimal

我得到了这个类的对象的arraylist。例如:

I get an arraylist of objects of this class. e.g.:

new Foo(1, "P1", 300, 400), 
new Foo(2, "P2", 600, 400),
new Foo(3, "P3", 30, 20),
new Foo(3, "P3", 70, 20),
new Foo(1, "P1", 360, 40),
new Foo(4, "P4", 320, 200),
new Foo(4, "P4", 500, 900)

我想通过创建targetCost和actualCost之和并将行分组来转换这些值例如

I want to transform these values by creating a sum of "targetCost" and "actualCost" and grouping the "row" e.g.

new Foo(1, "P1", 660, 440),
new Foo(2, "P2", 600, 400),
new Foo(3, "P3", 100, 40),
new Foo(4, "P4", 820, 1100)

我现在所写的内容:

data.stream()
       .???
       .collect(Collectors.groupingBy(PlannedProjectPOJO::getId));

我该怎么做?

推荐答案

使用 Collectors.groupingBy 是正确的方法,而不是使用单个参数版本,它将为每个组创建所有项目的列表你应该使用两个arg版本,它采用另一个收集器,它确定如何聚合每个组的元素。

Using Collectors.groupingBy is the right approach but instead of using the single argument version which will create a list of all items for each group you should use the two arg version which takes another Collector which determines how to aggregate the elements of each group.

当您想要聚合元素的单个属性或只计算每个元素的元素数时,这一点尤其顺利:

This is especially smooth when you want to aggregate a single property of the elements or just count the number of elements per group:


  • 计数:

  • Counting:

list.stream()
  .collect(Collectors.groupingBy(foo -> foo.id, Collectors.counting()))
  .forEach((id,count)->System.out.println(id+"\t"+count));


  • 总结一项物业:

  • Summing up one property:

    list.stream()
      .collect(Collectors.groupingBy(foo -> foo.id,
                                        Collectors.summingInt(foo->foo.targetCost)))
      .forEach((id,sumTargetCost)->System.out.println(id+"\t"+sumTargetCost));
    


  • 在你想要聚合更多的情况下指定自定义缩小操作的一个属性在此答案中建议的是正确的方法,但是,您可以执行缩减操作在分组操作期间,因此在执行缩减之前无需将整个数据收集到 Map< ...,List> 中:

    In your case when you want to aggregate more than one property specifying a custom reduction operation like suggested in this answer is the right approach, however, you can perform the reduction right during the grouping operation so there is no need to collect the entire data into a Map<…,List> before performing the reduction:

    (我假设您使用 import static java.util.stream.Collectors。*; now ...)

    (I assume you use a import static java.util.stream.Collectors.*; now…)

    list.stream().collect(groupingBy(foo -> foo.id, collectingAndThen(reducing(
      (a,b)-> new Foo(a.id, a.ref, a.targetCost+b.targetCost, a.actualCost+b.actualCost)),
          Optional::get)))
      .forEach((id,foo)->System.out.println(foo));
    






    为了完整性,这里有一个问题的解决方案您的问题的范围:如果您想要 GROUP BY 多列/属性怎么办?

    第一件事跳到程序员的脑海里,就是使用 groupingBy 来提取流的元素的属性并创建/返回一个新的密钥对象。但是这需要一个适当的持有者类来获得关键属性(而Java没有通用的Tuple类)。

    The first thing which jumps into the programmers mind, is to use groupingBy to extract the properties of the stream’s elements and create/return a new key object. But this requires an appropriate holder class for the key properties (and Java has no general purpose Tuple class).

    但还有另一种选择。使用三个arg形式的 groupingBy 我们可以为实际指定供应商 Map 实现,它将确定密钥相等。通过使用带有比较器的有序映射来比较多个属性,我们可以获得所需的行为,而无需额外的类。我们只需要注意不要使用比较器忽略的键实例中的属性,因为它们只有任意值:

    But there is an alternative. By using the three-arg form of groupingBy we can specify a supplier for the actual Map implementation which will determine the key equality. By using a sorted map with a comparator comparing multiple properties we get the desired behavior without the need for an additional class. We only have to take care not to use properties from the key instances our comparator ignored, as they will have just arbitrary values:

    list.stream().collect(groupingBy(Function.identity(),
      ()->new TreeMap<>(
        // we are effectively grouping by [id, actualCost]
        Comparator.<Foo,Integer>comparing(foo->foo.id).thenComparing(foo->foo.actualCost)
      ), // and aggregating/ summing targetCost
      Collectors.summingInt(foo->foo.targetCost)))
    .forEach((group,targetCostSum) ->
        // take the id and actualCost from the group and actualCost from aggregation
        System.out.println(group.id+"\t"+group.actualCost+"\t"+targetCostSum));
    

    这篇关于像Java一样用Java lambdas分组和汇总对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆