java8 流分组聚合
java8 stream grouping aggregate
给定一个 java class 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; }
}
我有一些东西的清单 objects,
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));
我想将objects按parent和children分组,然后找到"noThings"字段最近24小时的total/sum .
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))));
我可以弄清楚如何根据 parent 和 child、
获得不同的列表
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
class 也正确地实现了 equals
和 hashCode
。还需要更多吸气剂:
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
由地图值填充,其余由键填充。
给定一个 java class 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; }
}
我有一些东西的清单 objects,
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));
我想将objects按parent和children分组,然后找到"noThings"字段最近24小时的total/sum .
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))));
我可以弄清楚如何根据 parent 和 child、
获得不同的列表 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
class 也正确地实现了 equals
和 hashCode
。还需要更多吸气剂:
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
由地图值填充,其余由键填充。