3 回答
TA贡献1828条经验 获得超3个赞
虽然你的尝试没有成功,但我觉得是你最想表达的。所以我只是按照你的代码并修复它。试试这个!
Map<LocalDate, ImmutablePair<Integer, Map<String, Long>>> map = revenueRecords.stream() .collect(groupingBy(RevenueRecorder::getReportDate, collectingAndThen(toList(), list -> new ImmutablePair(list.stream().collect(summingInt(RevenueRecorder::getRevenue)), list.stream().collect(groupingBy(RevenueRecorder::getClientCode, Collectors.counting()))))));
结果如下
这是我自己的想法,希望能帮到你。╰( ̄▽ ̄)╭
TA贡献1820条经验 获得超9个赞
如果您已经使用 Java 12,则有一个新的收集器Collectors.teeing(),它允许使用两个独立的收集器进行收集,然后使用提供的 BiFunction 合并它们的结果。传递到结果收集器的每个元素都由两个下游收集器处理,然后使用指定的合并函数将它们的结果合并到最终结果中。因此Collectors.teeing()可能很适合,因为您想要计数和求和。
Map<LocalDate, Result> pairedReportDateMRR =
revenueRecords.stream().collect(Collectors.groupingBy(RevenueRecorder::getReportDate,
Collectors.teeing(Collectors.counting(),
Collectors.summingInt(RevenueRecorder::getRevenue), Result::new)));
System.out.println(pairedReportDateMRR);
//output: {2019-01-07={count=2, sum=46}, 2019-01-16={count=3, sum=224}}
出于测试目的,我使用了以下简单的静态类
static class Result {
private Long count;
private Integer sum;
public Result(Long count, Integer sum) {
this.count = count;
this.sum = sum;
}
@Override
public String toString() {
return "{" + "count=" + count + ", sum=" + sum + '}';
}
}
TA贡献1827条经验 获得超9个赞
首先,Map<LocalDate, Pair<Integer, Integer>>由于您想进行第二次分组,您无法制作地图,这意味着对于同一日期,您可能有多个客户代码,每个客户代码都有单独的计数器。
所以如果我没猜错你就不会得到这样的东西Map<LocalDate, MutablePair<Integer, Map<String, Integer>>>,如果它是正确的试试这个代码片段:
public static void main(String[] args) {
String data = "C1,2019-1-7,12\n" +
"C2,2019-1-7,34\n" +
"C1,2019-1-16,56\n" +
"C2,2019-1-16,78\n" +
"C3,2019-1-16,90";
Stream.of(data.split("\n")).forEach(System.out::println);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");
List<RevenueRecorder> revenueRecords = Stream.of(data.split("\n")).map(line -> {
String[] s = line.split(",");
String clientCode = s[0];
LocalDate reportDate = LocalDate.parse(s[1].trim(), formatter);
int revenue = Integer.parseInt(s[2]);
return new RevenueRecorder(clientCode, reportDate, revenue);
}).collect(toList());
Supplier<MutablePair<Integer, Map<String, Integer>>> supplier = () -> MutablePair.of(0, new HashMap<>());
BiConsumer<MutablePair<Integer, Map<String, Integer>>, RevenueRecorder> accumulator = (pair, recorder) -> {
pair.setLeft(pair.getLeft() + recorder.getRevenue());
pair.getRight().merge(recorder.getClientCode(), 1, Integer::sum);
};
BinaryOperator<MutablePair<Integer, Map<String, Integer>>> combiner = (p1, p2) -> {
p1.setLeft(p1.getLeft() + p2.getLeft());
p2.getRight().forEach((key, val) -> p1.getRight().merge(key, val, Integer::sum));
return p1;
};
Map<LocalDate, MutablePair<Integer, Map<String, Integer>>> pairedReportDateMRR = revenueRecords.stream()
.collect(
groupingBy(RevenueRecorder::getReportDate,
Collector.of(supplier, accumulator, combiner))
);
System.out.println(pairedReportDateMRR);
}
添加回答
举报