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)

      • 简介
        • 📚简介
        • 🏗️核心结构
          • 1. 模块划分
          • 2. 核心功能分类
          • 基本集合操作
          • 高级集合操作
        • 🚀快速开始
          • 1. 基本集合操作示例
          • 集合判空
          • 集合去重
          • 集合运算
          • 2. 列表专用操作示例
          • 3. 集合流操作示例
        • 🎯设计优势
        • 📌注意事项
        • 📚相关文档
      • 集合工具-CollUtil
      • 集合流工具-CollStreamUtil
      • 列表工具-ListUtil
      • 环形索引工具-RingIndexUtil
      • 分割迭代器工具-SpliteratorUtil
      • 迭代器工具-IterUtil
      • 集合工具-SetUtil
    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(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)
  • 集合(collection)
Hutool
2026-01-03
目录

简介

# 📚简介

Hutool的集合模块提供了丰富的集合操作工具类,为Java开发者提供了便捷的集合操作解决方案。该模块包含多个子模块,分别处理不同类型的集合操作需求,涵盖了从基本集合操作到高级流处理的各种功能。

# 🏗️核心结构

# 1. 模块划分

工具类名称 主要职责 核心功能
CollUtil 通用集合操作 集合判空、去重、并集交集差集、转换、过滤等
CollStreamUtil 集合流操作 集合与流之间的转换、流操作增强等
ListUtil 列表专用操作 列表创建、转换、操作等
RingIndexUtil 环形索引工具 实现环形结构的索引管理
SpliteratorUtil 分割迭代器工具 增强Spliterator功能
IterUtil 迭代器工具 迭代器创建、转换、操作等
SetUtil 集合专用操作 集合创建、转换、操作等

# 2. 核心功能分类

# 基本集合操作

  • 集合判空:提供多种方式判断集合是否为空
  • 集合去重:根据元素或属性去重
  • 集合运算:并集、交集、差集等
  • 集合转换:集合与字符串、数组等的转换
  • 集合编辑:过滤、修改集合元素

# 高级集合操作

  • 集合排序:根据属性、拼音等排序
  • 集合流处理:集合与流的转换、增强流操作
  • 迭代器操作:迭代器创建、转换、操作
  • 环形结构支持:实现环形索引管理

# 🚀快速开始

# 1. 基本集合操作示例

# 集合判空

List<String> list = new ArrayList<>();
// true
CollUtil.isEmpty(list);

Map<String, String> map = new HashMap<>();
// true
CollUtil.isEmpty(map);

# 集合去重

List<String> list = Arrays.asList("a", "b", "c", "a", "b");
// ["a", "b", "c"]
List<String> distinctList = CollUtil.distinct(list);

# 集合运算

List<String> list1 = Arrays.asList("a", "b", "c");
List<String> list2 = Arrays.asList("b", "c", "d");

// 并集:["a", "b", "c", "d"]
List<String> union = CollUtil.union(list1, list2);

// 交集:["b", "c"]
List<String> intersection = CollUtil.intersection(list1, list2);

// 差集:["a"]
List<String> subtract = CollUtil.subtract(list1, list2);

# 2. 列表专用操作示例

// 创建列表
List<String> list = ListUtil.of("a", "b", "c");

// 反转列表
List<String> reversed = ListUtil.reverse(list);

// 截取列表
List<String> subList = ListUtil.sub(list, 1, 3);

# 3. 集合流操作示例

List<String> list = Arrays.asList("a", "b", "c");

// 集合转流并处理
List<String> upperCaseList = CollStreamUtil.toStream(list)
    .map(String::toUpperCase)
    .collect(Collectors.toList());

# 🎯设计优势

  1. 全面覆盖:涵盖了几乎所有常用的集合操作
  2. 简单易用:提供简洁的API,一行代码即可完成复杂的集合操作
  3. 高性能:底层实现经过优化,性能优异
  4. 可扩展性强:采用接口设计,便于扩展新的功能
  5. 线程安全:所有工具类都是线程安全的,可以在多线程环境下安全使用
  6. 无外部依赖:纯Java实现,无外部依赖

# 📌注意事项

  1. 集合类型:不同的工具类适用于不同的集合类型,选择合适的工具类可以提高代码的可读性和性能
  2. 空值处理:大部分方法都进行了空值处理,但在使用时仍需注意
  3. 线程安全:工具类本身是线程安全的,但集合对象的线程安全性取决于具体的集合实现
  4. 性能考虑:对于大规模集合,某些操作可能会影响性能,建议根据实际情况选择合适的方法

# 📚相关文档

  • 集合工具-CollUtil
  • 集合流工具-CollStreamUtil
  • 列表工具-ListUtil
  • 环形索引工具-RingIndexUtil
  • 分割迭代器工具-SpliteratorUtil
  • 迭代器工具-IterUtil
  • 集合工具-SetUtil
哈希工具类-HashUtil
集合工具-CollUtil

← 哈希工具类-HashUtil 集合工具-CollUtil→

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