Java Stream Collectors 用法

Java Stream 的 Collectors 类提供了大量用于将流中元素收集到不同数据结构或执行聚合操作的工具方法。以下是其主要用法和高级功能:

1. 基础收集方法

toList()、toSet()、toMap()

这些是最常用的收集方法,分别将流元素收集到 List、Set 和 Map 中:

List<String> list = stream.collect(Collectors.toList()); Set<String> set = stream.collect(Collectors.toSet()); Map<String, Integer> map = stream.collect(Collectors.toMap(String::toLowerCase, String::length));

toMap 用于将流中的元素转换为 Map。当存在重复键时,需要提供合并函数。例如:

Map<String, String> idToName = people.stream()

.collect(Collectors.toMap(Person::getId, Person::getName,

(existing, replacement) -> replacement));

此外,还可以使用 toUnmodifiableMap(Java 10+)创建不可变的 Map:

Map<String, String> unmodifiableMap = people.stream()

.collect(Collectors.toUnmodifiableMap(Person::getId, Person::getName));

toCollection() 允许指定具体的集合类型:

List<String> list = stream.collect(Collectors.toCollection(ArrayList::new)); Set<String> set = stream.collect(Collectors.toCollection(HashSet::new));

2. 分组与分区

groupingBy()进行分组并结合下游收集器

根据指定条件对元素进行分组,返回 Map:

Map<String, List<Person>> peopleByCity = people.stream() .collect(Collectors.groupingBy(Person::getCity));

可以结合下游收集器进行统计:

例如,按条件分组并统计每组数量:

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

这种用法可以轻松实现类似 SQL 中 GROUP BY 的功能,并配合 counting()、summingInt() 等下游收集器进行统计操作。

groupingBy()进行多级分组

可以通过嵌套使用 groupingBy 实现多级分组。例如,先按部门分组,再按职位分组:

Map<String, Map<String, List<Employee>>> byDeptAndPosition = employees.stream()

.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.groupingBy(Employee::getPosition)));

partitioningBy()进行二元分类

根据布尔条件将元素分为两组:

Map<Boolean, List<Person>> partitioned = people.stream() .collect(Collectors.partitioningBy(p -> p.getAge() >= 18));

也可以结合下游收集器,如统计每个分区的元素数量:

Map<Boolean, Long> counts = numbers.stream()

.collect(Collectors.partitioningBy(n -> n % 2 == 0, Collectors.counting()));

3. 聚合与统计

counting() 统计元素数量:

long count = stream.collect(Collectors.counting());

summingInt()、summingLong()、summingDouble()

对数值字段进行求和:

int total = people.stream().collect(Collectors.summingInt(Person::getAge));

averagingInt()、averagingLong()、averagingDouble()

计算平均值:

double average = people.stream().collect(Collectors.averagingInt(Person::getAge));

summarizingInt() 等

提供全面的统计信息(计数、总和、平均值、最大值、最小值):

IntSummaryStatistics stats = people.stream() .collect(Collectors.summarizingInt(Person::getAge));

4. 字符串连接

joining()

将流中的字符串元素连接成一个字符串:

String result = strings.stream().collect(Collectors.joining(", "));

5. reducing 实现归约操作

reducing()

执行自定义的归约操作:

Optional<Integer> sum = numbers.stream().collect(Collectors.reducing(Integer::sum));

reducing 用于对流中的元素进行归约操作,例如求和或查找最大值:

// 求和

int sum = numbers.stream()

.collect(Collectors.reducing(0, Integer::sum));

// 查找最长字符串

Optional<String> longest = words.stream()

.collect(Collectors.reducing((a, b) -> a.length() > b.length() ? a : b));

6. 后处理

collectingAndThen()

在收集完成后对结果进行处理:

List<String> immutableList = stream.collect(Collectors.collectingAndThen( Collectors.toList(), Collections::unmodifiableList));

7. 并发收集

groupingByConcurrent()

支持并发分组操作:

Map<String, List<Person>> concurrentGrouped = people.stream() .collect(Collectors.groupingByConcurrent(Person::getCity));

8. 统计信息收集

Collectors 提供了多种用于统计的收集器,如 summarizingInt、summarizingDouble 等,可以方便地计算总和、平均值、最大值、最小值等:

IntSummaryStatistics stats = students.stream()

.collect(Collectors.summarizingInt(Student::getScore));

9. 自定义收集器

可以通过 Collector 接口创建自定义收集器,实现特定的收集逻辑。

这些功能使得 Collectors 成为 Java Stream API 中处理数据聚合、分组和转换的强大工具。

相关推荐
2601_967980111 小时前
Python 办公自动化合集 ⑰:Excel自动计算——总和/平均分/最大值
python·pycharm·办公自动化·数据处理·excel自动计算
她的男孩3 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
databook3 小时前
时间序列预测,模型换了一圈为什么没变好
python·数据分析
reasonsummer3 小时前
【教学类-07-17】20261119大3班破译电话号码HTML实际操作(添加“跳过按钮”和“返回按钮”在底部、增加“自编题目+解题”部分)
python·html
咖啡八杯4 小时前
LoginUser 适配器模式:适配 SysUser 到 UserDetails
java·开发语言·后端·框架·适配器模式·若依
北冥有鱼5 小时前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger5 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
2601_967980117 小时前
Python闭包(Closure)详解
python·作用域·装饰器·函数式编程·闭包
AI创界者7 小时前
ComfyUI 进阶实战:MiniMax-H3 视频人物脸部替换工作流全解析(对比 Face 5.5 实测与避坑指南)
人工智能·python·aigc
承渊政道7 小时前
【Python编程快速上手—让繁琐工作自动化】(模式匹配与正则表达式:从找到一个号码到批量提取信息)
python·正则表达式·pycharm·自动化·模式匹配