Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

      • 聚合操作模块
      • 流工具-StreamUtil
      • 收集器工具-CollectorUtil
        • 1. 基本收集器
          • 1.1 收集到集合
          • 1.2 收集到Map
        • 2. 高级收集器
          • 2.1 分组收集
          • 2.2 分区收集
          • 2.3 聚合收集
        • 3. 字符串收集器
          • 3.1 字符串连接
        • 4. 自定义收集器
          • 4.1 创建自定义收集器
        • 5. 收集器组合
          • 5.1 映射后收集
        • 6. 适用场景
      • Map Entry流-EntryStream
    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • 聚合操作(stream)
Hutool
2026-01-03
目录

收集器工具-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,开发者可以更加便捷地使用收集器,编写更简洁、更易维护的代码,同时享受到函数式编程带来的好处。

流工具-StreamUtil
Map Entry流-EntryStream

← 流工具-StreamUtil Map Entry流-EntryStream→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式