給出一個Java類Something
java8流分組彙總
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());
但我不知道如何將兩者結合起來,以獲取與總數不同的清單。這可能嗎?
工作!非常感謝您的意見。 – IncompleteCoder