程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • 快速入门

  • 克隆

  • 类型转换

  • 日期时间

  • IO流相关

  • 工具类

  • 语言特性

  • JavaBean

  • 集合类

    • 集合工具 - `CollUtil`
    • 列表工具 - `ListUtil`
      • 1. 获取满足指定规则的所有元素位置
      • 2. 拆分列表
      • 3. 平均拆分列表
      • 4. 编辑列表元素
      • 5. 查找元素位置
      • 6. 列表截取
      • 7. 列表排序
      • 8. 元素交换
      • 9. 列表分页
      • 10. 列表分组
    • 迭代器工具 - `IterUtil`
    • 有界优先队列 - `BoundedPriorityQueue`
    • 线程安全的 HashSet - `ConcurrentHashSet`
    • 集合串行流工具 - `CollStreamUtil`
    • 行遍历器 - `LineIter`
  • Map

  • Codec编码

  • 文本操作

  • 注解

  • 比较器

  • 异常

  • 数学

  • 线程和并发

  • 图片

  • 网络

  • 源码编译

  • 配置文件

  • 日志

  • 缓存

  • JSON

  • 加密解密

  • DFA查找

  • HTTP客户端

  • 定时任务

  • 扩展

  • 切面

  • 脚本

  • Office文档操作

  • 系统调用

  • 图形验证码

  • 网络Socket

  • JWT

  • Hutoll
  • 集合类
scholar
2024-08-20
目录

列表工具 - ListUtil

# 列表工具 - ListUtil

:: tip 简介

ListUtil 是 Hutool 针对 Java 中常用的 List 集合进行的扩展工具类,提供了丰富的实用方法,用于简化对列表的操作。它包含了如列表拆分、元素编辑、查找、排序、分页等功能,常用于处理大规模数据和复杂的业务场景。

:::

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

该方法用于获取列表中所有满足指定条件的元素的索引位置。

示例:获取满足条件的所有元素位置

import cn.hutool.core.collection.ListUtil;

import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<String> list = ListUtil.toLinkedList("A", "B", "C", "A", "B", "C");

        // 获取所有等于 "B" 的元素位置
        int[] indices = ListUtil.indexOfAll(list, "B"::equals);

        // 输出结果
        for (int index : indices) {
            System.out.println("元素 'B' 的位置: " + index);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • 方法签名:int[] indexOfAll(List<T> list, Predicate<? super T> predicate)
  • 参数:
    • list: 需要操作的 List 对象。
    • predicate: 匹配条件的 Predicate,通常使用 Lambda 表达式。
  • 返回值:int[],包含所有满足条件的元素位置。
  • 作用:快速定位列表中满足特定条件的元素,常用于数据筛选、条件过滤等场景。
  • 实际开发场景:在数据表格中寻找符合某个条件的所有记录索引时,可以使用此方法。

# 2. 拆分列表

将一个列表按指定长度拆分成多个子列表。

示例:拆分列表

import cn.hutool.core.collection.ListUtil;

import java.util.Arrays;
import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);

        // 按长度 3 拆分列表
        List<List<Integer>> splitList = ListUtil.split(list, 3);

        // 输出拆分后的子列表
        splitList.forEach(System.out::println);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • 方法签名:List<List<T>> split(List<T> list, int size)
  • 参数:
    • list: 需要操作的 List 对象。
    • size: 每个子列表的长度。
  • 返回值:List<List<T>>,包含拆分后的子列表。
  • 作用:将大列表拆分成更小的子列表,便于分批处理或分页显示。
  • 实际开发场景:在分页查询或批量处理数据时,可以使用该方法将数据拆分成更小的单元。

# 3. 平均拆分列表

将列表平均分成指定的份数,每份数量差不超过 1。

示例:平均拆分列表

import cn.hutool.core.collection.ListUtil;

import java.util.Arrays;
import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);

        // 平均拆分成 3 份
        List<List<Integer>> avgSplitList = ListUtil.splitAvg(list, 3);

        // 输出结果
        avgSplitList.forEach(System.out::println);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • 方法签名:List<List<T>> splitAvg(List<T> list, int parts)
  • 参数:
    • list: 需要操作的 List 对象。
    • parts: 要分成的份数。
  • 返回值:List<List<T>>,包含平均拆分后的子列表。
  • 作用:将数据平均分配到多个处理单元中,适合并行处理场景。
  • 实际开发场景:在多线程任务分配时,可以将数据按比例分配给不同线程进行处理。

# 4. 编辑列表元素

对列表中的每个元素进行修改,支持自定义规则。

示例:编辑列表元素

import cn.hutool.core.collection.ListUtil;

import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<String> list = ListUtil.toLinkedList("a", "b", "c");

        // 对每个元素添加前缀
        List<String> editedList = ListUtil.edit(list, str -> "edit-" + str);

        // 输出编辑后的列表
        editedList.forEach(System.out::println);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 方法签名:List<T> edit(List<T> list, UnaryOperator<T> editor)
  • 参数:
    • list: 需要操作的 List 对象。
    • editor: 修改规则,使用 UnaryOperator,通常使用 Lambda 表达式。
  • 返回值:List<T>,包含修改后的元素。
  • 作用:批量修改列表中的元素,简化常规遍历修改操作。
  • 实际开发场景:在数据处理、批量更新时可以使用该方法统一修改元素。

# 5. 查找元素位置

查找列表中所有满足条件的元素位置。

示例:查找元素位置

import cn.hutool.core.collection.ListUtil;

