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

前言

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

实现代码

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

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方法用以测试结果。

      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}。
    结果符合我们的要求,通过这个工具类,我们实现了所需功能。

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝麻馅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值