sum - java8流分组聚合

标签 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 的 map ,以及总数

    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());

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

最佳答案

首先我假设您正在使用java.util.Date (尽管我建议您转向新的 java.time API)。其次,我假设Something类也已正确实现 equalshashCode 。还需要更多 setter/getter :

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仅一次,但创建一个合适的分组键,即 Somethingparent , parentNamechild设置为原始,at更改为当天开始和 noThings设置为零。这样你就可以将你想要的东西分组。如果您只需要总金额,那么 summarizingInt没有必要,summingInt足够。之后,我们将生成的映射转换为创建新 Something 的列表。对象在 noThings从 map 值填充,其余部分从键填充。

关于sum - java8流分组聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30618552/

相关文章:

wpf - 在样式中设置 DataGrid.GroupStyle

grouping - 我可以对应用程序设置键进行分组吗?

mysql - 求别名的总和,然后分组

sql - 需要对总和和总和的有效查询

C 递归函数 - 在 main 中调用函数显示不正确的值

Java8 + Jcraft = 该算法的 key 太长

c++ - 按位分量和中的段错误

Java 8 : Stream, NIO 和 Lambda

eclipse - 应用程序无法在 Eclipse 内的 Tomcat 服务器上运行

matlab - 乐趣取决于潜艇的顺序和 accumarray 中的值