Java--ListUtil工具类,实现将一个大列表,拆分成指定长度的子列表

文章目录

前言

在项目中有时会出现列表很大,无法一次性批量操作,我们需要将列表分成指定大小的几个子列表,一份一份进行操作,本文提供这样的工具类实现这个需求。

实现代码

以下为ListUtil工具类代码实现:

java 复制代码
public class ListUtils {
    public static <T> List<List<T>> partition(final List<T> list, final int size) {
        if (list == null) {
            throw new NullPointerException("List must not be null");
        }
        if (size <= 0) {
            throw new IllegalArgumentException("Size must be greater than 0");
        }
        return new Partition<>(list, size);
    }
    private static class Partition<T> extends AbstractList<List<T>> {
        private final List<T> list;
        private final int size;
       /**
        * 
        * @param list 传入的列表
        * @param size 指定的长度,每size个数据切割为一个子列表
        */
        private Partition(final List<T> list, final int size) {
            this.list = list;
            this.size = size;
        }

       /**
        * 获取按指定长度拆分后,索引位置的子列表
        * @param index
        * @return
        */
        @Override
        public List<T> get(final int index) {
            //获取拆分后子列表的个数
            final int listSize = size();
            if (index < 0) {
                throw new IndexOutOfBoundsException("Index " + index + " must not be negative");
            }
            if (index >= listSize) {
                throw new IndexOutOfBoundsException("Index " + index + " must be less than size " +
                        listSize);
            }
            final int start = index * size;
            final int end = Math.min(start + size, list.size());
            //返回子列表
            return list.subList(start, end);
        }
        @Override
        public int size() {
            //(传入列表总长度/指定的长度)向上取整,即为拆分后子列表的个数
            return (int) Math.ceil((double) list.size() / (double) size);
        }
        @Override
        public boolean isEmpty() {
            return list.isEmpty();
        }
    }
}
执行结果
  1. 在上述类里写个main方法用以测试结果。

    java 复制代码
      public static void main(String[] args) {
         List<String> list=new ArrayList<String>();
         for (int i = 0; i <= 2000; i++) {
    	      list.add(i+"");
         }
     	 //将list每2000条数据拆分成一个子列表
      	 List<List<String>> partition = ListUtils.partition(list, 2000);
    	 System.out.println("将list每2000条数据拆分成一个子列表:");
     	 System.out.println("子列表个数:"+partition.size());
     	 System.out.println("第二个子列表的内容:");
    	 partition.get(1).forEach(System.out::print);
         System.out.println("-------------------------------------------------------");
         System.out.println("将list每10条数据拆分成一个子列表:");
         List<List<String>> partition1 = ListUtils.partition(list, 10);
         System.out.println("子列表个数:"+partition1.size());
         System.out.println("第三个子列表的内容:");
    	 partition1.get(2).forEach(s -> {
    	      System.out.print(s+" ");
         });
      }
  2. 执行main方法,得到结果如下:

  3. 分析结果

    将list每2000条数据拆分成一个子列表后,子列表个数为2,第一个子列表里的内容{0,1,...,1998,1999},第二个子列表的内容为{2000}。

    将list每10条数据拆分成一个子列表后,子列表个数为201,分别为{0,1,...,8,9},......,{1990,1991,...,1998,1999},{2000}。

    结果符合我们的要求,通过这个工具类,我们实现了所需功能。

相关推荐
码智社24 分钟前
AES加密原理详解及Java实现加解密实战
java·开发语言
萧瑟余晖1 小时前
JDK 26 新特性详解
java·开发语言
马优晨2 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
维天说5 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
Zane19945 小时前
并发 vs 并行:别再傻傻分不清了,一文讲透 Java 并发编程的第一课
java·后端
噢,我明白了6 小时前
java中Excel的导入和导出(EasyExcel)
java·开发语言·excel
吠品6 小时前
Zabbix Web界面误报Server未运行的排查与解决
java·服务器·数据库
啊湘6 小时前
天气查询API接口 按月Token鉴权 实时天气 物联网可用 文档齐全
java·后端·struts
Java内核笔记7 小时前
告别十亿美元的错误 : Spring Boot 4 空安全 (JSpecify) 实战
java·spring boot·后端
糖果店的幽灵7 小时前
langgraph的 MessagesState 解读
java·开发语言·人工智能·windows·langgraph