java8流分组聚合

时间:2015-06-03 11:16:23

标签: sum java-8 grouping java-stream

给定java类Something

class Something {
     String parent;
     String parentName;
     String child;
     Date at;
     int noThings;

     Something(String parent, String parentName, String child, Date at, int noThings) {
          this.parent = parent;
          this.parentName = parentName;
          this.child = child;
          this.at = at;
          this.noThings = noThings;
      }

      String getParent() { return parent; }
      String getChild() { return child; }
      int getNoThings() { return noThings; }
}

我有一些对象列表,

List<Something> hrlySomethings = Arrays.asList(
    new Something("parent1", "pname1", "child1", new Date("01-May-2015 10:00:00"), 4),
    new Something("parent1", "pname1", "child1", new Date("01-May-2015 12:00:00"), 2),
    new Something("parent1", "pname1", "child1", new Date("01-May-2015 17:00:00"), 8),
    new Something("parent1", "pname1", "child2", new Date("01-May-2015 07:00:00"), 12),
    new Something("parent1", "pname1", "child2", new Date("01-May-2015 17:00:00"), 14),
    new Something("parent2", "pname2", "child3", new Date("01-May-2015 11:00:00"), 3),
    new Something("parent2", "pname2", "child3", new Date("01-May-2015 16:00:00"), 2));

我希望按父项和子项对对象进行分组,然后查找过去24小时内“noThings”字段的总数/总和。

List<Something> dailySomethings = Arrays.asList(
    new Something("parent1", "pname1", "child1", new Date("01-May-2015 00:00:00"), 14),
    new Something("parent1", "pname1", "child2", new Date("01-May-2015 00:00:00"), 26),
    new Something("parent2", "pname2", "child3", new Date("01-May-2015 00:00:00"), 5))

我正在尝试使用流来执行此操作

我可以弄清楚如何使用分组来获取地图地图和总数

    Map<String,Map<String,IntSummaryStatistics>> daily =
        hrlySomethings.stream().collect(
    Collectors.groupingBy(Something ::getParent, 
    Collectors.groupingBy(ClientCollectionsReceived::getChild,
    Collectors.summarizingInt(ClientCollectionsReceived::getNoThings))));

我可以弄清楚如何根据父母和孩子获得一个独特的列表,

    Date startHour = "01-May-2015 00:00:00";
    int totalNoThings = 0; // don't know how to put sum in here
    List<Something> newList 
      = hrlySomethings.stream()
            .map((Something other) -> {
                    return new Something(other.getParent(),
                    other.getChild(), startHour, totalNoThings);
                })
            .distinct()
            .collect(Collectors.toList());

但我不知道如何将两者结合起来获得不同的列表,总计。这可能吗?

1 个答案:

答案 0 :(得分:2)

首先,我假设您使用的是java.util.Date(尽管我建议您转到新的java.time API)。其次,我假设Something类已正确实施equalshashCode。还需要更多的吸气剂:

String getParentName() { return parentName; }
Date getAt() { return at; }

在这些假设下,您的任务可以像这样解决:

List<Something> dailySomethings = hrlySomethings.stream().collect(
    Collectors.groupingBy(
        smth -> new Something(smth.getParent(), 
                              smth.getParentName(), 
                              smth.getChild(), 
                              new Date(smth.getAt().getYear(),
                                       smth.getAt().getMonth(), 
                                       smth.getAt().getDate()), 
                              0),
        Collectors.summingInt(Something::getNoThings)
    )).entrySet().stream()
                 .map(entry -> new Something(entry.getKey().getParent(),
                                             entry.getKey().getParentName(), 
                                             entry.getKey().getChild(), 
                                             entry.getKey().getAt(), 
                                             entry.getValue()))
                 .collect(Collectors.toList());

我们只使用groupingBy一次,但创建了一个合适的分组键,Something parentparentNamechild设置为原始,at已更改为开始日期,noThings设置为零。这样你就可以分组你想要的东西。如果您只需要总金额,则不需要summarizingIntsummingInt就足够了。之后,我们将生成的地图转换为列表,创建新的Something对象,其中noThings从地图值填充,其余部分从键中填充。

相关问题