在Java的集合操作里,统计元素数量是非常高频的需求,传统方式往往需要手动遍历集合累加计数,而Java 8引入的Stream API搭配Collectors.counting收集器,可以用更简洁的方式完成各类计数操作,适配不同的业务场景。

Collectors.counting基础用法
Collectors.counting是java.util.stream.Collectors类下的静态方法,返回的是一个Collector收集器,用于将流中的元素计数,最终返回Long类型的统计结果。最基础的用法是直接对集合的所有元素进行计数,示例如下:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CountingDemo {
public static void main(String[] args) {
// 创建一个字符串集合
List<String> fruitList = Arrays.asList("苹果", "香蕉", "橘子", "苹果", "葡萄");
// 使用Collectors.counting统计总元素数量
Long totalCount = fruitList.stream().collect(Collectors.counting());
System.out.println("集合总元素数量:" + totalCount);
}
}
上述代码中,先将集合转换为流,再通过collect方法传入Collectors.counting()收集器,最终得到集合的总元素数量,输出结果为5。
结合过滤操作的计数
实际业务中经常需要先过滤符合条件的元素,再统计数量,此时可以配合Stream的filter方法使用,先筛选出目标元素,再进行计数。比如统计集合中"苹果"的出现次数:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FilterCountingDemo {
public static void main(String[] args) {
List<String> fruitList = Arrays.asList("苹果", "香蕉", "橘子", "苹果", "葡萄");
// 过滤出"苹果"后统计数量
Long appleCount = fruitList.stream()
.filter(fruit -> "苹果".equals(fruit))
.collect(Collectors.counting());
System.out.println("苹果的数量:" + appleCount);
}
}
该示例中filter方法先筛选出等于"苹果"的元素,后续的counting收集器只统计过滤后的流中元素数量,最终输出结果为2。
分组场景下的计数
当需要对集合元素按某个规则分组,同时统计每个分组的数量时,可以结合Collectors.groupingBy使用,groupingBy的第一个参数是分组规则,第二个参数传入Collectors.counting()作为下游收集器,即可完成分组计数。示例如下:
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class GroupCountingDemo {
public static void main(String[] args) {
List<String> fruitList = Arrays.asList("苹果", "香蕉", "橘子", "苹果", "葡萄", "香蕉", "橘子");
// 按水果名称分组,统计每个水果的数量
Map<String, Long> fruitCountMap = fruitList.stream()
.collect(Collectors.groupingBy(fruit -> fruit, Collectors.counting()));
// 遍历输出分组计数结果
fruitCountMap.forEach((fruit, count) -> System.out.println(fruit + "的数量:" + count));
}
}
运行上述代码,会输出每个水果对应的数量:苹果的数量:2,香蕉的数量:2,橘子的数量:2,葡萄的数量:1。
与传统计数方式的对比
传统统计集合元素数量的方式通常是使用for循环遍历,手动定义计数器累加,比如统计集合总数量的传统写法:
import java.util.Arrays;
import java.util.List;
public class TraditionalCountDemo {
public static void main(String[] args) {
List<String> fruitList = Arrays.asList("苹果", "香蕉", "橘子", "苹果", "葡萄");
long count = 0;
for (String fruit : fruitList) {
count++;
}
System.out.println("集合总元素数量:" + count);
}
}
对比可见,使用Collectors.counting的方式代码更简洁,可读性更强,尤其是在复杂的过滤、分组场景下,不需要手动维护计数器逻辑,减少了出错概率。同时Collectors.counting返回的是Long类型,避免了int类型可能出现的溢出问题,更适合处理大容量集合的计数场景。
注意事项
- Collectors.counting返回的结果是Long类型,如果需要转换为int类型,要注意数值范围,避免溢出。
- 如果流是并行流,Collectors.counting也能保证计数的线程安全,不需要额外加锁处理。
- 对于空集合,使用Collectors.counting统计的结果是0,不会抛出异常,使用的时候不需要额外做空集合判断。
Collectors.counting的底层实现其实就是对流中的元素进行累加,等效于调用Stream的count()方法,两者的统计结果一致,开发者可以根据代码风格选择使用,count()方法写起来更简短,而Collectors.counting更适合在复杂的收集器组合场景中使用。
Collectors.countingJava_stream集合计数Stream_API修改时间:2026-06-27 09:51:23