Java8进行多个字段分组统计实现

Posted 胖子学习天地

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8进行多个字段分组统计实现相关的知识,希望对你有一定的参考价值。

 

// 分组统计
Map<String, Long> countMap = records.stream().collect(Collectors.groupingBy(o -> o.getProductType() + "_" + o.getCountry(), Collectors.counting()));

List<Record> countRecords = countMap.keySet().stream().map(key -> {
    String[] temp = key.split("_");
    String productType = temp[0];
    String country = temp[1];
    
    Record record = new Record();
    record.set("device_type", productType);
    record.set("location", country;
    record.set("count", countMap.get(key).intValue());
    return record;
}).collect(Collectors.toList());

  

以上是关于Java8进行多个字段分组统计实现的主要内容,如果未能解决你的问题,请参考以下文章