Hutool Hutool
(opens new window)
🏡首页
📖指南
  • 核心(Hutool-core)
  • 配置文件(Hutool-setting)
  • 日志(Hutool-log)
  • 缓存(Hutool-cache)
  • JSON(Hutool-json)
  • 加密解密(Hutool-crypto)
  • DFA查找(Hutool-dfa)
  • 数据库(Hutool-db)
  • HTTP客户端(Hutool-http)
  • 定时任务(Hutool-cron)
  • 扩展(Hutool-extra)
  • 布隆过滤(Hutool-bloomFilter)
  • 切面(Hutool-aop)
  • 脚本(Hutool-script)
  • Office文档操作(Hutool-poi)
  • 系统调用(Hutool-system)
  • 图形验证码(Hutool-captcha)
  • 网络Socket(Hutool-socket)
  • JWT(Hutool-jwt)
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
  • 核心(Hutool-core)
  • 配置文件(Hutool-setting)
  • 日志(Hutool-log)
  • 缓存(Hutool-cache)
  • JSON(Hutool-json)
  • 加密解密(Hutool-crypto)
  • DFA查找(Hutool-dfa)
  • 数据库(Hutool-db)
  • HTTP客户端(Hutool-http)
  • 定时任务(Hutool-cron)
  • 扩展(Hutool-extra)
  • 布隆过滤(Hutool-bloomFilter)
  • 切面(Hutool-aop)
  • 脚本(Hutool-script)
  • Office文档操作(Hutool-poi)
  • 系统调用(Hutool-system)
  • 图形验证码(Hutool-captcha)
  • 网络Socket(Hutool-socket)
  • JWT(Hutool-jwt)
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(Hutool-core)

    • AI(Hutool-ai)

    • 克隆

    • 类型转换

    • 日期时间

    • IO流相关

    • 工具类

    • 语言特性

    • JavaBean

    • 集合类

      • 概述
      • 集合工具-CollUtil
      • 列表工具-ListUtil
        • 介绍
        • 使用
          • 获取满足指定规则所有的元素的位置
          • 拆分
          • 编辑元素
          • 查找位置
          • 列表截取
          • 排序
          • 元素交换
          • 分页
          • 分组
      • Iterator工具-IterUtil
      • 有界优先队列-BoundedPriorityQueue
      • 线程安全的HashSet-ConcurrentHashSet
      • 集合串行流工具-CollStreamUtil
      • 行遍历器-LineIter
    • Map

    • Codec编码

    • 文本操作

    • 注解

    • 比较器

    • 异常

    • 数学

    • 线程和并发

    • 图片

    • 网络

    • 源码编译

  • 配置文件(Hutool-setting)

  • 日志(Hutool-log)

  • 缓存(Hutool-cache)

  • JSON(Hutool-json)

  • 加密解密(Hutool-crypto)

  • DFA查找(Hutool-dfa)

  • 数据库(Hutool-db)

  • HTTP客户端(Hutool-http)

  • 定时任务(Hutool-cron)

  • 扩展(Hutool-extra)

  • 布隆过滤(Hutool-bloomFilter)

  • 切面(Hutool-aop)

  • 脚本(Hutool-script)

  • Office文档操作(Hutool-poi)

  • 系统调用(Hutool-system)

  • 图形验证码(Hutool-captcha)

  • 网络Socket(Hutool-socket)

  • JWT(Hutool-jwt)

  • 指南
  • 核心(Hutool-core)
  • 集合类
Hutool
2023-03-28
目录

列表工具-ListUtil

特别赞助 by:

# 介绍

List在集合中使用最为频繁,因此新版本的Hutool中针对List单独封装了工具方法。

# 使用

# 获取满足指定规则所有的元素的位置

List<String> a = ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1");
// [1, 5]
int[] indexArray = ListUtil.indexOfAll(a, "2"::equals);

其他方法与CollUtil工具类似,很多工具也有重复。

# 拆分

对集合按照指定长度分段,每一个段为单独的集合,返回这个集合的列表:

List<List<Object>> lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 1);
List<List<Object>> lists = ListUtil.split(null, 3);

也可以平均拆分,即平均分成N份,每份的数量差不超过1:

// [[1, 2, 3, 4]]
List<List<Object>> lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 1);

// [[1, 2], [3], [4]]
lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 3);

# 编辑元素

我们可以针对集合中所有元素按照给定的lambda定义规则修改元素:

List<String> a = ListUtil.toLinkedList("1", "2", "3");
final List<String> filter = (List<String>) CollUtil.edit(a, str -> "edit" + str);

// edit1
filter.get(0);

# 查找位置

List<String> a = ListUtil.toLinkedList("1", "2", "3", "4", "3", "2", "1");

// 查找所有2的位置
// [1,5]
final int[] indexArray = ListUtil.indexOfAll(a, "2"::equals);

# 列表截取

final List<Integer> of = ListUtil.of(1, 2, 3, 4);

// [3, 4]
final List<Integer> sub = ListUtil.sub(of, 2, 4);

// 对子列表操作不影响原列表
sub.remove(0);

# 排序

如我们想按照bean对象的order字段值排序:

@Data
@AllArgsConstructor
class TestBean{
	private int order;
	private String name;
}

final List<TestBean> beanList = ListUtil.toList(
		new TestBean(2, "test2"),
		new TestBean(1, "test1"),
		new TestBean(5, "test5"),
		new TestBean(4, "test4"),
		new TestBean(3, "test3")
		);

final List<TestBean> order = ListUtil.sortByProperty(beanList, "order");

# 元素交换

将指定元素和指定下标位置的元素交换位置

List<Integer> list = Arrays.asList(7, 2, 8, 9);

// 将元素8和下标为1的元素交换
ListUtil.swapTo(list, 8, 1);

# 分页

根据传入页码和每页条目数,返回该页拥有的所有数据(第一页的页码取决于PageUtil.getFirstPageNo(),默认为0)

List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
List<String> page = ListUtil.page(1, 2, list);

# 分组

通过传入分区长度,将指定列表为不同的块,每块区域的长度相同(最后一块可能小于长度)

List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
List<List<String>> partition = ListUtil.partition(list, 2);
上次更新: 2025/05/21, 11:39:56
集合工具-CollUtil
Iterator工具-IterUtil

← 集合工具-CollUtil Iterator工具-IterUtil→

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