收集器工具-CollectorUtil
CollectorUtil是Hutool中用于增强JDK Collector API的工具类,提供了丰富的收集器实现,让流的收集操作更加便捷和强大。
# 1. 基本收集器
# 1.1 收集到集合
// 收集到List
List<String> list = Stream.of("a", "b", "c").collect(CollectorUtil.toList());
// 收集到Set
Set<String> set = Stream.of("a", "b", "c").collect(CollectorUtil.toSet());
// 收集到LinkedList
LinkedList<String> linkedList = Stream.of("a", "b", "c").collect(CollectorUtil.toCollection(LinkedList::new));
# 1.2 收集到Map
// 收集到Map
List<User> users = Arrays.asList(new User(1, "张三"), new User(2, "李四"));
Map<Integer, User> userMap = users.stream().collect(CollectorUtil.toMap(User::getId, Function.identity()));
// 收集到Map,处理重复键
Map<Integer, User> userMap = users.stream().collect(CollectorUtil.toMap(User::getId, Function.identity(), (existing, replacement) -> existing));
// 收集到LinkedHashMap
LinkedHashMap<Integer, User> linkedHashMap = users.stream().collect(CollectorUtil.toLinkedMap(User::getId, Function.identity()));
# 2. 高级收集器
# 2.1 分组收集
// 根据属性分组
List<User> users = Arrays.asList(
new User(1, "张三", "男"),
new User(2, "李四", "男"),
new User(3, "王五", "女")
);
Map<String, List<User>> groupByGender = users.stream().collect(CollectorUtil.groupingBy(User::getGender));
// 分组并统计数量
Map<String, Long> countByGender = users.stream().collect(CollectorUtil.groupingBy(User::getGender, CollectorUtil.counting()));
// 分组并求和
Map<String, Integer> sumById = users.stream().collect(CollectorUtil.groupingBy(User::getGender, CollectorUtil.summingInt(User::getId)));
# 2.2 分区收集
// 根据条件分区
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Map<Boolean, List<Integer>> partition = numbers.stream().collect(CollectorUtil.partitioningBy(num -> num % 2 == 0));
// 分区并统计数量
Map<Boolean, Long> partitionCount = numbers.stream().collect(CollectorUtil.partitioningBy(num -> num % 2 == 0, CollectorUtil.counting()));
# 2.3 聚合收集
// 统计数量
long count = Stream.of("a", "b", "c").collect(CollectorUtil.counting());
// 求和
int sum = Stream.of(1, 2, 3).collect(CollectorUtil.summingInt(num -> num));
// 求平均值
Double average = Stream.of(1, 2, 3).collect(CollectorUtil.averagingInt(num -> num));
// 求最大值
Optional<Integer> max = Stream.of(1, 2, 3).collect(CollectorUtil.maxBy(Comparator.naturalOrder()));
// 求最小值
Optional<Integer> min = Stream.of(1, 2, 3).collect(CollectorUtil.minBy(Comparator.naturalOrder()));
# 3. 字符串收集器
# 3.1 字符串连接
// 简单连接
String joined = Stream.of("a", "b", "c").collect(CollectorUtil.joining());
// 用分隔符连接
String joinedWithSeparator = Stream.of("a", "b", "c").collect(CollectorUtil.joining(","));
// 带前缀、分隔符和后缀连接
String joinedWithPrefixSuffix = Stream.of("a", "b", "c").collect(CollectorUtil.joining(",", "[", "]"));
# 4. 自定义收集器
# 4.1 创建自定义收集器
// 创建自定义收集器
Collector<String, ?, List<String>> customCollector = CollectorUtil.of(
ArrayList::new, // 供应器
(list, str) -> list.add(str.toUpperCase()), // 累加器
(list1, list2) -> { list1.addAll(list2); return list1; }, // 组合器
Function.identity() // 完成器
);
List<String> upperCaseList = Stream.of("a", "b", "c").collect(customCollector);
# 5. 收集器组合
# 5.1 映射后收集
// 映射后收集到List
List<String> upperCaseList = Stream.of("a", "b", "c")
.collect(CollectorUtil.mapping(String::toUpperCase, CollectorUtil.toList()));
// 映射后分组
Map<String, List<Integer>> groupByLength = Stream.of("a", "ab", "abc")
.collect(CollectorUtil.groupingBy(String::length, CollectorUtil.mapping(String::length, CollectorUtil.toList())));
# 6. 适用场景
CollectorUtil适用于以下场景:
- 需要将流数据收集到各种容器时
- 需要进行分组、分区等复杂收集操作时
- 需要自定义收集逻辑时
- 需要组合多个收集器进行复杂数据处理时
- 希望使用更友好的API进行收集操作时
通过CollectorUtil,开发者可以更加便捷地使用收集器,编写更简洁、更易维护的代码,同时享受到函数式编程带来的好处。