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}。

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

相关推荐
爱上语文7 分钟前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
serve the people11 分钟前
springboot 单独新建一个文件实时写数据,当文件大于100M时按照日期时间做文件名进行归档
java·spring boot·后端
qmx_071 小时前
HTB-Jerry(tomcat war文件、msfvenom)
java·web安全·网络安全·tomcat
为风而战1 小时前
IIS+Ngnix+Tomcat 部署网站 用IIS实现反向代理
java·tomcat
技术无疆3 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
架构文摘JGWZ6 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
拾光师7 小时前
spring获取当前request
java·后端·spring
aPurpleBerry7 小时前
neo4j安装启动教程+对应的jdk配置
java·neo4j
我是苏苏7 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言
xujinwei_gingko7 小时前
Spring IOC容器Bean对象管理-Java Config方式
java·spring