import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<String> list = ListUtil.toLinkedList("X", "Y", "Z", "X", "Y", "Z");

        // 查找所有等于 "Y" 的位置
        int[] indices = ListUtil.indexOfAll(list, "Y"::equals);

        // 输出结果
        for (int index : indices) {
            System.out.println("元素 'Y' 的位置: " + index);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • 方法签名:int[] indexOfAll(List<T> list, Predicate<? super T> predicate)
  • 参数:
    • list: 需要操作的 List 对象。
    • predicate: 匹配条件的 Predicate,通常使用 Lambda 表达式。
  • 返回值:int[],包含所有满足条件的元素位置。
  • 作用:快速定位列表中满足特定条件的元素,常用于数据筛选、条件过滤等场景。
  • 实际开发场景:在数据表格中寻找符合某个条件的所有记录索引时,可以使用此方法。

# 6. 列表截取

按指定的起止索引截取列表,支持负数索引。

示例:列表截取

import cn.hutool.core.collection.ListUtil;

import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<Integer> list = ListUtil.of(1, 2, 3, 4, 5);

        // 截取索引 2 到 4 的子列表
        List<Integer> subList = ListUtil.sub(list, 2, 4);

        // 输出截取的子列表
        subList.forEach(System.out::println);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 方法签名:List<T> sub(List<T> list, int start, int end)
  • 参数:
    • list: 需要操作的 List 对象。
    • start: 起始索引(包含),支持负数。
    • end: 结束索引(不包含),支持负数。
  • 返回值:List<T>,包含截取的子列表。
  • 作用:在处理大列表时,获取其中的部分数据,简化区间操作。
  • 实际开发场景:在分页、区间处理时,经常需要从大列表中截取一部分数据。

# 7. 列表排序

按指定属性对列表中的对象进行排序。

示例:列表排序

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;

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

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表,包含多个对象
        List<Item> items = ListUtil.toList(
                new Item(3, "Item3"),
                new Item(1, "Item1"),
                new Item(2, "Item2")
        );

        // 按照 order 属性排序
        List<Item> sortedItems = ListUtil.sortByProperty(items, "order");

        // 输出排序后的结果
        sortedItems.forEach(item -> System.out.println(item.getName()));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  • 方法签名:List<T> sortByProperty(List<T> list, String propertyName)
  • 参数:
    • list: 需要排序的 List 对象。
    • propertyName: 排序依据的属性名称。
  • 返回值:List<T>,返回排序后的列表。
  • 作用:对包含对象的列表进行自定义属性排序,适用于根据特定字段进行排序的场景。
  • 实际开发场景:在处理对象列表时,可以根据某个字段进行升序或降序排序,例如商品列表按价格排序。

# 8. 元素交换

将指定元素与目标位置的元素交换位置。

示例:元素交换

import cn.hutool.core.collection.ListUtil;

import java.util.Arrays;
import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<Integer> list = Arrays.asList(1, 2, 3, 4);

        // 将元素 3 移动到索引 1 位置
        ListUtil.swapTo(list, 3, 1);

        // 输出交换后的列表
        list.forEach(System.out::println);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • 方法签名:List<T> swapTo(List<T> list, T element, int targetIndex)
  • 参数:
    • list: 需要操作的 List 对象。
    • element: 需要交换的元素。
    • targetIndex: 目标位置的索引。
  • 返回值:List<T>,返回交换后的列表。
  • 作用:在列表中调整元素顺序,适用于拖拽排序等操作。
  • 实际开发场景:在用户界面中,通过拖拽操作调整元素位置时,可以使用该方法进行元素交换。

# 9. 列表分页

根据页码和每页条目数,返回指定页的所有数据。

示例:列表分页

import cn.hutool.core.collection.ListUtil;

import java.util.ArrayList;
import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        // 获取第一页的数据,每页 2 条
        List<String> page = ListUtil.page(0, 2, list);

        // 输出分页后的列表
        page.forEach(System.out::println);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  • 方法签名:List<T> page(int pageNo, int pageSize, List<T> list)
  • 参数:
    • pageNo: 页码,从 0 开始。
    • pageSize: 每页的条目数。
    • list: 需要分页的 List 对象。
  • 返回值:List<T>,返回当前页的数据。
  • 作用:在处理分页逻辑时,简化数据截取操作。
  • 实际开发场景:在前端或后台实现分页功能时,可以使用该方法获取指定页的数据。

# 10. 列表分组

将列表按照指定长度进行分组,每组为一个新列表。

示例:列表分组

import cn.hutool.core.collection.ListUtil;

import java.util.Arrays;
import java.util.List;

public class ListUtilExample {
    public static void main(String[] args) {
        // 初始化列表
        List<String> list = Arrays.asList("a", "b", "c", "d");

        // 按长度 2 进行分组
        List<List<String>> partitionedList = ListUtil.partition(list, 2);

        // 输出分组后的列表
        partitionedList.forEach(System.out::println);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • 方法签名:List<List<T>> partition(List<T> list, int size)
  • 参数:
    • list: 需要操作的 List 对象。
    • size: 每组的长度。
  • 返回值:List<List<T>>,包含分组后的子列表。
  • 作用:在处理数据时,将大列表分成更小的组,便于批处理或分片操作。
  • 实际开发场景:在并发处理或多线程任务中,可以将数据分组后分配到不同线程处理。

以上就是按照字符串工具 - StrUtil 的模板对列表工具 - ListUtil 进行的重新总结,包括各个 API 的详细说明、使用场景以及完整的代码示例。如果有更多内容需要补充或进一步探讨,欢迎继续交流!

编辑此页 (opens new window)
上次更新: 2024/12/28, 18:32:08
集合工具 - `CollUtil`
迭代器工具 - `IterUtil`

← 集合工具 - `CollUtil` 迭代器工具 - `IterUtil`→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式