在Java中如何使用Collectors.counting统计元素数量_集合计数操作解析

Collectors.counting返回Long类型计数结果,用于统计流中元素数量;可单独使用统计总数,如fruits.stream().collect(Collectors.counting())得5;常与groupingBy结合实现分类计数,如按水果名称分组统计得{orange=1, banana=2, apple=2};也可基于对象属性分组,如按Person的city字段分组得{Beijing=2, Shanghai=2, Guangzhou=1};还能按条件动态分组,如按年龄段(age/10*10)统计得{20=3, 30=2};支持嵌套其他收集器,适用于数据聚合与分布分析场景。

在Java中,Collectors.counting 是一个用于统计流(Stream)中元素数量的收集器。它通常与 Collectors.groupingBy 或其他分组、过滤操作结合使用,实现对集合元素的分类计数。这个方法返回一个 Collector,其结果类型是 Long,表示匹配条件的元素个数。

基本用法:统计集合总数量

最简单的使用场景是统计整个流中的元素总数。

List fruits = Arrays.asList("apple", "banana", "orange", "apple", "banana");

long totalCount = fruits.stream() .collect(Collectors.counting());

System.out.println(totalCount); // 输出:5

这里直接通过 stream().collect(Collectors.counting()) 得到集合的总元素数,等价于调用 list.size(),但在流处理链中更自然。

结合 groupingBy 实现分类计数

更常见的用途是配合 Collectors.groupingBy 对数据按某属性分组并统计每组数量。

Map countByFruit = fruits.stream()
    .collect(Collectors.groupingBy(fruit -> fruit, Collectors.counting()));

System.out.println(countByFruit); // 输出:{orange=1, banana=2, apple=2}

上面代码将水果名称作为分组键,并使用 Collectors.counting() 统计每个名称出现的次数,相当于词频统计。

按条件或属性进行分组统计

你也可以根据对象的某个字段进行分组计数。例如有一个 Person 类:

class Person {
    String name;
    int age;
    String city;
// 构造函数
public Person(String name, int age, String city) {
    this.name = name;
    this.age = age;
    this.city = city;
}

// getter 方法省略

}

现在统计每个人所在城市的居民数量:

List people = Arrays.asList(
    new Person("Alice", 25, "Beijing"),
    new Person("Bob", 30, "Shanghai"),
    new Person("Charlie", 35, "Beijing"),
    new Person("David", 28, "Guangzhou"),
    new Person("Eva", 22, "Shanghai")
);

Map countByCity = people.stream() .collect(Collectors.groupingBy(Person::getCity, Collectors.counting()));

System.out.println(countByCity); // 输出:{Beijing=2, Shanghai=2, Guangzhou=1}

这展示了如何基于对象属性动态分组并计数。

与其他收集器组合使用

Collectors.counting() 还可以和其他收集器嵌套使用,比如只统计满足特定条件的元

素数量。

Map countByAgeGroup = people.stream()
    .collect(Collectors.groupingBy(
        p -> p.getAge() / 10 * 10,  // 按年龄段分组,如 20, 30
        Collectors.counting()
    ));

System.out.println(countByAgeGroup); // 输出:{20=3, 30=2} (假设年龄为25,28,22归为20段;30,35归为30段)

这种写法可用于生成数据分布报表。

基本上就这些。Collectors.counting 使用简单,但功能强大,尤其适合在流式处理中做聚合统计。关键是理解它作为一个“终端收集行为”,必须配合 collect 方法使用,并常用于分组上下文中。掌握它有助于写出简洁高效的数据分析代码。