给定一个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
类也已正确实现 equals
和hashCode
。还需要更多 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
仅一次,但创建一个合适的分组键,即 Something
与 parent
, parentName
和child
设置为原始,at
更改为当天开始和 noThings
设置为零。这样你就可以将你想要的东西分组。如果您只需要总金额,那么 summarizingInt
没有必要,summingInt
足够。之后,我们将生成的映射转换为创建新 Something
的列表。对象在 noThings
从 map 值填充,其余部分从键填充。
关于sum - java8流分组聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30618552/