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

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

相关推荐
daidaidaiyu20 小时前
JMS, ActiveMQ 学习一则
java
weixin_7042660521 小时前
SpringBoot全注解开发指南
java·spring boot·mybatis
星如雨グッ!(๑•̀ㅂ•́)و✧21 小时前
Webflux fromXXX对比
java
competes21 小时前
学生需求 交易累计积分,积分兑换奖品
java·大数据·开发语言·人工智能·java-ee
小羊子说21 小时前
Android系统中 socketpair 的源码解读与应用分析小结
android·java
splage21 小时前
Java进阶——IO 流
java·开发语言·python
always_TT1 天前
从Python_Java转学C语言需要注意什么?
java·c语言·python
一招定胜负1 天前
课堂教学质量综合评分系统
java·linux·前端
Hui Baby1 天前
spring优雅释放资源
java·spring
启山智软1 天前
【启山智软智能商城系统技术架构剖析】
java·前端·架构